MediaWiki:TranscludedIn.js

From Wikisource
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/* SCRIPT THATS ADDS ONE OR MORE TABS, IN NAMESPACE PAGE, THAT POINT TO THE TEXTS THAT TRASCLUDE THAT PAGE
 *
 * Instructions to install in your Wikisource subdomain:
 *
 * in your MediaWiki:Common.js, add (and translate in your language) the following lines:
 
mw.loader.load('//wikisource.org/w/index.php?title=MediaWiki:TranscludedIn.js&action=raw&ctype=text/javascript');
*/

function api_embeddedin(eititle,einamespace) {
	eititle = eititle || mw.config.get('wgPageName');
	einamespace = einamespace || '0';
	var api_url=["/w/api.php?action=query",
             "format=json",
             "list=embeddedin",
             "einamespace="+einamespace,
             "eititle="+eititle].join("&");
	var result=JSON.parse($.ajax({url:api_url,async:false}).responseText);
	return result.query.embeddedin;
}
 
function addNamespaceLink( html, url, id, title ) {
	const pId = $( '#p-namespaces' ).length ? 'p-namespaces' : 'p-associated-pages';
	const $html = $( html );
 	const link = mw.util.addPortletLink(pId, url, '', id, title );
 	if ( link ) {
 		$( link ).find( 'a' ).append( $html );
 	}
}
function embeddedin_run() {
  if (mw.config.get('wgCanonicalNamespace')=="Page") {
    var linkedNs0=api_embeddedin();
    for (var i=0;i<linkedNs0.length;i+=1) {   
       let html = '<img src="//upload.wikimedia.org/wikipedia/commons/thumb/9/92/Open_book_nae_02.svg/25px-Open_book_nae_02.svg.png" width="25" height="14">';
       if (linkedNs0.length > 1) html += ' <small>'+(i+1)+'</small>';
       addNamespaceLink(html, mw.util.getUrl( linkedNs0[i].title ), 'ca-ns0_'+i, linkedNs0[i].title );
    }
  }
}

$(document).ready(embeddedin_run);