Jump to content

User:Pasixxxx/TOCLinker.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
//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(); });