简体   繁体   English

如何在后台获取chrome扩展程序的内容脚本?

[英]How can I get chrome extension's content script to in the background?

This is my package.json, but my background.js is not running in the background. 这是我的package.json,但是我的background.js不在后台运行。 I've been trying different things for a while, and haven't been able to figure it out. 我已经尝试了一段时间,但还没弄清楚。 Thanks in advance for your help, 在此先感谢您的帮助,

{
  "manifest_version": 2,
  "name": "Focus",
  "version": "1.9.3",
  "description": "Block distracting websites and get inspired to focus on what's important",
  "background_page": "background.html",
  "icons": {
    "16": "inspirelogo16.png",
    "48": "inspirelogo48.png",
    "128": "inspirelogo128.png"
  },
  "browser_action": {
    "default_icon": "inspirelogo19.png",
    "default_title": "Inspire Router",
    "default_popup": "/pages/popup.html"
  },
  "permissions": [
    "background",
    "notifications",
    "tabs",
    "<all_urls>",
    "webRequest",
    "*://*.google.com/"
  ],
  "background": {
    "scripts": [
      "background.js"
    ],
    "content_scripts": [
      {
        "matches": ["<all_urls>"],
        "js": ["background.js"]
      }
    ],
    "persistent": true
  },
  "options_page": "/pages/fieldpage.html",
  "content_security_policy": "script-src 'self' 'unsafe-eval' chrome-extension://bjmcjeffnloaojffcbaekmcokegnejlf/pages/inspire.html http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js https://ssl.google-analytics.com/ga.js http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js https://connect.facebook.net https://www.facebook.com https://chrome.google.com https://chrome.google.com; object-src 'self'"
}

you should put the content_script outside of the background, like 您应该将content_script放在背景之外,例如

"background": {
  "scripts": [
    "background.js"
  ],
  "persistent": true
},
"content_scripts": [
  {
    "matches": ["<all_urls>"],
    "js": ["background.js"]
  }
],

And as mentioned by @minj you should remove the background_page attribute. 如@minj所述,您应该删除background_page属性。 (you either use it or the background one) (您可以使用它,也可以使用background

Also the background runs on its own page, not in the page you see in the browser. 后台也会在自己的页面上运行,而不是在浏览器中看到的页面上运行。 You can find it . 你可以找到它。

If you want to have a script accessible both in the bg and content script, like a utils.js file with shared functions, you can add it to the background.scripts and content_scripts.js arrays 如果要使脚本可以同时在bg和内容脚本中访问,例如具有共享功能的utils.js文件,则可以将其添加到background.scriptscontent_scripts.js数组中

You should think of the background page as an additional page (that you can access directly by going to chrome://extensions/ and clicking background page on your extension), separated from the tabs open in your browser. 您应该将背景页面视为一个附加页面(可以通过访问chrome://extensions/并点击扩展程序上的background page直接访问该background page ),与浏览器中打开的标签分开。 For instance you can't access the dom elements of the page you are viewing in the browser from the background page. 例如,您无法从后台页面访问正在浏览器中查看的页面的dom元素。

The bg page and it's variable don't refresh when you reload a normal page (in opposition to the bg page mentioned above) for instance. 例如,当您重新加载常规页面(与上述bg页面相对)时,bg页面及其变量不会刷新。 They're just two different tabs. 它们只是两个不同的标签。

The content scripts however are simply scripts that get loaded in the pages that matches content_scripts.matches , as if they were loaded from the page themselves. 但是,内容脚本只是在与content_scripts.matches匹配的页面中加载的脚本,就像它们是从页面本身加载的一样。 They don't persist between pages, or share information. 它们不会在页面之间持久存在,也不会共享信息。

You might want to both interact with the dom elements and have a persistent bg script. 您可能要与dom元素进行交互并拥有一个持久的bg脚本。 To do that, you use the message passing protocol that makes asynchronous between the content scripts and the bg page. 为此,您使用消息传递协议,该协议使内容脚本和bg页面之间保持异步。 I usually use the bg page as a persistent controller that gets info from the content scripts and send them orders. 我通常将bg页用作从内容脚本获取信息并向其发送订单的持久控制器。

Remove background_page because it overrides background . 删除background_page因为它会覆盖background You can only use one or the other, not both. 您只能使用其中一个,不能同时使用两者。

Alternatively, add your scripts to background.html file via regular script tags: 或者,通过常规script标记将脚本添加到background.html文件:

<script type="application/x-javascript" src="/background.js"></script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从我的 Chrome 扩展程序的后台脚本中获取变量? - How to get a variable from my Chrome Extension's background script? 如何在Chrome扩展程序的内容脚本中将事件监听器添加到Google表格链接? - How can I add event Listener to google sheets link in chrome extension's content script? 如何在 chrome 扩展中动态运行后台脚本? - How can I run background script dynamically in chrome extension? 在 chrome 扩展中,无法从内容脚本向后台脚本发送消息并获得响应 - In a chrome extension, can't send a message from the content script to the background script and get a response Chrome扩展程序:无法使消息传递在后台脚本和内容脚本之间正常工作 - Chrome extension: can't get message passing to work between background script and content script 如何确定Chrome扩展程序是否位于内容脚本的弹出窗口中? - How can I determine if a Chrome extension is in a popup from the content script? "<i>How can I fetch data in my content script?<\/i>如何在我的内容脚本中获取数据?<\/b> <i>(chrome extension)<\/i> (镀铬扩展)<\/b>" - How can I fetch data in my content script? (chrome extension) 如何在 chrome 扩展内容脚本中使用 google api? - How can I use google api in chrome extension content script? 如何在Chrome扩展程序中将参数从后台脚本传递到内容脚本? - How to pass parameter from a background script to content script in chrome extension? 在Chrome扩展程序中从后台页面获取配置到内容脚本 - Get configs from background page to content script in chrome extension
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM