javascript 删除 cookie chrome 扩展
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13029458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Delete cookies chrome extension
提问by opc0de
I want to delete all cookie on certain domain automatically so I have crafted an extension.I am able to view the cookies for the domain but I didn't find any method to delete them
我想自动删除某个域上的所有 cookie,所以我制作了一个扩展。我可以查看域的 cookie,但我没有找到任何删除它们的方法
Here is my code the function eraseCookie is just called one time
这是我的代码,函数 eraseCookie 只被调用了一次
Any suggestions ?
有什么建议 ?
function eraseCookie(name) {
document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
}
$(document).ready(function() {
var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++)
{
window.alert(cookies[i]);
eraseCookie(cookies[i].split("=")[0]);
}
});
I am also using jquery but I don't see a problem in that!
我也在使用 jquery,但我看不出有什么问题!
{
"name": "Gapa",
"version": "0.1",
"description": "",
"browser_action": {
"default_icon": "sigla.png",
"default_title": "",
"popup": "hello.html"
},
"content_scripts": [
{
"matches": ["*://*.google.ro/*"],
"js": ["jquery-1.8.2.min.js","cookie_handler.js"]
}
],
"icons": {
"128":"sigla.png" },
"permissions": [
"cookies",
"tabs",
"*://*.google.ro/*"
],
"manifest_version": 2
}
LE : Here is how my script file looks now:
LE:这是我的脚本文件现在的样子:
$(document).ready(function() {
var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++)
{
chrome.cookies.remove({"url": ".google.ro", "name":cookies[i].split("=")[0]}, function(deleted_cookie) { window.alert('deleted cookie') });
}
});
回答by Arkadiusz 'flies' Rzadkowolski
First of all you must provide cookies permission in your manifest.
首先,您必须在清单中提供 cookie 权限。
Second of all Chrome provides you with cookies api where remove function is localted:
其次,Chrome 为您提供了 cookie api,其中删除功能是本地化的:
chrome.cookies.remove(object details, function callback);
You can use it like that:
你可以这样使用它:
chrome.cookies.remove({"url": "http://domain.com", "name": "cookieName"}, function(deleted_cookie) { console.log(deleted_cookie); });
Try using this to list all cookies for selected domains (inner delete function removes all cookies from this domain):
尝试使用它来列出选定域的所有 cookie(内部删除功能会从此域中删除所有 cookie):
chrome.cookies.getAll({domain: "domain.com"}, function(cookies) {
for(var i=0; i<cookies.length;i++) {
chrome.cookies.remove({url: "http://domain.com" + cookies[i].path, name: cookies[i].name});
}
});
In your manifest.json add:
在您的 manifest.json 添加:
"background": {
"scripts": ["background.js"]
},
and in background.js you include proposed function.
并在 background.js 中包含建议的功能。
回答by Jon
I pieced together Arkadiusz's answer and got this working:
我拼凑了 Arkadiusz 的答案并得到了这个工作:
In manifest.json:
在 manifest.json 中:
"background": {
"scripts": ["background.js"]
},
"permissions": [
"cookies",
"https://*/",
"http://*/"
]
In background.js:
在 background.js 中:
chrome.cookies.getAll({domain: ".mydomain.com"}, function(cookies) {
for(var i=0; i<cookies.length;i++) {
console.log(cookies[i]);
chrome.cookies.remove({url: "https://" + cookies[i].domain + cookies[i].path, name: cookies[i].name});
}
});