Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome extension: content script to run on all pages, and another to only run on one page

I'm writing a Chrome extension that needs to do the following:

  • inject a content script into the current (any) page when the popup fires
  • inject a different content script into all pages of a specific domain, always

So far I have the first one implemented, by calling chrome.tabs.executeScript() in the popup.html file and having the following in the manifest to allow the script to run on any page:

"permissions": [
  "tabs", "http://*/*"
],

Now, according to http://code.google.com/chrome/extensions/content_scripts.html, if I want a content script to always run on a specific page, I must declare it in the manifest and set permissions to include only pages that the code should run on. However, this will break the first part.

How do I solve this? The only way I can think of is to always call some kind of "caller" script which then does its own checking and loading of other scripts, but that just seems very messy, and i assume there has to be a better way.

like image 869
Mala Avatar asked Oct 04 '11 21:10

Mala


People also ask

What is Content_scripts?

Content scripts are files that run in the context of web pages. By using the standard Document Object Model (DOM), they are able to read details of the web pages the browser visits, make changes to them, and pass information to their parent extension.

What is an unpacked Chrome Extension?

Chrome extensions can be either packed or unpacked. Packed extensions are a single file with a . crx extension. Unpacked extensions are a directory containing the extension, including a manifest. json file.

Can you use 2 Chrome extensions at once?

Use multiple 'New Tab' chrome extensions at the same time. MultiNewTab is a chrome extension that enables you to use several 'New Tab' chrome extensions at the same time without having to manually enable and disable them all the time.


1 Answers

{
  "content_scripts": [
    {
      "matches": ["http://www.google.com/*"],
      "js": ["myscript.js"]
    }
  ],
  "permissions": [
     "tabs", "http://*/*"
  ]
}

myscript.js is automatically injected to google.com, you can still manually inject to http://*/*.

like image 191
serg Avatar answered Nov 22 '22 02:11

serg