Jump to content

User:Alexis Jazz/SkinEnforcer.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.
//SkinEnforcer ([[w:en:User:Alexis Jazz/SkinEnforcer]]) is a script to enforce a skin when you're not logged in. Or on the mobile site. (use MobileFrontend vectors like collapsible sections on Vector classic, yay!)
//SkinEnforcer is public domain, irrevocably released as WTFPL Version 2[www.wtfpl.net/about/] by its author, Alexis Jazz.
//<nowiki>
/*globals $:false,mw:false*/
window.SENF = {};
var SENF = window.SENF;
SENF.go = function(UL){
console.log('SkinEnforcer: ALPHA JULIETT WE\'RE GOING IN');
UL=mw.config.get('wgUserLanguage');
SENF.addMenu = function(int,el) {
	SENF.LM();
	$('#SENF').remove();
	if ( mw.config.get('skin') == 'minerva' && !$('#page-actions-overflow-toggle')[0] ) {
		el = document.createElement('li');
		el.style = 'display:inline-block !important';
		el.id = 'SENF';
		el.innerHTML = '<a>'+SENF.m['prefs-skin']+'</a>';
		$('#footer-places').append(el);
	} else {
		SENF.el = mw.util.addPortletLink(
			'p-tb',
			'#',
			SENF.m['prefs-skin'],
			'SENF',
			SENF.m['prefs-skin'],
			undefined,
			'#t-specialpages'
		);
	}
	SENF.menu = document.createElement('ul');
	SENF.menu.id = 'SENFmenu';
	SENF.menu.classList.add('SENFhidden');
	for(int=0;int<Object.keys(SENF.m).length;int++){
		SENF.mk = Object.keys(SENF.m)[int];
		SENF.mv = SENF.m[SENF.mk];
		if ( SENF.mk != 'prefs-skin' ) {
			SENF.menulinks = (SENF.menulinks||'') + '<li><a id="SENF-'+SENF.mk+'" class="SENF">'+SENF.mv+'</a></li>';
		}
	}
	SENF.menu.innerHTML = SENF.menulinks;
	$('#SENF').append(SENF.menu);
	$('#SENF a:eq(0)').on('click',function(event){
		event.preventDefault();event.stopPropagation();
		if ( $('#SENFmenu.SENFhidden')[0] ) {
			$('#SENFmenu').removeClass('SENFhidden');
			$('#SENFmenu').scrollIntoView();
		} else {
			$('#SENFmenu').addClass('SENFhidden');
		}
	});
	$('.SENF').on('click',function(event,s){
		event.preventDefault();
		s = event.delegateTarget.id.slice(5);
		if ( s != mw.config.get('skin') ) {
			SENF.skin = s;
			if ( s == 'skin-action-delete' ) {
				console.log('SkinEnforcer: ALPHA JULIETT WE LOST A MAN');
				mw.storage.remove('SENF');
				return;
			}
			mw.storage.set('SENF',s);
			SENF.enforce(null,window.location.href);
		} else {
			$('#SENFmenu').removeClass('SENFhidden');
		}		
	});
	mw.util.addCSS('body.skin-minerva #SENFmenu>li{display:inline-block}.SENFhidden{display:none}#SENFmenu{font-size:inherit}body:not(.skin-monobook) #SENFmenu>li{cursor:pointer;margin-left:0.5em;margin-right:0.5em}body.skin-vector #SENFmenu>li,body.skin-vector-2022 #SENFmenu>li{font-size:larger}body.skin-minerva #SENFmenu>li{margin-top:1em}');
};
SENF.enforce = function(event,uri) {
	uri = new mw.Uri((uri||event.delegateTarget.href));
	uri.extend({useskin:SENF.skin});
	console.log('SkinEnforcer: ALPHA JULIETT ATTACHED '+SENF.skin+' TO TARGET, TARGET SECURE, RETURN TO BASE');
	window.location = uri.toString();
};
SENF.msgsEN = {'prefs-skin':'Skin',vector:'Vector','vector-2022':'Vector (2022)',minerva:'MinervaNeue',monobook:'Monobook',timeless:'Timeless',modern:'Modern',cologneblue:'Cologne Blue','skin-action-delete':'Delete'};
SENF.LM = function() {
	try{SENF.m = JSON.parse(mw.storage.get('SENFLang'))[UL];} catch (e) {}
	SENF.m = ( SENF.m || SENF.msgsEN );
};
SENF.getLang = function() {
	SENF.LM();
	if ( SENF.m.date && SENF.m.date < (new Date().getTime()-604800000) ) { //(7*24*3600*1000) cached lang >1 week old
		mw.storage.remove('SENFLang');
	}
	if ( UL != 'en' && !mw.storage.get('SENFLang') ) {
		var api = new mw.Api();
		api.get( { format: 'json', action: 'query', meta: 'allmessages', amfilter:'skin', amlang:UL} ).done( function ( data,int,k,d,ek,m,msg ) {
			d=data.query.allmessages;
			m={};
			m[UL]={};
			for(int=0;int<d.length;int++){
				ek=d[int].name;
				if ( ek == 'prefs-skin' || ek == 'skin-action-delete' || (ek.match(/^skinname\-/) && ek != 'skinname-nostalgia' && ek != 'skinname-wikimediaapiportal') ) {
					m[UL][ek.replace('skinname-','')] = d[int]['*'];
				}
			}
			mw.storage.set('SENFLang',JSON.stringify(m));
			SENF.addMenu();
		});
	} else {
		SENF.addMenu();
	}
};
SENF.getLang();
SENF.skin = (mw.util.getParamValue('useskin') || (mw.storage.get('SENF') && mw.storage.get('SENF') != mw.config.get('skin') && mw.storage.get('SENF')));
if ( SENF.skin ) {
	console.log('SkinEnforcer: ALPHA JULIETT REQUEST PERMISSION TO ENFORCE '+SENF.skin);
	if ( mw.config.get('skin') != SENF.skin && !mw.util.getParamValue('useskin') ) { //you have a skin set in localStorage but it doesn't match your current skin
		console.log('SkinEnforcer: ALPHA JULIETT DO YOU COPY WE HAVE A HOSTAGE SITUATION');
		SENF.enforce(null,window.location.href);
	}
	$('a:not(.skin-minerva a#ca-edit,.eventonly)').on('click',function(event,ignoreEl,copyEl){
		copyEl = document.createElement('span');
		copyEl.append(event.delegateTarget.cloneNode());
		ignoreEl = copyEl.querySelectorAll('.oo-ui-buttonElement-button,.mw-ui-button,#user-notifications,.minerva-talk-add-button,a#ca-watch,a#page-actions-overflow-toggle,a.language-selector')[0];
		if ( ignoreEl ) {
			console.log('SkinEnforcer: ALPHA JULIETT FALSE ALARM, LINK IS EXPECTED TO PREVENTDEFAULT');
			return;
		}
		if ( event.delegateTarget.href != '' && !event.delegateTarget.attributes.href.nodeValue.match(/^(http[s]?:)?(\/\/|#)/) && !event.delegateTarget.attributes.href.nodeValue.match(/veaction=/) ) { //href-less link generally just triggers some event, external links and anchors on the current page should be left alone
			event.preventDefault();event.stopPropagation();
			console.log('SkinEnforcer: ALPHA JULIETT WE HAVE A VISUAL ON THE TARGET');
			SENF.enforce(event);
		}
	});
}
};//SENF.go
mw.loader.using(['mediawiki.util','mediawiki.storage','mediawiki.api'],function(){SENF.go();});