If you open a text file (.txt, .js, .css, …) in your browser, it will get wrapped up in a nice DOM tree.
For example, open this .txt file and enter
javascript:alert(document.documentElement.innerHTML);
into your address bar. Nice… every major browser supports DOM manipulation on this wrapped text files, which is a great thing for writing powerful bookmarklets or user scripts.
However, Firefox fails on assigning any element’s innerHTML. For example,
javascript: document.body.innerHTML = document.body.innerHTML.replace(/(\d+\s+\w+(?=\s+\d+))/g, '<span style="color:red">$1</span>'); void 0;
will work in every browser but Firefox.
Is there a trick to work around this issue?
(No, I don’t want to parse the innerHTML string manually, and no, it doesn’t work with jQuery either.)
I think I found a working solution. First of all, let me give some more details on the question.
The problem is: Firefox creates something like
but the wrapped document object does not support setting innerHTML properly. So, the basic idea is, create a new document object with full innerHTML support. Here’s how it works:
edit:
This version is better and faster; using XSLTProcessor instead of iFrame.