Jump to content

User:Enterprisey/quick-before.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Enterprisey (talk | contribs) at 06:42, 15 November 2018 (fix). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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 () {
    var pageName;

    var urls = [
        ["//www.google.com/search?as_eq=wikipedia&q=%22", "%22&num=50" ],
        [ "//www.google.com/search?tbs=bks:1&q=%22", "%22+-wikipedia" ],
        [ "//www.google.com/search?tbm=nws&q=%22", "%22+-wikipedia" ],
        [ "//www.google.com/search?&q=%22", "%22+site:news.google.com/newspapers&source=newspapers" ],
        [ "//scholar.google.com/scholar?q=%22", "%22" ],
        [ "https://www.google.com/search?safe=off&tbs=sur:fmc&tbm=isch&q=%22", "%22+-site:wikipedia.org+-site:wikimedia.org" ],
        [ "https://www.google.com/custom?hl=en&cx=007734830908295939403%3Agalkqgoksq0&cof=FORID%3A13%3BAH%3Aleft%3BCX%3AWikipedia%2520Reference%2520Search&q=%22", "%22" ],
        "//en.wikipedia.org/wiki/Wikipedia:Free_English_newspaper_sources",
        [ "https://www.highbeam.com/Search?searchTerm=%22", "%22" ],
        [ "https://www.jstor.org/action/doBasicSearch?Query=%22", "%22&acc=on&wc=on" ],
        [ "https://www.nytimes.com/search/%22", "%22" ],
        "https://wikipedialibrary.wmflabs.org/partners/"
    ];
    var labels = [ "Google", "books", "news", "newspapers", "scholar", "free images", "WP refs", "FENS", "HighBeam", "JSTOR", "NYT", "TWl" ];

    /**
     * Adds a link to the given container. Takes either a [prefix, suffix] array
     * or a single static URL.
     */
    function addLinkTo( container, label, url ) {
        var link = document.createElement( "a" );
        link.href = ( typeof url === typeof [] ) ? ( url[0] + encodeURIComponent( pageName ) + url[1] ) : url;
        link.textContent = label;
        link.className = "external text";
        link.setAttribute( "rel", "nofollow" );
        container.appendChild( link );
    }

    function addSeparatorTo( container ) {
        container.appendChild( document.createTextNode( "\u00A0" ) );
        var sep = document.createElement( "b" );
        sep.textContent = "·";
        container.appendChild( sep );
    }

    function buildSearchDiv() {
        var container = document.createElement( "span" );
        container.className = "plainlinks";
        container.innerHTML = "<i>Find sources:</i>&nbsp;";
        addLinkTo( container, labels[0], urls[0] );
        container.appendChild( document.createTextNode( " (" ) );
        for( var i = 1; i < 6; i++ ) {
            addLinkTo( container, labels[i], urls[i] );
            addSeparatorTo( container );
        }
        addLinkTo( container, labels[6], urls[6] );
        container.appendChild( document.createTextNode( ")" ) );
        for( var i = 7; i < 12; i++ ) {
            addSeparatorTo( container );
            addLinkTo( container, labels[i], urls[i] );
        }
        return container;
    }

    /**
     * Load the main cv-revdel panel and add buttons/other stuff to
     * the history UI
     */
    function load( evt ) {
        if( evt ) evt.preventDefault();

        // Don't load the panel for a second time if it's already there
        if( document.getElementById( "before-panel" ) ) return;

        // Style for the panel
        mw.util.addCSS(
            "#before-panel { border: thin solid rgb(197, 197, 197); " +
            "box-shadow: 0px 3px 8px rgba(0, 0, 0, 0.25); border-radius: 3px;" +
            "padding: 2em; display: inline-block }" +
            "#before-close {position: absolute; top: 35px; right: 5px;}" +

            // Low-budget mw-ui
            "#cv-revdel #cv-rd-submit { color: #fff;background-color: #36c;border-color: #36c;" +
            "transition: background-color 100ms,color 100ms,border-color 100ms,box-shadow 100ms;" +
            "padding: 0.625em 0.94em 0.55em; border-style: solid;border-width: 1px;border-radius: 2px;" +
            "cursor: pointer; font-weight: bold; font-family: sans-serif; }" +
            "#cv-rd-submit:hover {background-color: #447ff5;border-color: #447ff5;}" +
            "#cv-rd-submit:active {color: #fff;background-color: #2a4b8d;border-color: #2a4b8d;box-shadow: none;}" +
            "#cv-rd-submit:focus {border-color: #36c;box-shadow: inset 0 0 0 1px #36c,inset 0 0 0 2px #fff;outline: 0;}"
        );

        // Add the panel itself
        var panel = document.createElement( "div" );
        panel.id = "before-panel";
        panel.appendChild( buildSearchDiv() );
        panel.innerHTML += "<button id='before-close'>Close</button>";
        document.getElementById( "bodyContent" ).insertBefore( panel,
            document.getElementById( "mw-content-text" ) );


        // Close handler
        document.getElementById( "before-close" ).addEventListener( "click", function () {
            $( "#before-panel" ).remove();
        } );
    }

    mw.loader.using( [ "mediawiki.util", "mediawiki.ui.input" ], function () {
        pageName = mw.config.get( "wgPageName" );

        var link = mw.util.addPortletLink( "p-cactions", "#", "BEFORE search", "pt-before" );
        link.addEventListener( "click", load );
    } );
} );