Last active
December 11, 2015 03:19
-
-
Save kawanet/4536949 to your computer and use it in GitHub Desktop.
Xmas - Feel Christmas on every website
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
var walker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT); | |
var buf = []; | |
while(walker.nextNode()) { | |
var src = walker.currentNode; | |
if (! src) continue; | |
var text = src.data; | |
if (! text.length) continue; | |
text = text.replace(/[\s\r\n\t]+/g, ''); | |
if (text.length < 1) continue; | |
if (! src.parentNode) continue; | |
var tag = src.parentNode.tagName; | |
if (tag.toUpperCase() == 'SCRIPT') continue; | |
if (tag.toUpperCase() == 'STYLE') continue; | |
var cls = src.parentNode.getAttribute('class'); | |
if (cls == 'xmas-red') continue; | |
if (cls == 'xmas-green') continue; | |
buf.push(src); | |
} | |
var cnt = 0; | |
var loop; | |
loop = function () { | |
for(var j=0; j<10; j++) { | |
var src = buf.shift(); | |
if (! src) return; | |
var fg = document.createDocumentFragment(); | |
var list = src.data.split(''); | |
var len = list.length; | |
for(var i=0; i<len; i++){ | |
var chr = list[i]; | |
if (chr.match(/[\s\r\n\t]/)) { | |
var space = document.createTextNode(chr); | |
fg.appendChild(space); | |
} else { | |
var tn = document.createTextNode(chr); | |
var font = document.createElement('font'); | |
cls = (cnt ++ % 2) ? 'xmas-red' : 'xmas-green'; | |
font.setAttribute('class', cls); | |
font.appendChild(tn); | |
fg.appendChild(font); | |
} | |
} | |
src.parentNode.replaceChild(fg, src); | |
} | |
setTimeout(loop, 1); | |
}; | |
var rule = "font.xmas-red, font.xmas-green { font-weight: bold; display: inline !important; margin: 0 !important; padding: 0 !important; border: none !important; background: none !important; }\n"; | |
rule += "li { font-weight: bold; }\n"; | |
rule += "font.xmas-red, li:nth-child(even) { color: #F00000 !important; }\n"; | |
rule += "font.xmas-green, li:nth-child(odd) { color: #008000 !important; }\n"; | |
var style = document.createElement('style'); | |
var sn = document.createTextNode(rule); | |
style.appendChild(sn); | |
document.body.appendChild(style); | |
setTimeout(loop, 1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
2012-12-22
Xmas クリスマス気分になれるブックマークレット
http://kawanet.hatenablog.com/entry/20121222/1356188018