User:Rail/global.js

/** * Global scripts divided into modules * @author Rail * * Main modules: * - snippets      | Small code scippets run on various pages * - importDevCodes | Import codes from Dev Wiki * * Helper modules: * - conf            | Hold wiki meta configuration * - msg           | Simple i18n logic * - isDarkWiki    | Determine whther wiki is dark or light themed * - init          | Run other modules, hold scripts configuration */	'use strict';
 * ( function( mw, window ) {

// Prevent double loading if ( window['4F0A95F9A3AE15C99DBF32CA3FC0CBC8E1141B7D43DDF99FFE46EB6D76C0BBA8'] ) { return console.warn( '[UCP Global JS] Already loaded' ); }

const self = { conf: mw.config.get( [			'wgContentLanguage',			'wgUserLanguage',			'wgScriptPath',			'wgWikiaEnvironment',			'wgCityId',			'wgAction',			'wgCanonicalSpecialPageName',			'wgDBname',			'skin'		] ), locales: { en: { // English (en) 'summary-housekeeping': 'Housekeeping', 'summary-vandalism': 'Vandalism', 'summary-spam': 'Spam', 'summary-tou': 'Violation of Terms of Use', 'analytics': 'ANA', 'announcements': 'ANN', 'api-sandbox': 'API', 'interwiki-edit': 'IE' },			pl: { // polski (pl) 'summary-housekeeping': 'Porządki', 'summary-vandalism': 'Wandalizm', 'summary-spam': 'Spam', 'summary-tou': 'Naruszenie Zasad Użytkowania' }		},		msg: function( msg, lang ) { function getLanguage { var language;

if ( lang ) { language = ( lang === 'content'						? self.conf.wgContentLanguage						: lang					); } else { language = self.conf.wgUserLanguage; }

return language; }

/**			 * Replacement for `jQuery.fn.extend` method * https://stackoverflow.com/a/38777298 */			function extend( defaults, options ) { var prop, extended = {};

for ( prop in defaults ) { if ( Object.prototype.hasOwnProperty.call( defaults, prop ) ) { extended[prop] = defaults[prop]; }				}				for ( prop in options ) { if ( Object.prototype.hasOwnProperty.call( options, prop ) ) { extended[prop] = options[prop]; }				}

return extended; }

function getString { // Use English message as a fallback if noting else is available const string = extend(					self.locales.en,					self.locales[getLanguage]				);

return string[msg]; }

return getString; },		snippets: function { const userToolbar = document.querySelector( '#WikiaBarWrapper .tools' ); const trackers = document.querySelectorAll( '[data-tracking-label], [data-tracking]' ); const deleteReason = document.getElementById( 'wpReason' );

function onToolbarLoaded( callback ) { const interval = setInterval( function {					if ( !!userToolbar ) {						clearInterval( interval );						callback;					}				}, 500 ); }

function createToolLinks( tools ) { if ( !userToolbar ) return;

const toolsMenu = document.getElementById( 'my-tools-menu' );

tools.forEach( function( tool ) {					const element = document.createElement( 'li' );					const link = document.createElement( 'a' );

element.classList.add( 'overflow' ); element.setAttribute( 'id', tool.id );

link.setAttribute( 'href', tool.url ); link.innerText = self.msg( tool.id );

element.appendChild( link );

if ( tool.inDropdown === false ) { userToolbar.prepend( element ); } else { toolsMenu.appendChild( element ); }				} );			}

onToolbarLoaded( function {				createToolLinks( [ {						id: 'announcements', url: self.conf.wgScriptPath + '/announcements' },					{						id: 'analytics', url: mw.util.getUrl( 'Special:Analytics' ) },					{						id: 'api-sandbox', url: mw.util.getUrl( 'Special:ApiSandbox' ) },					{						id: 'interwiki-edit', url: 'https://community.fandom.com/wiki/Special:InterwikiEdit?action=Edit+wiki&wikia_id=' + self.conf.wgCityId, inDropdown: false }				] );

// Environment data and switch – used for UCP testing & stuff // (less of) Abomination if ( userToolbar && self.conf.skin === 'oasis' ) { var envInfoElement = document.createElement( 'li' );

envInfoElement.classList.add( 'overflow' ); envInfoElement.setAttribute( 'id', 'fandom-env-info' ); envInfoElement.innerHTML = 'ENV:  | ID:   | DB:  ';

userToolbar.appendChild( envInfoElement ); }			} );

if ( trackers ) trackers.forEach( function( tracker ) {				tracker.removeAttribute( 'data-tracking-label' );				tracker.removeAttribute( 'data-tracking' );			} );

if ( self.conf.wgAction === 'delete' && deleteReason ) { deleteReason.value = self.msg( 'summary-vandalism', 'content' ); }

// Quick theme changer mw.loader.using( 'mediawiki.api', function {				if ( self.conf.skin !== 'fandomdesktop' ) {					return;				}

function themeSwitch( theme ) { new mw.Api.saveOption( 'theme', theme ).done( function( d ) {						if ( d.options === 'success' ) {							location.reload;						}					} ); }

$( '.fandom-community-header__community-name-wrapper' ).append(					$( ' ', { class: 'quick-theme-switcher' } ).append( $( '', {							text: '(light)',							href: '#',							click: function {								themeSwitch( 'light' );							}						} ), ' ', $( '', {							text: '(dark)',							href: '#',							click: function {								themeSwitch( 'dark' );							}						} ), ' ', $( '', {							text: '(wiki)',							href: '#',							click: function {								themeSwitch( 'wiki' );							}						} ) )				);			} );		},		isDarkWiki: function {			// https://awik.io/determine-color-bright-dark-using-javascript/			function isDarkColor( color ) {				// Variables for red, green, blue values				var r, g, b, hsp;

// Check the format of the color, HEX or RGB? if ( color.match( /^rgb/ ) ) { // If RGB --> store the red, green, blue values in separate variables color = color.match( /^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+(?:\.\d+)?))?\)$/ );

r = color[1]; g = color[2]; b = color[3]; }				else { // If hex --> Convert it to RGB: http://gist.github.com/983661 color = + (						'0x' + color.slice(1).replace( color.length < 5 && /./g, '$&$&'						)					);

r = color >> 16; g = color >> 8 & 255; b = color & 255; }

// HSP (Highly Sensitive Poo) equation from http://alienryderflex.com/hsp.html hsp = Math.sqrt(					0.299 * ( r * r ) +					0.587 * ( g * g ) +					0.114 * ( b * b )				);

// Using the HSP value, determine whether the color is light or dark return ( hsp > 127.5					? false					: true				); }

function getBackgroundColor { const bcgSelector = document.getElementsByClassName( 'WikiaPageContentWrapper' )[0]; const bcgColor = getComputedStyle( bcgSelector ).getPropertyValue( 'background-color' );

return bcgColor; }

if ( self.conf.skin === 'fandomdesktop' ) { return document.body.classList.contains( 'theme-fandomdesktop-dark' ); }

return isDarkColor(				getBackgroundColor			); },		// Import Dev Wiki codes importDevCodes: function { function getCodePages { return [ 'MediaWiki:SyntaxHighlight.js', 'MediaWiki:FastDelete/code.js', 'MediaWiki:WHAM/code.2.js', 'MediaWiki:NullEditButton/code.js', 'MediaWiki:View_Source/code.js', 'MediaWiki:AjaxBatchDelete.js', 'MediaWiki:LastEdited/code.js', 'MediaWiki:CustomizeAce.js', 'MediaWiki:ProtectionIcons.js' ];			}

function concatPages { return getCodePages.join('|'); }

function getImportQuery { return { mode: 'articles', only: 'scripts', articles: concatPages };			}

function importCodes { const devLoad = 'https://dev.fandom.com/load.php'; const rlModules = [ 'mediawiki.Uri', 'ext.fandom.ContentReview.legacyLoaders.js' ];

mw.loader.using( rlModules, function {					const devUri = new mw.Uri( devLoad );

devUri.query = getImportQuery; mw.loader.load( devUri.toString ); } );			}

return importCodes; },		init: function { // WHAM window.WHAMDeleteReason = self.msg( 'summary-housekeeping', 'content' ); window.WHAMBlockReason = self.msg( 'summary-vandalism', 'content' );

// I don't like this thing – kill WikiActivity window.UCP = window.UCP || {}; window.UCP.WikiActivity = window.UCP.WikiActivity || {}; window.UCP.WikiActivity.loaded = true;

// UserAndIPTools window.UserAndIPTools = { enable: { MultiLookup: true, LookupUser: true },				openInNewPage: true };

// Custom Ace editor window.aceCustomSettings = { theme: ( self.isDarkWiki					? 'gruvbox'					: 'dawn'				), wrap: true, showInvisibles: true };

// FastDelete buttons window.fdButtons = [ {					summary: self.msg( 'summary-housekeeping', 'content' ), label: 'HK' },				{					summary: self.msg( 'summary-spam', 'content' ), label: 'S'				}, {					summary: self.msg( 'summary-vandalism', 'content' ), label: 'V'				}, {					summary: self.msg( 'summary-tou', 'content' ), label: 'ToU' }			];

// DotSyntaxHighlighter var syntaxHighlighterSiteConfig = syntaxHighlighterSiteConfig || {}; syntaxHighlighterSiteConfig.timeout = 150;

// AjaxBatchDelete window.batchDeleteDelay = 125;

// LastEdited window.lastEdited = { avatar: false };

// Kill WikiaNotification window.WikiaNotificationLoaded = true;

// Run modules self.importDevCodes; self.snippets;

// Import SOAP goodies mw.loader.load( 'https://internal-soap.fandom.com/wiki/User:Rail/scripts.js?action=raw&ctype=text/javascript' );

window['4F0A95F9A3AE15C99DBF32CA3FC0CBC8E1141B7D43DDF99FFE46EB6D76C0BBA8'] = true; }	};

self.init; } )( mediaWiki, this );