Jump to content

User:Bradv/Scripts/Superlinks.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Bradv (talk | contribs) at 04:08, 24 September 2018 (create). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
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.
$(function() {
    'use strict';

    var ns = mw.config.get("wgNamespaceNumber");
    if (ns >= 0) {
        var pp = {
            styleSheet: mw.util.addCSS(
                '#superlinks {display:inline-block; line-height: 24px} ' +
                //'#mw-indicator-mw-helplink a {margin-left: 0.5em} ' +
                '#superlinks > a {padding: 0 0.2em; font-size: 0.75em; } ' +
                '#superlinks-window {position: absolute; top: 48px; right: 5px; border: 2px solid #a7d7f9; width: 45vw; height: 65vh;} ' +
                '#superlinks-window:before {content: ""; position: absolute; top: -20px; width: 0; height: 0; border: 10px solid transparent; border-bottom-color: #a7d7f9; margin-right: -16px;} ' +
                '#superlinks-window:after  {content: ""; position: absolute; top: -17px; width: 0; height: 0; border: 10px solid transparent; border-bottom-color: transparent; margin-right: -16px;} ' +
                '#superlinks-close {position:absolute; left:-2px; top:-14px; content:"close"; font-size:10px; line-height:10px; background-color:#a7d7f9; padding: 2px 8px } ' +
                '#superlinks-content {overflow: auto; height: calc(100% - 10px); width: calc(100% - 10px); font-size: 90%; padding:5px; background-color: #f6fbfe;  } ' +
                '#superlinks-content > ul#pagehistory > li > input {display: none} ' +
                '#superlinks-content > #empty {position:absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); color: #a7d7f9; font-size: 1.5em; font-weight: bold;} '
            ),
            links: {},
            init: function () {
                function mk(caption) {
                    var link = document.createElement("a");
                    link.append(document.createTextNode(caption));
                    link.href="#";
                    $("#superlinks").append(link);
                    $(link).click(pp.click);
                    return link;
                }

                var div = document.createElement("div");
                div.id="superlinks";
                $("div.mw-indicators.mw-body-content").prepend(div);
                div.append(document.createTextNode("["));

                pp.articleElement = $("#p-namespaces > ul > li:first-child");
                pp.talkElement = $("#ca-talk");
                pp.historyElement =  $("#ca-history");
                if (pp.articleElement.hasClass("selected")) { //on article or article history
                    if (!pp.articleElement.hasClass("new")) {
                        if (pp.historyElement.hasClass("selected")) { //on history page
                            pp.links.article = mk(pp.articleElement.find("a").text());
                        }
                        if (!pp.talkElement.hasClass("new")) { //talk exists
                            pp.links.talk = mk("Talk");
                        }
                        if (pp.historyElement.length && (!pp.historyElement.hasClass("selected"))) { //not on history page (but it exists)
                            pp.links.history = mk("History");
                        }
                        pp.links.logs = mk("Logs");
                    }
                } else { //on talk or talk history
                    if (!pp.articleElement.hasClass("new")) { //article exists
                        pp.links.article = mk(pp.articleElement.find("a").text());
                        pp.links.articleHistory = mk("Page history");
                    }
                    if (!pp.talkElement.hasClass("new")) { //talk exists
                        if (pp.historyElement.hasClass("selected")) { //on history page
                            pp.links.talk = mk("Talk");
                        } else {
                            pp.links.history = mk("Talk history");
                        }
                        pp.links.logs = mk("Talk logs");
                    }
                }

                pp.relevantUser = mw.config.get("wgRelevantUserName");
                if (pp.relevantUser) {
                    pp.relevantUser = mw.util.wikiUrlencode(pp.relevantUser);
                    pp.links.contribs = mk("Contribs");
                    pp.links.blocklog = mk("Block log");
                }

                div.append(document.createTextNode("]"));
            },
            click: function(e) {
                function msg(text) {
                    var c = $("#superlinks-content");
                    c.empty();
                    var div = document.createElement("div");
                    div.id="empty";
                    div.append(document.createTextNode(text));
                    c.append(div);
                }
                function loadcheck() {
                    var c = $("#superlinks-content");
                    if (c.children().length===0) msg("No results");
                    $(c).find("a[href]").attr('target', '_blank');
                }

                e.preventDefault();
                if (pp.active == e.target) {
                    pp.close();
                } else {
                    pp.close();
                    pp.active=e.target;
                    var wnd = document.createElement("div");
                    wnd.id="superlinks-window";
                    $("#superlinks").append(wnd);

                    var el = $(e.target)[0];
                    var pos = el.offsetParent.offsetWidth - el.offsetLeft - (el.offsetWidth/2);
                    pp.sstemp = mw.util.addCSS('#superlinks-window:before {right: ' + pos + 'px} #superlinks-window:after {right: ' + pos + 'px}');

                    var closeLink = document.createElement("a");
                    closeLink.id="superlinks-close";
                    closeLink.href="#";
                    closeLink.append(document.createTextNode("close"));
                    $(wnd).append(closeLink);
                    $(closeLink).click(pp.close);

                    var content = document.createElement("div");
                    content.id = "superlinks-content";
                    wnd.append(content);
                    msg("Loading...");

                    $(document).ajaxError(function() {msg("No results")});
                    var url='';
                    if (e.target == pp.links.article) {
                        url=pp.articleElement.find("a")[0].href.replace('/wiki/', '/w/index.php?action=render&title=');
                        $(content).load(url, loadcheck);
                    }
                    if (e.target == pp.links.articleHistory) {
                        url=pp.articleElement.find("a")[0].href.replace('/wiki/', '/w/index.php?action=history&safemode=1&title=');
                        $(content).load(url + " #pagehistory", loadcheck);
                    }
                    if (e.target == pp.links.talk) {
                        url=$("#ca-talk > span > a")[0].href.replace('/wiki/', '/w/index.php?action=render&title=');
                        $(content).load(url, loadcheck);
                    }
                    if (e.target == pp.links.history) {
                        url=$("#ca-history > span > a")[0].href+'&safemode=1';
                        $(content).load(url + " #pagehistory", loadcheck);
                    }
                    if (e.target == pp.links.logs) {
                        url='/w/index.php?title=Special:Log&page='+mw.config.get("wgPageName")+'&safemode=1';
                        $(content).load(url + " #mw-content-text > ul", loadcheck);
                    }
                    if (e.target == pp.links.contribs) {
                        url='/w/index.php?title=Special:Contributions/'+pp.relevantUser+'&safemode=1';
                        $(content).load(url + " #mw-content-text > ul ", loadcheck);
                    }
                    if (e.target == pp.links.blocklog) {
                        url='/w/index.php?title=Special:Log/block&page='+pp.relevantUser+'&safemode=1';
                        $(content).load(url + " #mw-content-text > ul", loadcheck);
                    }
                }
            },
            close: function(e) {
                if (e) e.preventDefault();
                if (pp.sstemp) {
                    $(pp.sstemp.ownerNode).remove();
                    pp.sstemp = null;
                }
                $("#superlinks-window").remove();
                pp.active=null;
            }
        };
        pp.init();
    }
});