chrome.cookies.getAll 返回一个空数组



我正在开发一个简单的chrome扩展程序,只需单击一下即可从域中删除所有cookie,但由于某种原因,它不起作用。当我尝试从域中获取所有 cookie 时,它会返回一个空数组。我做错了什么? 这是 js 脚本:

$("#fixTheCookiesButton").click(() => {
// delete the cookies
chrome.cookies.getAll({domain: "https://www.youtube.com"}, (cookies) => {
console.log("deleting " + cookies.length + " cookies")
for(var i = 0; i < cookies.length; i++){
console.log(i + " deleted")
chrome.cookies.remove({
url: "https://www.youtube.com" + cookies[i].path,
name: cookies[i].name
})
}

// some other stuff that isn't relevant here
}

这是我的清单:

{
"manifest_version": 2,
"name": "FixYT",
"version": "1.0",
"description": "Fixes that YT cookie bug with one click",
"browser_action": {
"default_title": "FixYT",
"default_popup": "popup.html"
},
"permissions": [
"cookies",
"https://www.youtube.com/",
"*://www.youtube.com/",
"tabs",
"*://*/"
]
}

我尝试在互联网上四处寻找,但我找不到任何解决方案。

permissionshost_permissions都是必需的。

"permissions": [
"cookies",
"tabs"
],
"host_permissions": ["<all_urls>"],

正如isa所说,chrome.cookies仅在后台定义.js

添加到清单,以便我们可以在后台访问 chrome.cookie.js  

 "permissions": [
     ...
      "cookies",
    ],

背景.js

...
chrome.cookies.getAll({
}, function (theCookies) {
cookies = theCookies
console.log(cookies)
});


从后台.js发送到其他视图的 Cookie

(不是必需的,但仍然有用(


添加到面板.js以搜索 Cookie。[这将在您打开扩展程序时触发,即(拼图图标(->单击扩展]

chrome.runtime.sendMessage({ command: "GetCookies"}, 
function(response) {
console.log("I received cookies!")
console.log(response)
}
);

添加到后台.js逻辑以从浏览器获取 cookie 并检查已知的 cookie


chrome.runtime.onMessage.addListener(function (message, sender, callback) {
if (message.command === 'GetCookies') {
checkKnownCookies()
}
});
let cookies = [] // Hold IDs of recognized cookies
function checkKnownCookies() {
chrome.cookies.getAll({
}, function (theCookies) {
cookies = theCookies
console.log(cookies)
callback(theCookies)
});
}
 

https://developer.chrome.com/docs/extensions/reference/cookies/#method-getAll

要查看控制台的背景.js请转到(拼图图标(->管理扩展,然后单击指向背景的 href 链接.html以获取扩展

你应该在后台调用这个代码块.js

chrome.cookies.getAll({
domain: ".youtube.com"
}, function (cookies) {
for (var i = 0; i < cookies.length; i++) {
console.log(cookies[i] + "deleted");
chrome.cookies.remove({
url: "https://" + cookies[i].domain + cookies[i].path,
name: cookies[i].name
});
}
});

这是一个更现代的示例,将 cookie 从服务工作者传递到内容脚本,该脚本根据当前准则使用承诺而不是回调。对于此方法,必须使用清单版本 3 或更高版本。

content_script.js

chrome.runtime.sendMessage({ action: 'getCookies', domain: document.domain })
.then(response => {
if (response.success) {
// Construct the cookie header string
const cookieHeader = response.cookies.map(cookie => `${cookie.name}=${cookie.value}`).join('; ');
// Make a fetch request with the cookies included
fetch('/some/path', {
headers: {Cookie: cookieHeader}
})
} else {
console.log('Error: ' + response.error)
}
})

service_worker.js

chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
// getCookies
if (message.action === 'getCookies') {
// Get all cookies for this domain
chrome.cookies.getAll({ domain: message.domain }, cookies => {
if (chrome.runtime.lastError) {
// Handle any errors
sendResponse({ success: false, error: chrome.runtime.lastError });
} else {
// Return the cookies in the response message
sendResponse({ success: true, cookies: cookies });
}
});
// Return true to indicate that we will send a response asynchronously
return true;
}

}(;

最新更新