summaryrefslogblamecommitdiff
path: root/blog/sbs_2/blogpost.js
blob: 4c177727a23d86e2c145a73b42ee37633cdde598 (plain) (tree)






































































































                                                                                                                                       
//License: Expat (MIT)
//Chrisoft Xiong 2017
var prev,succ,tocid=0,headerlist=[];
function request(url,func)
{
	var h=new XMLHttpRequest();
	h.open("GET",url);
	h.onload=()=>{func(h.response,h.status)};
	h.send();
}
function parsetags(tgs)
{
	var r="";
	var a=tgs.split(",");
	r="#"+a[0];
	for(var i=1;i<a.length;++i)r+=" #"+a[i];
	return r;
}
function dfs(el,le,p)
{
	var e=document.createElement('li');
	e.innerHTML='<a class="toctarg" href="#tocanch'+tocid+'">'+el.innerHTML+'</a>';
	le.appendChild(e);
	el.id='tocanch'+(tocid++);
	el.classList.add('tvis');
	var che=null,i;
	for(i=p+1;i<headerlist.length;)
	{
		if(headerlist[i].tagName<=el.tagName)break;
		if(headerlist[i].classList.contains('notoc'))continue;
		if(che===null)
		{
			var te=document.createElement('li');
			che=document.createElement('ul');
			che.classList.add('tocnode');
			te.appendChild(che);
			le.appendChild(te);
		}
		i=dfs(headerlist[i],che,i);
	}
	return i;
}
function bpinit()
{
	var parr=window.location.pathname.substr(10).split('/');
	var pflt="";ppn=0;
	if(parr.length>1&&parr[1].length)
	{
		request("/blog/cgi-bin/get-post-content.cgi?p="+parr[1],
			(r,s)=>
			{
				if(s>=400)window.location="/blog";
				var p1=r.search('\n');
				var title=r.substr(0,r.search('\n'));
				var p2=r.substr(p1+1).search('\n');
				var date=r.substr(p1+1,p2);
				var p3=r.substr(p1+p2+2).search('\n');
				var tags=r.substr(p1+p2+2,p3);
				document.getElementById("title").innerHTML=title;
				document.getElementById("titleh").innerHTML=title;
				document.getElementById("datetags").innerHTML=date+"<br>"+parsetags(tags);
				document.getElementById("article").innerHTML=r.substr(p1+p2+p3+3);
				var l=document.getElementById("article").querySelectorAll("script");
				for(var i of l)
				{
					var s=document.createElement("script");
					s.async=true;
					s.src=i.src;
					document.getElementById("article").appendChild(s);
				}
				var tgs=tags.split(',');
				for(var i=0;i<tgs.length;++i)
				{
					var l=document.createElement('li');
					l.innerHTML='<a href="/blog/list/'+tgs[i]+'/">'+tgs[i]+"</a>";
					document.getElementById('tagslist').appendChild(l);
				}
				request("/blog/cgi-bin/get-archive-list.cgi?qn="+parr[1],
				(r,s)=>
				{
					if(s>=400)return;
					var o=JSON.parse(r);
					prev=o.prev;succ=o.succ;
				}
				);
				l=document.getElementById('article').querySelectorAll('h2,h3,h4,h5,h6');
				var tocroot=document.getElementById('tocroot');
				for(var i of l)
				{
					if(!i.classList.contains('notoc'))headerlist.push(i);
				}
				for(var i=0;i<headerlist.length;)i=dfs(headerlist[i],tocroot,i);
				if(!tocroot.children.length)document.getElementById('tocouter').style.display='none';
				for(var i=0;i<tgs.length;++i)
				footnoter();
				if(window.location.hash.length&&document.querySelector(window.location.hash))
				window.scroll(window.scrollX,document.querySelector(window.location.hash).getBoundingClientRect().top);
			}
		);
	}
}
function prevpost(){if(prev.length)window.location="/blog/post/"+prev;}
function succpost(){if(succ.length)window.location="/blog/post/"+succ;}