Gaurang Tandon
Gaurang Tandon

Reputation: 6753

Content script not being injected in options page

manifest.json

"content_scripts": [{
    "js" : ["js/detector.js"],
    "matches": ["<all_urls>"],
    "all_frames": true,
    "run_at": "document_end"
}]

detector.js

(function () {
    "use strict";

    console.log("Injection");
    ....

I have this setup in my extension. The detector.js listens for certain keypresses and performs desired action. I want it to work on the options page too so that I can help out users. But it doesn't work there. I have the console.log at the very top of the file. But, I see no logs in the options page.

My question:
How do I get my content script to work on the options page?

UPDATE: Now I am in some trouble. If I include options.js file, that handles the options page, and detector.js file (the content script) together in options page like:

<script src="../js/options.js"></script>
<script src="../js/detector.js"></script>

then they both are interfering with the chrome.storage. I could of course put a check in the detector.js like if(!data_already_modified) then do this;but it is a kind of hack that I would put as my last resort. So, my question:

I want my content script to run in the options page, as it would on a normal webpage. Is there a better way than just to link the content script as a <script> element?

As simple as that.

UPDATE: Emrys commented, "the option page works as a normal webpage, including a script with works exactly as on a normal webpage". I cannot validate this claim, as ever since I asked this question, I managed to split my content script into two separate files - one for keypress manipulation, the other for data storage handling. That way I could include the former file into options.html through a <script> tag without any conflicts.

Upvotes: 1

Views: 258

Answers (1)

Emrys Myrooin
Emrys Myrooin

Reputation: 2231

You have to link the javascript file of your Content Script in your Option page HTML file like this :

<script src='detector.js'></script>

Upvotes: 3

Related Questions