User:Aaron Liu/TemporaPaint.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump.
This code will be executed when previewing this page.
This code will be executed when previewing this page.
Documentation for this user script can be added at User:Aaron Liu/TemporaPaint.
// <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 } );
}
});
});
}() );