User:Surjection/tocColumn.js

From Wiktionary, the free dictionary
Jump to navigation Jump to search

Note – after saving, you may have to bypass your browser’s cache to see the changes.

  • Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh);
  • Konqueror and Chrome: click Reload or press F5;
  • Opera: clear the cache in Tools → Preferences;
  • Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.

mw.hook("wikipage.content").add(function($) {
    var toc = document.getElementById("toc");
    var bodyContent = document.getElementById("bodyContent");
    var contentText = document.getElementById("mw-content-text");
    var tocUl = document.getElementById("toc").querySelector("ul");
    if (toc && bodyContent && contentText) {
        var tocHolder = document.createElement("div");
        var tocParent = document.createElement("div");
        tocHolder.style.display = "flex";
        tocParent.style.marginLeft = "0.5em";
        bodyContent.insertBefore(tocHolder, contentText);
        tocHolder.appendChild(contentText);
        tocHolder.appendChild(tocParent);
        tocParent.appendChild(toc);
        contentText.style.flex = "1";
        contentText.style.flexShrink = "0";
        if (tocUl) {
            tocUl.style.width = "16em";
        }
    }
});