Jump to content

User:HueSatLum/findSources.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.
/**
 * Adds a portal to the sidebar of the page with customizable links for finding sources
 * 
 * @author HueSatLum
 * @version 1.1
 */

/* To install, add the following line to your common.js:

importScript('User:HueSatLum/findSources.js') // Linkback: [[User:HueSatLum/findSources.js]]

See the documentation at [[User:HueSatLum/findSources]] for costumization instructions. */

window.FindSources = window.FindSources || {};
if ( FindSources.searchEngines === undefined ) {
  FindSources.searchEngines = {
    'bing': {
      name: 'Bing',
      url: 'http://www.bing.com/search?q=$1'
    },
    'duckduckgo': {
      name: 'DuckDuckGo',
      url: 'https://duckduckgo.com/?q=$1&ia=about'
    },
    'google': {
      name: 'Google',
      url: 'https://www.google.com/search?as_eq=wikipedia&q=$1'
    },
    'googlebooks': {
      name: 'Google Books',
      url: 'https://www.google.com/search?tbm=bks&q=$1'
    },
    'googleimages': {
      name: 'Free images',
      url: 'https://www.google.com/search?safe=off&tbm=isch&tbs=sur:fmc&as_sitesearch=wikimedia.org&as_dt=e&q=$1+-site:wikipedia.org'
    },
    'googlenews': {
      name: 'Google News',
      url: 'https://www.google.com/search?tbm=nws&q=$1'
    },
    'googlenewspapers': {
      name: 'Newspapers',
      url: 'https://www.google.com/search?as_sitesearch=news.google.com/newspapers&q=$1'
    },
    'googlescholar': {
      name: 'Scholar',
      url: 'https://scholar.google.com/scholar?q=$1'
    },
    'highbeam': {
      name: 'HighBeam',
      url: 'http://www.highbeam.com/Search?searchTerm=$1'
    },
    'jstor': {
      name: 'JSTOR',
      url: 'http://www.jstor.org/action/doBasicSearch?Query=$1'
    },
    'nytimes': {
      name: 'NYTimes',
      url: 'http://query.nytimes.com/search/sitesearch/#/$1/'
    }
  };
}
if ( FindSources.order === undefined ) {
  FindSources.order = [ 'google', 'googlebooks', 'googlenews', 'googlenewspapers', 'nytimes', 'googleimages' ];
}

( function ( $, mw ) {
  if ( [ 0, 1, 118, 118 ].indexOf( mw.config.get( 'wgNamespaceNumber' ) ) === -1 && !mw.config.get( 'wgIsSearchResultPage' ) ) {
    // Only run on article and draft namespaces and their talk pages and search pages
    return;
  }
  var searchTitle = mw.config.get( 'wgTitle' );
  if ( mw.config.get( 'wgCanonicalSpecialPageName' ) === 'Search' ) {
    searchTitle = mw.util.getParamValue( 'search' );
  }
  
  searchTitle = searchTitle.toLowerCase()
    .replace( /\((.+?)\)$/, '$1' );
  
  var $portalClone = $( '#p-tb' ).clone();
  $portalClone.attr( 'id', 'p-findsources' );
  $portalClone.find( 'h3' )
    .text( 'Find sources' )
    .attr( 'id', function ( _ind, value ) {
      if ( value ) {
        var id = 'p-findsources-label';
        $portalClone.attr( 'aria-labelledby', id );
        return id;
      }
    } );
  $portalClone.find( 'ul' ).empty();
  $portalClone.insertAfter( '#p-tb' );

  FindSources.order.forEach( function ( engine ) {
    var engineData = FindSources.searchEngines[ engine ];
    if ( !engineData ) {
      return console.info( 'findSources.js: \'' + engine + '\' was skipped because its data was not found.' );
    }
    if ( typeof engineData.url !== 'string' ) {
      return console.info( 'findSources.js: \'' + engine + '\' was skipped because its URL was malformed or not found.' );
    }

    var url = engineData.url.replace( '$1', encodeURIComponent( searchTitle ) );
    
    var engineLink = mw.util.addPortletLink(
      'p-findsources',
      url,
      engineData.name || engine,
      'fs-' + engine
    );
    $( engineLink ).find( 'a' ).attr( 'target', '_blank' );
  } );
} ) ( jQuery, mediaWiki );