Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Content Script is not loading #95

Open
doutatsu opened this issue Sep 6, 2020 · 6 comments
Open

Content Script is not loading #95

doutatsu opened this issue Sep 6, 2020 · 6 comments

Comments

@doutatsu
Copy link

doutatsu commented Sep 6, 2020

I am trying to use a content script but to no avail. I've used the boilerplate setup and I can see it being compiled, but no code gets executed in the content script. 

I have also used alert and console.log in the background script, to confirm the listener is working and it does. All that doesn't work, is executing script from a file. I even used it with some inline code, to see if executeScript on it's own works, and it does...

Content script

console.log('Hello from the content-script')
alert('Hello from the content-script')

Background

browser.runtime.onMessage.addListener(
  (message, callback) => {
    browser.tabs.executeScript({
      file: 'content-script.js',
    });
  },
);

Manifest

{
  "manifest_version": 2,
  "name": "Title",
  "homepage_url": "https://example.com/",
  "description": "Dscription",
  "default_locale": "en",
  "permissions": [
    "tabs",
    "activeTab",
    "<all_urls>",
    "*://*/*"
  ],
  "icons": {
    "16": "icons/16.png",
    "48": "icons/48.png",
    "128": "icons/128.png"
  },
  "background": {
    "scripts": [
      "js/background.js"
    ],
    "persistent": false
  },
  "browser_action": {
    "default_popup": "popup.html",
    "default_title": "Title",
    "default_icon": {
      "19": "icons/19.png",
      "38": "icons/38.png"
    }
  }
}

Compiled result

@adambullmer
Copy link
Owner

adambullmer commented Sep 12, 2020

There's been a bit of confusion around this, so I probably need to make the docs more clear around the boilerplate. The default setup should execute your content script when you click on the browser action button.

Most developers are expecting their content scripts to be automatically injected on every page, but given that requires special permissions when you publish the extension and it is my belief that if developers start with the most open permissions, they will not ever revisit and trim it down to only what is necessary.

If automatically injecting your content script is something you wanted to do, here's the chrome docs for what your manifest file needs to include: https://developer.chrome.com/extensions/content_scripts#declaratively

@doutatsu
Copy link
Author

The default setup should execute your content script when you click on the browser action button.

@adambullmer Do you mean when pressing the extension button, so that the popup opens? Because if so, that also doesn't work. But perhaps I am misunderstanding what you mean. I've read over the docs and made permissions very open (as can be seen in my manifest), so I am not sure that's the cause here. I've tried all kinds of communications, but it never works

@maitysubhasis
Copy link

Dropping the following in the manifest.json will seems to work.

"content_scripts": [
    {
      "matches": [
        "<all_urls>"
      ],
      "js": [
        "js/content-script.js"
      ]
    }
  ],

@remcinerney
Copy link

@doutatsu Did you solve this? I'm having the same issue.

@myrccar
Copy link

myrccar commented Jun 11, 2022

for me my content script only loads sometimes(refresh the page a few times and you just might see it) other times its not there(no file (chrome dev tools) ) how can i fix this?

manifest.json:
"content_scripts": [ { "matches": ["<all_urls>"], "js": ["editer.js"] } ], "manifest_version": 3, "name": "chrome extension", "version": "0.1", "description": "idk", "author": "myrccar", "permissions": [ "*://*/*", "webNavigation", "tabs", "background", "contentscripts", "<all_urls>" ], "background": { "service_worker": "background.js" }, "action": { "default_title": "a popup", "default_popup": "popup.html" }

@Heimdall21
Copy link

Same issue for me as @myrccar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants