User:Pasixxxx/TOCLinker.js

//Inserts links to toc if conditions are met function MyHtmlFrag(doc) {    var sm,txt,a; txt=doc.createTextNode("[table of contents]"); this.elem=doc.createElement("a"); this.elem.setAttribute("href","#toc"); this.elem.setAttribute("class","mw-editsection"); this.elem.appendChild(txt);

return this.elem; } function insertLinks(tg,doc) {     var el,coll; var i,ch;

coll=doc.getElementsByTagName(tg);

for(i=0;i<coll.length;i++) {         el=coll.item(i); if(el.parentNode.parentNode.id=="mw-content-text") {         ch=el.childNodes[9999]; el.insertBefore(new MyHtmlFrag(doc),ch); }     } } function insertLinksToTOC {	var doc= $(document)[0]; if(($('#toc')[0]) === undefined) return;

insertLinks("h2",doc); insertLinks("h3",doc); insertLinks("h4",doc); } $(function {insertLinksToTOC; });