🇮🇷 Iran Proxy | https://www.wikipedia.org/wiki/User:Aaron_Liu/TemporaPaint.js
Jump to content

User:Aaron Liu/TemporaPaint.js

From Wikipedia, the free encyclopedia
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.
// <section begin=version />0.2.1<section end=version />
/* eslint-disable no-bitwise */
if (typeof get32BitHashOfString === "undefined")
/**
 * Return a unique 32-bit number for every unique string input.
 * @copyright https://stackoverflow.com/a/52171480/16134571
 * @param {string} str The string to hash
 * @returns {number} A 32-bit hash.
 */
	var get32BitHashOfString = function( str ) {
		let h = 9;
		for ( let i = 0; i < str.length; ) {
			h = Math.imul( h ^ str.charCodeAt( i++ ), 9 ** 9 );
		}
		return h ^ h >>> 9;
	};
if (typeof getColorOfString === "undefined")
/**
 * Returns a unique foreground and background color for each string.
 * @copyright https://stackoverflow.com/a/16348977
 * @param {string} str The input string
 * @returns {[string, string]} CSS syntax representing colors to fill [color, backgroundColor]
 */
	var getColorOfString = function( str ) {
		const hash = get32BitHashOfString( str );
		let colour = '#', colourInv = '#';
		for ( let i = 0; i < 3; i++ ) {
			const value = ( hash >> ( i * 8 ) ) & 0xff;
			colour += value.toString( 16 ).padStart( 2, '0' );
			colourInv += ( 0xFF ^ value ).toString( 16 ).padStart( 2, '0' );
		}
		return [ colour, colourInv ];
	};
/* eslint-enable no-bitwise */
( function () {
	mw.hook( 'wikipage.content' ).add( ( $content ) => {
		const $target = $content.find( '.mw-tempuserlink' );
		$target.each((i, element) => {
			const account = element.getAttribute( 'data-mw-target' );
			if (account != null) {
				const [ colour, bgColour ] = getColorOfString(account);
				// fix minerva by setting style on the child <bdi>
				// (minerva has a style that targets that element and overrides background-color)
				$( element ).find( 'bdi' ).css( { color: colour, backgroundColor: bgColour } );
			}
		});
	});
}() );