Misplaced Pages

User:Alexis Jazz/SkinEnforcer.js

Article snapshot taken from[REDACTED] with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.
< User:Alexis Jazz

This is an old revision of this page, as edited by Alexis Jazz (talk | contribs) at 01:16, 9 October 2022 (Created page with '//SkinEnforcer (w:en:User:Alexis Jazz/SkinEnforcer) is a script to enforce a…' ). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Revision as of 01:16, 9 October 2022 by Alexis Jazz (talk | contribs) (Created page with '//SkinEnforcer (w:en:User:Alexis Jazz/SkinEnforcer) is a script to enforce a…' )(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump.
This code will be executed when previewing this page.
This user script seems to have a documentation page at User:Alexis Jazz/SkinEnforcer.
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 Misplaced Pages:Bypass your cache.
//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 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') ) {
		el = document.createElement('li');
		el.style = 'display:inline-block !important';
		el.id = 'SENF';
		el.innerHTML = '<a>'+SENF.m+'</a>';
		$('#footer-places').append(el);
	} else {
		SENF.el = mw.util.addPortletLink(
			'p-tb',
			'#',
			SENF.m,
			'SENF',
			SENF.m,
			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);
		SENF.mv = SENF.m;
		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') ) {
			$('#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'));} 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={};
			for(int=0;int<d.length;int++){
				ek=d.name;
				if ( ek == 'prefs-skin' || ek == 'skin-action-delete' || (ek.match(/^skinname\-/) && ek != 'skinname-nostalgia' && ek != 'skinname-wikimediaapiportal') ) {
					m = d;
				}
			}
			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)').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');
		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?:)?(\/\/|#)/) ) { //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(,function(){SENF.go();});
User:Alexis Jazz/SkinEnforcer.js Add topic