作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
{
"name": "coffee",
"manifest_version":2,
"version": "1.0",
"description": "coffee test",
"browser_action": {
"default_icon": "icon.png" ,
"default_title": "My Task List",
"default_popup": "popup.html"
},
"background": {
"scripts": ["background.js"]
},
"content_scripts": [{
"matches": ["http://*/*","https://*/*" ],
"js": ["inject.js"],
"run_at": "document_end",
"all_frames": true
}],
"permissions": [
"tabs", "http://*/*","https://*/*"
]
}
list .josn
count=1;
背景.js
var bgpg=chrome.extension.getBackgroundPage();
if(bgpg)
{
alert("ok");
if(bgpg.count>=0){
bgpg.count=bgpg.count+1;
}else{
bgpg.count=0;
}
alert(bgpg.count);
}else{alert("NO");}
注入(inject).js。
它没有改变任何东西。事实上,上周我成功地使用了背景值,但今天我完全找不到我错在哪里。
最佳答案
内容脚本无法调用 chrome.extension.getBackgroundPage()
,这对它来说权限太高了。
出于安全原因内容脚本 have very limited access to Chrome APIs 。后台页面具有完全访问权限,因此禁止直接操作它。您的代码抛出错误并停止。
However, content scripts have some limitations. They cannot:
- Use
chrome.*
APIs, with the exception of:
extension
(getURL
,inIncognitoContext
,lastError
,onRequest
,sendRequest
)- [...]
您将需要使用Messaging与后台页面通讯;但是,如果您需要的只是一些数据,您可以尝试使用 chrome.storage
- 它由两者共享。
关于javascript - 如何使用backgroundpage保留在注入(inject)js中使用的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29913946/
我是一名优秀的程序员,十分优秀!