If you use
document.write to write an entire new document,
Chrome might apply a security-policy, warning or even blocking every internal-script you have.
As an alternative, only use
document.write to write the
and set the HTML-source using
document.documentElement.innerHTML = ".......".
Here is a working code you, that should be loaded after the document has ready but before it rendered (a.k.a
at_document_end if you are doing this in Chrome’s extension).