Skip to content

Instantly share code, notes, and snippets.

@cherenkov
Created July 28, 2009 00:01
Show Gist options
  • Select an option

  • Save cherenkov/156829 to your computer and use it in GitHub Desktop.

Select an option

Save cherenkov/156829 to your computer and use it in GitHub Desktop.
//前のコードだと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