User:Enterprisey/quick-before.js

( 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.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", "JSTOR", "NYT", "TWL" ];

/**

* encodeURIComponent, but actually fixed.

* Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent#Description

*/

function fixedEncodeURIComponent( str ) {

return encodeURIComponent( str ).replace( /[!'( )*_]/g, function( c ) {

return '%' + c.charCodeAt( 0 ).toString( 16 );

} );

}

/**

* 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] + fixedEncodeURIComponent( pageName.replace( /_/g, " " ) ) + url[1] ) : url;

link.textContent = label;

link.target = "_blank";

link.className = "external text";

link.setAttribute( "rel", "nofollow noopener noreferrer" );

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 = "Find sources: ";

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 }"

);

// Add the panel itself

var panel = document.createElement( "div" );

panel.id = "before-panel";

panel.appendChild( buildSearchDiv() );

panel.innerHTML += "
";

document.getElementById( "bodyContent" ).insertBefore( panel,

document.getElementById( "mw-content-text" ) );

// Close handler

document.getElementById( "before-close" ).addEventListener( "click", function () {

$( "#before-panel" ).remove();

} );

}

$.when( $.ready, mw.loader.using( [ "mediawiki.util" ] ) ).then( function () {

pageName = mw.config.get( "wgTitle" );

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

if( link ) link.addEventListener( "click", load );

} );

} )();