MediaWiki:Gadget-PortletLinks.js

From Zelda Wiki, the Zelda encyclopedia
Revision as of 09:45, 13 August 2017 by KokoroSenshi (talk | contribs) (added a fix to Frozen Animations - gotta use !important to override the "display:block;" of "li.gallerybox div.thumb img")
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.
//// --------------------------------------------------------
//// Portlet Links
//// This script adds additional/custom links to the sidebar, 
//// and additional/custom tabs at the top-right next to the Search Box.
//// https://www.mediawiki.org/wiki/ResourceLoader/Core_modules#addPortletLink
//// https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.util-method-addPortletLink
//// --------------------------------------------------------


var action = mw.config.get("wgAction"),
    server = mw.config.get("wgServer"),
    pageName = mw.config.get("wgPageName"),
    canonicalNamespace = mw.config.get("wgCanonicalNamespace");

// --------------------------------------------------------
// last diff
// Adds a tab which gives the latest diff for a page.
// --------------------------------------------------------
if ( canonicalNamespace != 'Special' 
     && action != 'edit' 
     && action != 'delete' 
     && action != 'watch' 
     && action != 'unwatch' 
     && action != 'protect' ){
	
	$(document).ready(function(){
		
		mw.util.addPortletLink(
			"p-cactions",            //Tab (Action)
			server + "/index.php?title=" + encodeURIComponent(pageName) + "&diff=cur&oldid=prev", 
			"last",                  //Name of Tab
			"ca-last",               //id
			"Show most recent diff", //Tooltip
			'2'                      //accesskey
		);
		
	});
	
}

// --------------------------------------------------------
// Highlight Redirects
// Adds a tab to the top of pages; when clicked it highlights all links on the page that are redirects.
// Notes: https://www.mediawiki.org/wiki/ResourceLoader/Core_modules#addCSS
// --------------------------------------------------------
if ( canonicalNamespace != 'Special' 
     && action != 'history' 
     && action != 'delete' 
     && action != 'watch' 
     && action != 'unwatch' 
     && action != 'protect' 
     && action != 'markpatrolled' 
     && action != 'rollback' ){
	
	$(document).ready( function(){
		
		var highlightRedirectsCss = mw.util.addCSS(
			'a.mw-redirect { color:black; background-color:yellow; }' +
			'a.mw-redirect:visited { color:dimgrey; background-color:yellow; }'
		); 
		highlightRedirectsCss.disabled = true;
		
		var portletLink = mw.util.addPortletLink(
			'p-cactions', '#',	'redirects', 'ca-redirects',
			'Highlights all links which are redirects', 'r'
		);
		$( portletLink ).click( function(e){
			e.preventDefault();
			highlightRedirectsCss.disabled = !highlightRedirectsCss.disabled;
		});
		
	});
	
}

// --------------------------------------------------
// Page logs 
// Because they're not just for admins :P
// Adds a link in the Tools section of the sidebar.
// --------------------------------------------------
if (canonicalNamespace != "Special") {
	
	$(document).ready(function(){
		mediaWiki.util.addPortletLink("p-tb", 
			"http://zeldawiki.org/index.php?title=Special%3ALog&type=&user=&page=" + pageName, 
			"Page Logs", 
			"t-logs", 
			"Show all relevant logs for this page.", 
			'3'
		);
	});
	
}

// --------------------------------------------------
// Restyle the Purge tab from the current "Refresh"
// --------------------------------------------------

$(document).ready(function(){
	
	$("#ca-purge a")
		.attr("title","Purge the server cache of this page [alt-shift-0]")
		.attr("accesskey","0")
		.text("Purge");
		
});

// --------------------------------------------------------
// Frozen Animations
// Replaces images with the first frame
// Uses html5 canvas https://caniuse.com/#search=canvas
// Notes: https://www.mediawiki.org/wiki/ResourceLoader/Core_modules#addCSS
// --------------------------------------------------------
if ( canonicalNamespace != 'Special' 
     && action != 'history' 
     && action != 'delete' 
     && action != 'watch' 
     && action != 'unwatch' 
     && action != 'protect' 
     && action != 'markpatrolled' 
     && action != 'rollback' ){
	
	$(document).ready( function(){
		$("a.image").each(function(){
			var $this = $(this)
			  , img = $this.children()[0]
			  , $cvs = $('<canvas/>').appendTo($this)
			  , ctx = $cvs[0].getContext('2d');
			//$this.addClass('frozen-animations');
			$cvs.attr('width', img.naturalWidth);
			$cvs.attr('height',img.naturalHeight);
			ctx.drawImage(img,0,0);
			$cvs.css('width', img.clientWidth);
			$cvs.css('height',img.clientHeight);
		});
		//var frozenAnimationsCss   = mw.util.addCSS('a.frozen-animations > img { display: none; }')
		//  , unfrozenAnimationsCss = mw.util.addCSS('a.frozen-animations > canvas { display: none; }'); 
		var frozenAnimationsCss   = mw.util.addCSS('a.image > img { display: none !important;}')
		  , unfrozenAnimationsCss = mw.util.addCSS('a.image > canvas { display: none; }'); 
		frozenAnimationsCss.disabled = true;
		
		var portletLink = mw.util.addPortletLink(
			'p-cactions', '#',	'frozen', 'ca-frozen',
			'Replaces animations with a still of the first frame', 'f'
		);
		$( portletLink ).click( function(e){
			e.preventDefault();
			frozenAnimationsCss.disabled = !frozenAnimationsCss.disabled;
			unfrozenAnimationsCss.disabled = !unfrozenAnimationsCss.disabled;
		});
		
	});
	
}