User:Unicodesnowman/T64266Prototype.js

From mediawiki.org

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.
// MultimediaViewer T64266 Prototype D
// This script is licensed under GPL v2 or any later version. Parts of this script
// are from the MultimediaViewer project licensed under GPLv2 or any later version.
// See https://www.mediawiki.org/wiki/Extension:MultimediaViewer

mw.loader.using( ['mmv.bootstrap.autostart'], function () {

console.log('MultimediaViewer prototype loaded.');

var MMVB = mw.mmv.bootstrap;

// replace MMVB.internalHashChange
MMVB.internalHashChange = function ( e ) {
	var hash = e.hash,
		title = e.title;
	
	// The advantage of using pushState when it's available is that it has to ability to truly 
	// clear the hash, not leaving "#" in the history                                          
	// An entry with "#" in the history has the side-effect of resetting the scroll position when navigating the history
	if ( this.browserHistory && this.browserHistory.pushState ) {
		// In order to truly clear the hash, we need to reconstruct the hash-free URL
		var cleared = false;
		if ( hash === '#' ) {
			cleared = true;
			hash = window.location.href.replace( /#.*$/, '' );
		}
		
		// Only add history entry when entering the lightbox (option D, T64266)
		if ( e.target.URL.indexOf( '#mediaviewer/' ) !== -1 && !cleared ) {
			this.browserHistory.replaceState( null, title, hash );
		} else {
			this.browserHistory.pushState( null, title, hash );
		}
	} else {
		// Since we voluntarily changed the hash, we don't want MMVB.hash (which will trigger on hashchange event) to treat it
		this.skipNextHashHandling = true;
		
		window.location.hash = hash;
	}
	
	document.title = title;
};

} );