Last active
April 9, 2017 23:18
-
-
Save lodi-g/28dc959df42cfc39551cac8adb764e3b to your computer and use it in GitHub Desktop.
Change hex codes by setting its associated color as background color
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==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