Skip to content

Instantly share code, notes, and snippets.

@niklasl
Created January 2, 2016 19:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niklasl/3b17ea557840fba92e44 to your computer and use it in GitHub Desktop.
Save niklasl/3b17ea557840fba92e44 to your computer and use it in GitHub Desktop.
/**
* To level up your synesthesia, choose colors, apply on web pages and read
* some 100 000 words. Purportedly. :)
* http://www.theatlantic.com/technology/archive/2012/07/can-you-teach-yourself-synesthesia/259519/
*/
var charColorMap = {
a: 'darkred',
e: 'green',
s: 'blue',
t: 'brown'
};
function replaceTextNode(tnode) {
var el = tnode.parentElement;
var span = document.createElement('span');
span.innerHTML = tnode.nodeValue.replace(
/[aest]/gi, (c) => '<span style="color: '+ charColorMap[c.toLowerCase()] + '">'+ c +'</span>');
el.replaceChild(span, tnode);
}
function walkTree(el) {
for (var i=0, l=el.childNodes; i < l.length; i++) {
var child = l[i];
if (child.nodeType === Node.ELEMENT_NODE) {
walkTree(child);
} else if (child.nodeType === Node.TEXT_NODE) {
replaceTextNode(child);
}
}
}
walkTree(document.body);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment