Skip to content

Instantly share code, notes, and snippets.

@lodi-g
Last active April 9, 2017 23:18
Show Gist options
  • Save lodi-g/28dc959df42cfc39551cac8adb764e3b to your computer and use it in GitHub Desktop.
Save lodi-g/28dc959df42cfc39551cac8adb764e3b to your computer and use it in GitHub Desktop.
Change hex codes by setting its associated color as background color
// ==UserScript==
// @name hex to bg color
// @namespace lodi-g
// @version 0.1
// @description Change hex codes by setting its associated color as background color
// @author lodi-g
// @match *://*/*
// @require https://raw.githubusercontent.com/padolsey/findAndReplaceDOMText/master/src/findAndReplaceDOMText.js
// @grant none
// ==/UserScript==
function getColorByBgColor(bgColor) {
return (parseInt(bgColor.replace('#', ''), 16) > 0xffffff / 2) ? '#000' : '#fff';
}
findAndReplaceDOMText(document.querySelector('body'), {
find: /(#[0-9a-f]{3,6})/ig,
replace: function(portion, match) {
var el = document.createElement("span");
el.style.backgroundColor = match[0];
el.style.color = getColorByBgColor(match[0]);
el.innerHTML = match[0];
return el;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment