User:Suffusion of Yellow/central.js

From Meta, a Wikimedia project coordination wiki

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.
/*
 * Hacky script to allow central login even without third party cookies.
 * Works by opening about a dozen tabs, making the cookies "first party".
 */
$(function() {
	mw.util.addPortletLink(window.centralLoginPortletId || 'p-tb',
						   '/wiki/Special:BlankPage/CentralLogin',
						   'Central login',
						   'ca-centrallogin',
						   'Login to most WMF wikis (no third party cookies needed');
});

if (mw.config.get('wgPageName') == "Special:BlankPage/CentralLogin")
	$.when($.ready, mw.loader.using(['mediawiki.util','oojs-ui'])).then(function() {
		let lang = mw.config.get("wgContentLanguage");

		let wikis = window.centralLoginWikis || [
			lang + "wiki",
			lang + "wikisource",
			lang + "wiktionary",
			lang + "wikibooks",
			lang + "wikiversity",
			lang + "wikiquote",
			lang + "wikinews",
			lang + "wikivoyage",
			"metawiki",
			"commonswiki",
			"wikidatawiki",
			"mediawikiwiki",
			"specieswiki"
		];

		// If the script ran at all, we are already logged in here
		wikis = wikis.filter(wiki => wiki != mw.config.get("wgDBname"));

		document.title = "Central login (no thirdy party cookies)";

		$("#firstHeading").text("Central login (no thirdy party cookies)");

		$("#mw-content-text").html("<div>Click on the 'Open' button below to automatically login to most WMF wikis, even if your browser is blocking third-party cookies. <b>" + wikis.length + " new tabs or windows containing raw JavaScript will be opened. </b> <p> Once all the pages have <i>finished</i> loading, you can click on the 'Close' button. <p> Problem? Try disabling your browser's popup blocker on this site. </div>");

		let openButton = new OO.ui.ButtonWidget({
			label: "Open",
			title: "Open " + wikis.length + " new tabs or windows and login to most WMF wikis"
		});

		let closeButton = new OO.ui.ButtonWidget({
			label: "Close",
			title: "Close all the tabs or windows (do not click until all pages have finished loading)",
			disabled: true
		});

		$("#mw-content-text").append(openButton.$element, closeButton.$element);

		let windows = [];

		openButton.on('click', function() {
			// Here be dragons
			for (let wiki of wikis)
				windows.push(window.open("https://login.wikimedia.org/wiki/Special:CentralAutoLogin/checkLoggedIn?type=script&proto=https&wikiid=" + wiki));

			closeButton.setDisabled(false);
		});

		closeButton.on('click', function() {
			for (let win of windows)
				win.close();

			windows = [];

			closeButton.setDisabled(true);
		});
	});