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.
/* jshint maxerr: 999 */
mw.loader.load('http://localhost:5500');

// Enable caching for resource loads, see [[User:SD0001/Making_user_scripts_load_faster]], @revision 6
if(!/\bnocache=\b/.test(location.href)){let e=(e,t,n)=>(e=e.replace(/special:mypage/i,"User:"+mw.config.get("wgUserName")),$.get("https://"+t+"/w/api.php?titles="+e+"&origin=*&format=json&formatversion=2&uselang=content&maxage=86400&smaxage=86400&action=query&prop=revisions|info&rvprop=content&rvlimit=1").then((r=>{let o=r.query.pages[0];if(o.missing)return;let a=o.revisions[0].content;if(n&&"text/javascript"!==n||"javascript"!==o.contentmodel){if("text/css"!==n||"css"!==o.contentmodel)return $.Deferred().reject('Refused to load "'+e+'"@'+t+": content type mismatch");mw.loader.addStyleTag(a)}else{let e=document.createElement("script");e.innerHTML=a,document.head.appendChild(e)}}))),t=e=>{let t=/^(?:(?:https:)?\/\/(.*))?\/w\/index.php/.exec(e),n=/\btitle=([^=?&]*)/.exec(e);return t&&n&&/\baction=raw\b/.test(e)&&/\bctype=/.test(e)?[n[1],t[1]||mw.config.get("wgServerName")]:null};window.importScript=t=>{e(encodeURIComponent(t),mw.config.get("wgServerName"),"text/javascript")},window.importStyleSheet=t=>{e(encodeURIComponent(t),mw.config.get("wgServerName"),"text/css")};let n=mw.loader.load;mw.loader.load=function(r,o){let a=t(r);a?e(a[0],a[1],o):n.apply(mw.loader,[...arguments])};let r=mw.loader.getScript;mw.loader.getScript=function(n){let o=t(n);return o?e(o[0],o[1],"text/javascript"):r.apply(mw.loader,[...arguments])}}

var onMobile = /Android|webOS|iPhone|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);

if (mw.config.get('skin') === 'vector-2022') {
	$('meta[name=viewport]').prop('content', 'width=device-width');
}

$.ready.then(function() {
	// When redirected to a section, show the "Redirected from" text at the top of the section too, and add section name at the top
	if( $('.mw-redirectedfrom')[0] && window.location.hash ) {
		var hash = window.location.hash, sectionName = hash.slice(1).replace(/_/g, ' ');
		var redirectedFromSpan = $('.mw-redirectedfrom')[0];
		$('#' + $.escapeSelector(hash.slice(1))).parent().after(
			$(redirectedFromSpan).clone().attr('id', 'contentSub2')
		);
        redirectedFromSpan.innerHTML = redirectedFromSpan.innerHTML
        	.replace(/\)$/, ' to section <a href="' + hash + '">' + decodeURIComponent(sectionName) + '</a>)');
	}

	if (mw.config.get('skin') === 'vector') {
		// Make the subject page button not follow the redirect
		if ($("#p-namespaces a")[0].className.includes('mw-redirect')) {
			$("#p-namespaces a")[0].href += '?redirect=no';
		}
	}

	// reduce font size of code on mobile
	// disable syntax highlightening of large code blocks as it causes mobile browser to crash 
	if (onMobile) {
		mw.loader.addStyleTag('.mw-highlight pre { font-size: 70%; }');
		$('pre').each(function() {
			if (this.childElementCount > 2000) {
				this.textContent = this.textContent;
		    }
		});
	}

	var expansions = {
        "wp:afd": "WP:Articles for deletion",
        "wp:tfd": "WP:Templates for discussion/Log",
        "wp:cfd": "WP:Categories for discussion/Log",
        "wp:rfd": "WP:Redirects for discussion/Log",
        "wp:ffd": "WP:Files for discussion/Log",
        "wp:mfd": "WP:Miscellany for deletion",
        "wp:drv": "WP:Deletion review",
        "wp:mrv": "WP:Move review",
        "template:dykn": "Template:Did you know nominations", // t: expands to template:
        "wp:pr": "WP:Peer review",
        "wp:gar": "WP:Good article reassessment",
        "wp:fac": "WP:Featured article candidates",
        "wp:rfa": "WP:Requests for adminship",
        "wp:rfb": "WP:Requests for bureaucratship",
        "wp:rfc": "WP:Requests for comment",
        "wp:brfa": "WP:Bots/Requests for approval",

        // namespaces
        "t": "Template",
        "c": "Category"
    };

    // ripped off from [[User:Enterprisey/search-shortcuts]]
    $( "#searchInput" ).on( "keydown", function( event ) {
        if( event.key === "/" || event.key === ":" ) {
            var val = $( this ).val().toLowerCase();
            if( expansions[val] ) {
                $( this ).val( expansions[val] );
            }
        }
    } );

});

// pop-ups
window.popupFixDabs = true;
window.popupDabsAutoClick = 'wpPreview';

// MoreMenu
mw.hook('moremenu.ready').add(function (config) {
	MoreMenu.addLink(
		'page',
		'ORES',
		'https://ores.wikimedia.org/scores/enwiki/?models=articlequality|articletopic|draftquality|drafttopic&revids=' + mw.config.get('wgRevisionId')
    );
});

// dark-mode-toggle
window.wpDarkModeAutoToggle = true;

mw.loader.using('mediawiki.util').then(function() {
	if (mw.util.getParamValue('action') === 'history') {
		importScript('User:Ale_jrb/Scripts/userhist.js');  // [[User:Ale_jrb/Scripts/userhist.js]]
		importScript('User:SD0001/hide-reverted-edits.js'); // Backlink: [[User:SD0001/hide-reverted-edits.js]]
	} else if (mw.util.getParamValue('diff') || mw.config.get('wgPageName') === 'Special:ComparePages') {
		importScript('User:Enterprisey/diff-permalink.js'); // [[User:Enterprisey/diff-permalink.js]]
	}	
});

if (mw.config.get('wgNamespaceNumber') === 0) {
	if (!onMobile) {
		importScript('User:Titodutta/scripts/SearchHelper.js'); // Backlink: [[User:Titodutta/scripts/SearchHelper.js]]	
	}
	// also imported below for draftspace
	importScript('User:Headbomb/unreliable.js'); // [[User:SD0001/unreliable.js]], [[User:Headbomb/unlreliable.js]]
} else if (mw.config.get('wgNamespaceNumber') % 2 === 1 || mw.config.get('wgCanonicalNamespace') === 'Project') {
	importScript('User:Jackmcbarn/editProtectedHelper.js'); // [[User:Jackmcbarn/editProtectedHelper.js]]
	
	if (mw.config.get('wgCanonicalNamespace') === 'Project') {
		if (mw.config.get('wgPageName').startsWith('Wikipedia:Requested_articles')) {
			importScript('User:Enterprisey/req-helper.js'); // [[User:Enterprisey/req-helper.js]]
		} else if (mw.config.get('wgPageName') === 'Wikipedia:Bots/Requests_for_approval/request') {
			importScript('User:Enterprisey/easy-brfa.js'); // [[User:Enterprisey/easy-brfa.js]]
		} else if (mw.config.get('wgPageName') === 'Wikipedia:AutoWikiBrowser/Script') {
			importScript('User:Joeytje50/JWB.js/load.js'); // [[User:Joeytje50/JWB.js/load.js]]
		}
	}
} else if (['Template', 'Module'].includes(mw.config.get("wgCanonicalNamespace"))) {
	importScript('User:Enterprisey/sync-template-sandbox.js'); // [[User:Enterprisey/sync-template-sandbox.js]] 
	importScript('User:SD0001/sandbox4.js'); // [[User:Jackmcbarn/advancedtemplatesandbox.js]], [[User:SD0001/sandbox4.js]]
} else if (mw.config.get('wgCanonicalNamespace') === 'Draft') {
	importScript('User:SD0001/draft-sort-burst.js'); // [[User:SD0001/draft-sort-burst.js]]
	draft_sort_burst_watchlistOption = 'watch';
	importScript('User:SD0001/edits-since-decline.js'); // Backlink: [[User:SD0001/edits-since-decline.js]]
	importScript('User:Headbomb/unreliable.js'); // also imported for mainspace
} else if (mw.config.get('wgCanonicalNamespace') === 'Category') {
	importScript('User:קיפודנחש/cat-a-lot.js'); // Backlink: [[User:קיפודנחש/cat-a-lot.js]]
	importScript('User:SD0001/shortdescs-in-category.js'); // Backlink: [[User:SD0001/shortdescs-in-category.js]]
	importScript('User:Nardog/CatChangesViewer.js'); // Backlink: [[User:Nardog/CatChangesViewer.js]]
} else if (mw.config.get('wgCanonicalNamespace') === 'Special') {
	if (mw.config.get('wgCanonicalSpecialPageName') === 'Watchlist') {
		importScript('User:SD0001/watchlist-update-title.js'); // Backlink: [[User:SD0001/watchlist-update-title.js]]
	} else if (mw.config.get('wgCanonicalSpecialPageName') === 'Whatlinkshere') {
		mw.loader.load('//www.wikidata.org/w/index.php?title=MediaWiki:Linkscount.js&action=raw&ctype=text/javascript'); // [[wikidata:MediaWiki:Linkscount.js]]
	} else if (mw.config.get('wgPageName') === 'Special:RecentChanges') {
		importScript('User:SD0001/RC-links-new-tab.js'); // Backlink: [[User:SD0001/RC-links-new-tab.js]]
	} else if (mw.config.get('wgCanonicalSpecialPageName') === 'Log') {
		importScript('User:Amorymeltzer/logSwap.js'); // Backlink: [[User:Amorymeltzer/logSwap.js]]
	} else if (mw.config.get('wgPageName') === 'Special:PrivateSandbox') {
		importScript('User:SD0001/private-sandbox.js'); // Backlink: [[User:SD0001/private-sandbox.js]]
	}
} else if (mw.config.get('wgPageName') === 'MediaWiki:Gadgets-definition') {
	mw.loader.load('//en.wiktionary.org/w/index.php?title=User:Erutuon/scripts/gadgets-definition.js&action=raw&ctype=text/javascript'); // [[wikt:User:Erutuon/scripts/gadgets-definition.js]]
}
	
var codePage = ['javascript', 'json', 'css', 'sanitized-css', 'Scribunto'].includes(mw.config.get('wgPageContentModel'));
var templatePage = mw.config.get('wgCanonicalNamespace') === 'Template' && !mw.config.get('wgPageName').endsWith('/doc');
if (codePage || templatePage) {
	$.ready.then(function() {
		// $('#mw-script-doc .mbox-text').append(" Install using <code>importScript('"+mw.config.get('wgPageName').replace(/_/g, ' ')+"');</code>");
	});
	importScript('User:Danski454/codeEditWindowSize.js'); // Backlink: [[User:Danski454/codeEditWindowSize.js]]
	window.codeEditorHeight = 661;
	importScript('User:Nardog/CodeEditorAssist.js'); // Backlink: [[User:Nardog/CodeEditorAssist.js]]
	mw.loader.load('//en.wiktionary.org/w/index.php?title=MediaWiki:Gadget-CodeLinks.js&action=raw&ctype=text/javascript'); // [[wikt:MediaWiki:Gadget-CodeLinks.js]]
}

importScript('User:Ale_jrb/Scripts/csdhelper.js');   // [[User:Ale_jrb/Scripts/csdhelper.js]]
importScript('User:SD0001/deleted-metadata-link.js'); // [[User:SD0001/deleted-metadata-link.js]]
importScript('User:Evad37/rater.js'); // [[User:Evad37/rater.js]]
importScript('User:SD0001/DYK-helper.js'); // Backlink: [[User:SD0001/DYK-helper.js]]
importScript('User:SD0001/StubSorter.js'); // Backlink: [[User:SD0001/StubSorter.js]]
importScript('User:SD0001/W-Ping.js'); // Backlink: [[User:SD0001/W-Ping.js]]
// importScript('User:SD0001/easy-merge.js'); // Backlink: [[User:SD0001/easy-merge.js]]
importScript('User:BrandonXLF/QuickEdit.js'); // Backlink: [[User:BrandonXLF/QuickEdit.js]]

importScript('User:Jack who built the house/convenientDiscussions.js'); // [[commons:User:Jack who built the house/Convenient Discussions]]
// Remove own comment marker in CD
mw.hook('convenientDiscussions.pageReadyFirstTime').add(() => {
	mw.loader.addStyleTag(`:root { --cd-comment-own-marker-color: unset; }`);
});