0

I want to modify HTML code before DOM rendering. I have tried to use ajax as below:

window.stop();
var xhr = new XMLHttpRequest();
xhr.open('GET', window.location.href, true);
xhr.onload = function () {
    var htmlcode = this.responseText;

    ... modify htmlcode ...

    document.open();
    document.write(htmlcode);
    document.close();
}
xhr.send();

But it doesn't look very good when I inject it into "http://www.google.com". how can I do that using a chrome extension?

zzm
  • 1
  • The only improvement I can think of is to start XHR earlier in chrome.webNavigation.onBeforeNavigation event listener (in the background/event page), then send the result into the content script using messaging. – wOxxOm Nov 08 '17 at 05:32
  • Also, make sure to use `"run_at": "document_start"` for your content script in manifest.json – wOxxOm Nov 08 '17 at 05:34
  • See https://stackoverflow.com/questions/18310484/modify-http-responses-from-a-chrome-extension – Maluen Nov 10 '17 at 10:47

0 Answers0