Jump to content

User:BDavis (WMF)/global.js: Difference between revisions

From Meta, a Wikimedia project coordination wiki
Content deleted Content added
Tag: Manual revert
Tag: Reverted
Line 98: Line 98:
mw.loader.load( 'https://en.wikipedia.org/w/index.php?title=User:BrandonXLF/WatchDoc.js&action=raw&ctype=text/javascript' ); // Backlink: [[w:en:User:BrandonXLF/WatchDoc.js]]
mw.loader.load( 'https://en.wikipedia.org/w/index.php?title=User:BrandonXLF/WatchDoc.js&action=raw&ctype=text/javascript' ); // Backlink: [[w:en:User:BrandonXLF/WatchDoc.js]]
mw.loader.load( 'https://en.wikipedia.org/w/index.php?title=User:Taavi/Unslashifier.js&action=raw&ctype=text/javascript' ); // Backlink: [[w:en:User:Taavi/Unslashifier.js]]
mw.loader.load( 'https://en.wikipedia.org/w/index.php?title=User:Taavi/Unslashifier.js&action=raw&ctype=text/javascript' ); // Backlink: [[w:en:User:Taavi/Unslashifier.js]]
mw.loader.load( 'https://en.wikipedia.org/w/index.php?title=User:Andrybak/Scripts/Unsigned_helper.js&action=raw&ctype=text/javascript' ); // Backlink: [[w:en:User:Andrybak/Scripts/Unsigned helper.js]]

Revision as of 17:01, 1 November 2024

// Enable caching for resource loads, see [[w:en:User:SD0001/Making_user_scripts_load_faster]], @revision 6
if (!/\bnocache=\b/.test(location.href)) { // Don't enable if nocache=1 url parameter is given
	let loadResource = (page, sitename, ctype) => {
		page = page.replace(/special:mypage/i, 'User:' + mw.config.get('wgUserName'));
		return $.get(
			'https://' + sitename + '/w/api.php?titles=' + page + // page is already URL-encoded
			'&origin=*&format=json&formatversion=2&uselang=content&maxage=86400&smaxage=86400' + 
			'&action=query&prop=revisions|info&rvprop=content&rvlimit=1'
		).then((apiResponse) => {
			let apiPage = apiResponse.query.pages[0];
			if (apiPage.missing) {
				return;
			}
			let content = apiPage.revisions[0].content;
			if ((!ctype || ctype === 'text/javascript') && apiPage.contentmodel === 'javascript') {
				let scriptTag = document.createElement('script');
				scriptTag.innerHTML = content;
				document.head.appendChild(scriptTag);
		    } else if (ctype === 'text/css' && apiPage.contentmodel === 'css') {
				mw.loader.addStyleTag(content);
			} else {
				return $.Deferred().reject('Refused to load "' + page + '"@' + sitename + ': content type mismatch');
			}
		});
	};
	let getSiteTitle = (url) => {
		let siteRgx = /^(?:(?:https:)?\/\/(.*))?\/w\/index.php/.exec(url),
			titleRgx = /\btitle=([^=?&]*)/.exec(url);
		if (siteRgx && titleRgx && /\baction=raw\b/.test(url) && /\bctype=/.test(url)) {
			return [titleRgx[1], siteRgx[1] || mw.config.get('wgServerName')];	
		} else return null;
	};
	window.importScript = (page) => {
		loadResource(encodeURIComponent(page), mw.config.get('wgServerName'), 'text/javascript');
	};
	window.importStyleSheet = (page) => {
		loadResource(encodeURIComponent(page), mw.config.get('wgServerName'), 'text/css');
	};
	let oldMwLoaderLoad = mw.loader.load;
	mw.loader.load = function(url, type) {
		let linkParts = getSiteTitle(url);
		if (linkParts) {
			loadResource(linkParts[0], linkParts[1], type);
		} else {
			oldMwLoaderLoad.apply(mw.loader, [...arguments]);
		}
	};
	let oldMwLoaderGetScript = mw.loader.getScript;
	mw.loader.getScript = function(url) {
		let linkParts = getSiteTitle(url);
		if (linkParts) {
			return loadResource(linkParts[0], linkParts[1], 'text/javascript');
		} else {
			return oldMwLoaderGetScript.apply(mw.loader, [...arguments]);
		}
	};
}

var ns = mw.config.get("wgNamespaceNumber"),
	action = mw.config.get('wgAction'),
	special = mw.config.get('wgCanonicalSpecialPageName'),
	title = mw.config.get('wgTitle');
if (action === 'edit') {
	// Syntax highlighter gadget from mediawiki.org
	mw.loader.load( 'https://www.mediawiki.org/w/index.php?title=MediaWiki:Gadget-DotsSyntaxHighlighter.js&action=raw&ctype=text/javascript' ); // Backlink: [[mw:MediaWiki:Gadget-DotsSyntaxHighlighter.js]]
	mw.loader.load( 'https://en.wikipedia.org/w/index.php?title=User:Nardog/CodeEditorAssist.js&action=raw&ctype=text/javascript' ); // Backlink: [[w:en:User:Nardog/CodeEditorAssist.js]]
}
if (ns == 2 || ns == 3) {
	// User info display on user and user_talk pages
	mw.loader.load( 'https://en.wikipedia.org/w/index.php?title=User:BDavis_(WMF)/userinfo.js&action=raw&ctype=text/javascript' ); // Backlink: [[w:en:User:BDavis (WMF)/userinfo.js]]
}
if (special === 'Blankpage') {
	if (title.indexOf('/globalwatchlistreset') > 2) {
		mw.loader.load( 'https://meta.wikimedia.org/w/index.php?title=User:BDavis_(WMF)/Scripts/GlobalWatchlistReset.js&action=raw&ctype=text/javascript' ); // Backlink: [[m:User:BDavis_(WMF)/Scripts/GlobalWatchlistReset.js]]
	} else if (title.indexOf('/RTRC') > 2) {
		mw.loader.load( 'https://meta.wikimedia.org/w/index.php?title=User:Krinkle/RTRC.js&action=raw&ctype=text/javascript' ); // Backlink: [[m:User:Krinkle/RTRC.js]]
	} else if (title.indexOf('/globalsul') > 2) {
		// [[File:Krinkle_Global_SUL.js]]
		mw.loader.load( 'https://meta.wikimedia.org/w/index.php?title=User:Krinkle/Tools/Global_SUL.js&action=raw&ctype=text/javascript' ); // Backlink: [[m:User:Krinkle/Tools/Global_SUL.js]]
	} else {
		$content = $('#bodyContent').empty().append(
			'<ul>' +
			'<li><a href="./Special:BlankPage/RTRC">Real-time recent changes</a></li>' +
			'<li><a href="./Special:BlankPage/globalwatchlistreset">Global watchlist reset</a></li>' +
			'<li><a href="./Special:BlankPage/globalsul">Globalize SUL</a></li>' +
			'</ul>'
		);
	}
}
mw.loader.load( 'https://en.wikipedia.org/w/index.php?title=User:TheDJ/responsiveContent.css&action=raw&ctype=text/css', 'text/css'); // Backlink: [[w:en:User:TheDJ/responsiveContent.css]]
mw.loader.load( 'https://meta.wikimedia.org/w/index.php?title=User:BDavis_(WMF)/Scripts/zonestamp.js&action=raw&ctype=text/javascript' ); // Backlink: [[m:User:BDavis (WMF)/Scripts/zonestamp.js]]
mw.loader.load( 'https://meta.wikimedia.org/w/index.php?action=raw&ctype=text/javascript&title=MediaWiki:MoreMenu.import.js' ); // Backlink: [[m:MediaWiki:MoreMenu.import.j]]
mw.loader.load( 'https://meta.wikimedia.org/w/index.php?title=User:ESanders_(WMF)/commentlinks.js&action=raw&ctype=text/javascript' ); // Backlink: [[m:User:ESanders (WMF)/commentlinks.js]]
mw.loader.load( 'https://meta.wikimedia.org/w/index.php?title=User:Krinkle/Scripts/Perf.js&action=raw&ctype=text/javascript' ); // Backlink: [[m:User:Krinkle/Scripts/Perf.js]]
mw.loader.load( 'https://meta.wikimedia.org/w/index.php?title=User:Tks4Fish/CAWhoisProxy.js&action=raw&ctype=text/javascript' ); // Backlink: [[m:User:Tks4Fish/CAWhoisProxy.js]]
mw.loader.load( 'https://en.wikipedia.org/w/index.php?title=User:Enterprisey/diff-permalink-2.js&action=raw&ctype=text/javascript' ); // Backlink: [[w:en:User:Enterprisey/diff-permalink-2.js]]
mw.loader.load( 'https://en.wikipedia.org/w/index.php?title=User:Anomie/previewtemplatelastmod.js&action=raw&ctype=text/javascript' ); // Backlink: [[w:en:User:Anomie/previewtemplatelastmod.js]]
mw.loader.load( 'https://en.wikipedia.org/w/index.php?title=User:BrandonXLF/WatchDoc.js&action=raw&ctype=text/javascript' ); // Backlink: [[w:en:User:BrandonXLF/WatchDoc.js]]
mw.loader.load( 'https://en.wikipedia.org/w/index.php?title=User:Taavi/Unslashifier.js&action=raw&ctype=text/javascript' ); // Backlink: [[w:en:User:Taavi/Unslashifier.js]]
mw.loader.load( 'https://en.wikipedia.org/w/index.php?title=User:Andrybak/Scripts/Unsigned_helper.js&action=raw&ctype=text/javascript' ); // Backlink: [[w:en:User:Andrybak/Scripts/Unsigned helper.js]]