apirogov
apirogov

Reputation: 1316

Is there a way to run a Google Chrome content script after the DOM is loaded but before any page scripts are executed?

I need to remove a script from a page before it is executed, because its getting in the way of my script...

If I set run_at to document_start, I have no access to the DOM, because it is not loaded. But when it is loaded, the scripts on the page are also executed...

Is there a way to tell Chrome to run my content script before running any scripts from the page?

I understand that Javascript is executed just in the moment the code is being loaded... but maybe there is a way to disable this behaviour when using the content script?

Upvotes: 4

Views: 977

Answers (2)

Zachary K
Zachary K

Reputation: 3335

I think you are much better off having a server side solution that removes that file from the template in the case that you don't want it. Anything done in Javascript is going to be very iffy and hard to get right (if it can be done at all)

Upvotes: 0

Caio
Caio

Reputation: 3215

Just load the DOM content for every script you have and set the order you want to be executed:

<script> // First
    document.addEventListener ("DOMContentLoaded", function () {}, false)
</script>

<script> // Second
    document.addEventListener ("DOMContentLoaded", function () {}, false)
</script>

<script> // Third
    document.addEventListener ("DOMContentLoaded", function () {}, false)
</script>

...

Remember that for external scripts the "async" method probably won´t be executed in the order specified.

Upvotes: 1

Related Questions