[英]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.scripts
和content_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.