Chrome Extension - Get Html DOM before loading js in browser

I am developing a chrome extension that needs to block the html page from loading, do some javascript checks that link to the page in my content script, and continue (or not) with the page load.

In my manifest with "run_at": "document_start", the content script gets empty html and cannot validate. With run_at in document_end, it alredy executed the js that goes into the page and right after that my extension does it validate ...

Is there a way to set both DOMContentBeforeLoad in my content script or something? I really have no options.

thank

+3


source to share


2 answers


Take a look at how TopLevel.js works: https://github.com/kristopolous/TopLevel (interesting source at https://github.com/kristopolous/TopLevel/blob/master/toplevel.js )

It is a library that you explicitly list on your page. When reached on the page and immediately triggers document.write () a <plaintext> element with style = 'display: none', which immediately stops the browser from viewing the rest of the page and hides the plain text result (plain text is the edited element. which stops interpreting the content of the page, and treats all HTML as vanilla unparsed plain text: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/plaintext ).



Then Toplevel parses the text content of the <plaintext> (and makes some templates that are the point of the library), and document.write () retrieves the new content into the page manually.

You should be able to do something like this: insert the text <plaintext> to stop the page being parsed by the browser, parse it yourself (or do whatever you want), then potentially write whatever you like (including the original content) on the page when you are happy.

+1


source


I think to do what you do, you have to do what you did with document_start and then load the html page via an ajax call and parse it yourself.



Browsers usually don't load all scripts and then execute, this happens asynchronously in the order of the page, so it doesn't make sense that you can catch it where the javascript will be loaded, but nothing (except when you also control the page content ).

0


source







All Articles