M. *_*nik 11 javascript cookies google-chrome google-chrome-extension
我正在开发一个简单的 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
}Run Code Online (Sandbox Code Playgroud)
这是我的清单:
{
"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",
"*://*/"
]
}
Run Code Online (Sandbox Code Playgroud)
我尝试在互联网上查找,但找不到任何解决方案。
和permissions都是host_permissions必需的。
"permissions": [
"cookies",
"tabs"
],
"host_permissions": ["<all_urls>"],
Run Code Online (Sandbox Code Playgroud)
正如isa所说,chrome.cookies仅在background.js中定义
\n添加到清单,以便我们可以访问background.js\n\xc2\xa0中的chrome.cookies
\n\xc2\xa0"permissions": [\n \xc2\xa0 \xc2\xa0 \xc2\xa0...\n \xc2\xa0 \xc2\xa0 \xc2\xa0 "cookies",\n \xc2\xa0 \xc2\xa0 ],\n\nRun Code Online (Sandbox Code Playgroud)\n背景.js
\n...\nchrome.cookies.getAll({\n }, function (theCookies) {\n cookies = theCookies\n console.log(cookies)\n });\n\nRun Code Online (Sandbox Code Playgroud)\n
\n添加到 panel.js 以搜索 cookie。[当您打开扩展程序 ie(拼图图标)-> 单击您的扩展程序时,这将触发]
chrome.runtime.sendMessage({ command: "GetCookies"}, \n function(response) {\n console.log("I received cookies!")\n console.log(response)\n }\n);\nRun Code Online (Sandbox Code Playgroud)\n添加到background.js逻辑以从浏览器获取cookie并检查已知cookie
\n\nchrome.runtime.onMessage.addListener(function (message, sender, callback) {\n if (message.command === \'GetCookies\') {\n checkKnownCookies()\n }\n});\nlet cookies = [] // Hold IDs of recognized cookies\nfunction checkKnownCookies() {\n chrome.cookies.getAll({\n }, function (theCookies) {\n cookies = theCookies\n console.log(cookies)\n callback(theCookies)\n });\n}\n\xc2\xa0\nRun Code Online (Sandbox Code Playgroud)\nhttps://developer.chrome.com/docs/extensions/reference/cookies/#method-getAll
\n要查看 background.js 的控制台,请转到(拼图图标)-> 管理扩展,然后单击指向您的扩展的 background.html 的 href 链接
\n小智 6
你应该在background.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
});
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17777 次 |
| 最近记录: |