Jump to content

User:Ilmari Karonen/watchedittalk.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.
// ADD TALK PAGE LINKS TO THE WATCHLIST EDIT PAGE  <pre><nowiki>

addOnloadHook(function () {
    if (window.location.href.indexOf("Special:Watchlist/edit") < 0) return;

    var form = document.getElementById('content').getElementsByTagName('form')[0];
    if (!form) return;

    var ns = "";

    for (var node = form.firstChild; node; node = node.nextChild) {
        if (node.nodeType != 1) continue;

        switch (node.tagName.toLowerCase()) {
          case 'h2':
            ns = node.innerText;
            break;

          case 'ul':
            for (var item = node.firstChild; item; item = item.nextChild) {
                if (item.nodeType != 1 || item.tagName.toLowerCase() != 'li') continue;
                var link = item.getElementsByTagName('a')[0];
                if (!link || !link.title || link.title.substr(0,ns.length) != ns) continue;

                var talkLink = document.createNode('a');
                talkLink.title = (ns ? ns+" talk" : "Talk:") + link.title.substr(ns.length);
                talkLink.href = "/wiki/" + encodeURIComponent(talkLink.title);
                talkLink.appendChild(document.createTextNode('talk'));

                item.appendChild(document.createTextNode(" ("));
                item.appendChild(talkLink);
                item.appendChild(document.createTextNode(") "));
            }
            break;
        }
    }
});

// </nowiki></pre>