最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to specify different match patterns for multiple content scripts in the manifest.json file of the same extensio

programmeradmin3浏览0评论

Here is the scenario:

Scenario 1: I want to inject different content scripts into different pages based on the URL Domain.

For ex: Inject cs1.js content script into www.a but inject cs2.js content script into www.b. Both the content scripts cs1.js and cs2.js are part of the same chrome extension.So how can I specify different match patterns ( under "matches" section while defining "content_scripts" in the manifest.json file ) so that one script is injected in one page while other is injected in other page.

Scenario 2: How can I mention, if under a subdomain of say www.a, I wish to inject the content script into all pages in the domain except www.b.a ( all other subdomains need to be injected with the content script ). How do I specify match pattern for this case.

Thanks in advance.

Here is the scenario:

Scenario 1: I want to inject different content scripts into different pages based on the URL Domain.

For ex: Inject cs1.js content script into www.a. but inject cs2.js content script into www.b.. Both the content scripts cs1.js and cs2.js are part of the same chrome extension.So how can I specify different match patterns ( under "matches" section while defining "content_scripts" in the manifest.json file ) so that one script is injected in one page while other is injected in other page.

Scenario 2: How can I mention, if under a subdomain of say www.a., I wish to inject the content script into all pages in the domain except www.b.a. ( all other subdomains need to be injected with the content script ). How do I specify match pattern for this case.

Thanks in advance.

Share Improve this question asked Apr 25, 2011 at 17:57 VenkateshVenkatesh 3583 silver badges9 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 9

Specifying the content_scripts within manifest.json is a bit limiting. Although you can specify different match patterns, you can only have it execute one possible slew of files.

To achieve what you want, you'll need to set up a background_page that can interpret page URLs, and execute content scripts as you see fit.

Three different methods might be:

  1. Use the manifest's content_scripts to match all possible URLs and execute a simple content script to send a message request to the background page via chrome.extension.sendRequest(). The background page listens for messages with chrome.extension.onRequest.addListener(), receives the message, interprets the tab's URL with sender.tab.url (or have the tab's message send window.location.hostname, probably easier), then decides what content scripts to inject to the tab via chrome.tabs.executeScript().
     
    or

  2. Just have your background_page listen for chrome.tabs.onCreated.addListener() and chrome.tabs.onUpdated.addListener() to get a tab's URL, then decide what content scripts to inject to the tab via chrome.tabs.executeScript(). No need to specify content_scripts in the manifest for this.  
     
    or

  3. Similar to 1, but have your manifest content_scripts script figure out what to do based on the current URL (again could interpret window.location.hostname), then use chrome.extension.sendRequest() with a message for your background_page stating which content scripts to execute (meaning your background_page doesn't decide - the original content script does). Same result though.

Meanwhile szenario 1 can be solved: You can inject different content scripts into different pages this way:

"content_scripts": 
[{
  "run_at": "document_end",
  "js": [
    "cs1.js"
  ],
  "matches": [
    "https://www.a./*"
  ]
},
{
  "run_at": "document_end",
  "js": [
    "cs2.js"
  ],
  "matches": [
    "https://www.b./*"
  ]
}],

Hope this answear helps for the initial question.

http://code.google./chrome/extensions/content_scripts.html#include-exclude-globs

Does this help at all?

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论