gpt4 book ai didi

javascript - 尝试从 Chrome 扩展中获取 Cookie 时出错

转载 作者:行者123 更新时间:2023-12-05 06:26:36 30 4
gpt4 key购买 nike

我有一个非常基本的 Chrome 扩展程序。我可以很好地执行 JS,但访问 Chrome API 中的任何内容似乎都是一个问题。我正在尝试删除特定站点的 cookie。但是,当我的代码执行时出现以下错误。

错误:

content.js:6 Uncaught TypeError: Cannot read property 'getAll' of undefined

我的代码:

(function(){
chrome.cookies.getAll({}, cookies=>{
_.forEach(cookies, cookie=>{
chrome.cookies.remove({name: cookie.name, url: "www.mydomain.com"});
});
});
)();

我想这可能与我的权限有关,但我不确定。这是我的 manifest.json 文件。

{
"manifest_version": 2,
"name": "Hello World Extension",
"version": "0.1",
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": ["jquery.js","content.js"]
}
],
"permissions": [
"cookies"
]
}

最佳答案

To use the cookies API, you must declare the "cookies" permission in your manifest, along with host permissions for any hosts whose cookies you want to access.

{
"manifest_version": 2,
"name": "Hello World Extension",
"version": "0.1",
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": ["jquery.js","content.js"]
}
],
"permissions": [
"cookies",
"http://*/*",
"https://*/*"
]
}

要删除 cookie,试试这个:

function removeAll(url){
chrome.cookies.getAll({}, function(cookies) {
for (var i in cookies) {
chrome.cookies.remove({"url": url, "name": cookie.name});
}
});
}

我希望这能奏效

关于javascript - 尝试从 Chrome 扩展中获取 Cookie 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55956872/

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