gpt4 book ai didi

javascript - 如何使用backgroundpage保留在注入(inject)js中使用的值?

转载 作者:行者123 更新时间:2023-12-03 10:15:15 24 4
gpt4 key购买 nike

{  
"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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com