Created
July 28, 2009 00:01
-
-
Save cherenkov/156829 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| //前のコードだとbody直下のテキストノードがうまく動作しなかったです。 | |
| //とりあえず動いたというコードですが。 | |
| //参考 http://d.hatena.ne.jp/os0x/20081108/1226099859 | |
| // http://userscripts.org/scripts/show/54277 | |
| var txt = document.evaluate('descendant::text()[string-length(normalize-space(self::text())) > 0 and not(ancestor::textarea) and not(ancestor::script) and not(ancestor::style)]', | |
| document.body,null,7,null); | |
| for(var i=0; i<txt.snapshotLength; i++) { | |
| var newText = txt.snapshotItem(i).nodeValue.replace(/(twitter)/gi, '<span class="hoge">$1</span>'); | |
| var parent = txt.snapshotItem(i).parentNode; | |
| var range = document.createRange(); | |
| range.selectNode(txt.snapshotItem(i)); | |
| var df = range.createContextualFragment(newText); | |
| if (df.firstChild) parent.replaceChild(df, txt.snapshotItem(i)); | |
| range.detach(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment