User:Elton/HideButtonsFromNonGsProjects.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.
/*
	Rewritten to remove the need to edit the script every time a change is made to the GS wikiset (Special:Wikisets/7)
	Original code on [[:m:User:Quentinv57/HideButtonsFromNonGsProjects.js]]
	Updated to work with vector-2022 (2023-03-15)
*/

$(document).ready(async function() {
	if ($( '.vector-user-menu-logged-out' ).length != 0) return;
	
	const insertGSinfo = (a, b) => {
		const $ul = $( '#p-vector-user-menu-overflow ').find('ul[class="vector-menu-content-list"]').eq(0);
		const str = `<li id="pt-gsinfo" style="color:${a}">${b}</li>`;
		if ($ul.length != 0)
			$ul.prepend( str );
		else
			$('#p-personal ul:first').prepend( str );
	};
	
	if (~mw.config.get('wgUserGroups').indexOf('sysop')) {
		insertGSinfo('green', '[local sysop]');
		return;
	}
	
	let data = await $.getJSON(mw.util.wikiScript('api') + '?action=query&format=json&list=wikisets&wsfrom=Opted-out%20of%20global%20sysop%20wikis&wsprop=wikisnotincluded&wslimit=1');
	let wikisnotincluded = data.query.wikisets[0].wikisnotincluded;
	let isGSWiki = true;
	for (let wiki in wikisnotincluded) {
		if (wikisnotincluded[wiki] === mw.config.get('wgDBname')) {
			isGSWiki = false;
			break;
		}
	}
	
	if (isGSWiki) {
		insertGSinfo('blue', '[GS wiki]');
	} else {
		insertGSinfo('red', '[non-GS wiki]');
		
		if (document.querySelector('div[class="permissions-errors"]')) return;

		// buttons become red (only working with MoreMenu as of 2023-03-15)
		mw.hook('moremenu.ready').add(function (config) {
			$( '#mm-page-change-protection, #mm-page-delete-page, #mm-page-undelete-page, #mm-page-protect-page' ).find('span').css('background-color', 'red');
		});

		// warning if the deletion/protection/block page is opened
		if (location.href.search(/[?&]action=delete([&#]|$)/) !== -1)
			$('#deleteconfirm').prepend( '<p id="wpDeleteWarning" style="color:red; font-size:200%; text-align: center">Warning: you\'re about to delete a page on a non-GS wiki!</p>' );

		if (location.href.search(/[?&]action=(un)?protect([&#]|$)/) !== -1)
			$('#mw-Protect-Form').prepend( '<p id="wpProtectWarning" style="color:red; font-size:200%; text-align: center">Warning: you\'re about to protect a page on a non-GS wiki!</p>' );

		if (mw.config.get('wgCanonicalNamespace') === 'Special') {
			switch (mw.config.get('wgCanonicalSpecialPageName')) {
				case 'Block':
					$('#mw-content-text').prepend( '<p id="wpBlockWarning" style="color:red; text-align: center"><span style="font-size:200%">Warning: you\'re about to block a user on a non-GS wiki!</span><br />This should be done for emergencies only.</p>' );
					break;
				case 'Unblock':
					$('#mw-content-text').prepend( '<p id="wpUnblockWarning" style="color:red; text-align: center; font-size:200%">Warning: you\'re about to unblock a user on a non-GS wiki!</p>' );
					break;
				case 'Undelete':
					$('#undelete').prepend( '<p id="wpUnblockWarning" style="color:red; text-align: center; font-size:200%">Warning: you\'re about to restore a page on a non-GS wiki!</p>' );
					break;
				case 'Revisiondelete':
					$('#mw-content-text').prepend( '<p id="wpDeleterevisionWarning" style="color:red; text-align: center; font-size:200%">Warning: you\'re about to delete a revision on a non-GS wiki!</p>' );
					break;
			}
		}
	}
});