How to call chrome extension content script once upon entering domain

回眸只為那壹抹淺笑 提交于 2019-12-25 07:24:59

问题


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"
    }
}

来源:https://stackoverflow.com/questions/39537741/how-to-call-chrome-extension-content-script-once-upon-entering-domain

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!