Raphael
Raphael

Reputation: 1

How to include content-script files at different moments when developing a google chrome extension?

So, basically, I'm working on this simple extension and I'd like to know if there's any way to insert the content script files at different moments. When I say different moments, I mean at different "run_at" values. More specifically, I'd like to include one file at "document_start" and another one at "document_end".

The reason for that is that I'm writing a URL redirecting extension and, in some occasions, I could find out the wanted URL on the current URL, while on other cases I have to read the HTML code of the page to do it.

I've tried including all the files at "document_start", but jQuery seems not to work properly when I do that.

Upvotes: 0

Views: 143

Answers (1)

Rob W
Rob W

Reputation: 349112

Specify a different run_at value for each object at the content_scripts section of manifest.json:

"content_scripts": [{
    "js": [ "firstone.js" ],
    "matches": [ "http://matchthisurl/*" ],
    "run_at": "document_start"
},
{
    "js": [ "betweenfirstandlast.js" ],
    "matches": [ "http://matchthisurl/*" ],
    "run_at": "document_idle"
},
{
    "js": [ "at_end.js" ],
    "matches": [ "http://matchthisurl/*" ],
    "run_at": "document_end"
}]

When run_at is omitted, the code will run at document_idle, by default.

Upvotes: 1

Related Questions