User:Edgars2007/google.js

// Guarantee that our dependencies are loaded mw.loader.using( [ 'mediawiki.api', 'mediawiki.util' ], function { var api = new mw.Api( { ajax: { // Use a user agent, so that sysadmins can find you and tell you to fix your tool headers: { 'Api-User-Agent': 'MyAPITool/1.0' } } } );

api.get( {     // Use the new output structure of the api      formatversion: 2,      prop: 'langlinks',         lllang: 'de',      titles: mw.config.get( 'wgPageName' ),      redirects: true    } ).done( function( data ) {      if ( !data.query.pages[0].langlinks.length ) {         // no german langlink for this page         return;      }      var title = data.query.pages[0].langlinks[0].title;      // Strip parenthesis      title = title.replace( /\s\(.*\)/, '' );

// Add when page is ready $( function {       mw.util.addPortletLink( 'p-tb', 'https://www.google.com/search?q=' + encodeURIComponent( title ), 'google' );     } );

} ); // add .fail promise if you want to handle errors } );