-->

How to call chrome extension content script once u

2019-09-14 07:44发布

问题:

I have a chrome extension that operates on multiple paths of a domain. The functionality I am trying to achieve is upon entering the domain (any of the paths) I call a content script (content_script_getdata.js) that parses the data and stores it in chrome.storage.local. After entering, navigation between the paths doesn't need to call content_script_getdata.js. Each path has it's own content script that gets from chrome.storage.local and updates the UI of that path based on the data.

How can I achieve this functionality? I was thinking of putting it in background.js but I need the functionality of a content script. I was also thinking of checking in each content script of the paths if the data has been set in chrome.storage.local and if not then call content_script_getdata.js via jQuery's $.getScript(), but am not sure if this is the best option.

Edit: manifest.json (relevant parts)

{
"background": { 
    "scripts": [ "background.js" ], 
    "persistent": false
  },

  "permissions": [
    "tabs",
    "storage"
  ],

  "content_scripts": [
    {
      "matches": ["http://a.com/b?*"],
      "js": ["jquery-3.1.0.min.js", "content_script_b.js"],
      "run_at": "document_end"
    },
    {
      "matches": ["http://a.com/c?*"],
      "js": ["jquery-3.1.0.min.js", "content_script_c.js"],
      "run_at": "document_end"
    },
    {
      "matches": ["http://a.com/d?*"],
      "js": ["jquery-3.1.0.min.js", "content_script_d.js"],
      "run_at": "document_end"
    }
}