单击按钮删除 Cookie - Web Extension API
Remove Cookies by clicking on button - Web Extension API
我是第一次使用 Firefox Web 扩展 API。我想通过单击按钮删除 example.com 的所有 cookie。我有以下代码但没有得到任何结果。
background.js
function onRemoved(cookie) {
console.log(`Removed: ${cookie}`);
}
function onError(error) {
console.log(`Error removing cookie: ${error}`);
}
function removeCookie() {
var removing = browser.cookies.remove({
url: "example.com"
});
removing.then(onRemoved, onError);
}
browser.browserAction.onClicked.addListener(removeCookie);
manifest.json
{
"description": "RemoveCookie",
"manifest_version": 2,
"name": "remove-cookies",
"version": "1.0",
"icons": {
"48": "icons/icon.png"
},
"background": {
"scripts": ["background.js"]
},
"browser_action": {
"default_icon": "icons/icon.png",
"default_title": "Remove Cookie!"
},
"permissions": [
"*://*.example.com/*",
"cookies"
]
}
来自https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/cookies/remove
The remove() method of the cookies API deletes a cookie, given its name and URL.
这意味着您还需要指定 name
参数。
此外,url
参数必须包含协议,否则承诺将仅通过 null
解析(意味着无法找到 cookie)。
If a cookie matching the details parameter could not be found, the promise is fulfilled with null.
也就是说,您可以使用 browser.cookies.getAll
获取给定 URL 的所有 cookie,然后根据其名称删除每个 cookie:
function onRemoved(cookies) {
console.log('Removed', cookies);
}
function onError(error) {
console.log(`Error removing cookie: ${error}`);
}
function removeCookie() {
var url = "http://example.com"; // NOTE the full url including protocol
browser.cookies.getAll({ url: url })
.then(function(cookies) {
return Promise.all(cookies.map(function(cookie) {
return browser.cookies.remove({ url: url, name: cookie.name });
}));
})
.then(onRemoved, onError);
}
browser.browserAction.onClicked.addListener(removeCookie);
我是第一次使用 Firefox Web 扩展 API。我想通过单击按钮删除 example.com 的所有 cookie。我有以下代码但没有得到任何结果。
background.js
function onRemoved(cookie) {
console.log(`Removed: ${cookie}`);
}
function onError(error) {
console.log(`Error removing cookie: ${error}`);
}
function removeCookie() {
var removing = browser.cookies.remove({
url: "example.com"
});
removing.then(onRemoved, onError);
}
browser.browserAction.onClicked.addListener(removeCookie);
manifest.json
{
"description": "RemoveCookie",
"manifest_version": 2,
"name": "remove-cookies",
"version": "1.0",
"icons": {
"48": "icons/icon.png"
},
"background": {
"scripts": ["background.js"]
},
"browser_action": {
"default_icon": "icons/icon.png",
"default_title": "Remove Cookie!"
},
"permissions": [
"*://*.example.com/*",
"cookies"
]
}
来自https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/cookies/remove
The remove() method of the cookies API deletes a cookie, given its name and URL.
这意味着您还需要指定 name
参数。
此外,url
参数必须包含协议,否则承诺将仅通过 null
解析(意味着无法找到 cookie)。
If a cookie matching the details parameter could not be found, the promise is fulfilled with null.
也就是说,您可以使用 browser.cookies.getAll
获取给定 URL 的所有 cookie,然后根据其名称删除每个 cookie:
function onRemoved(cookies) {
console.log('Removed', cookies);
}
function onError(error) {
console.log(`Error removing cookie: ${error}`);
}
function removeCookie() {
var url = "http://example.com"; // NOTE the full url including protocol
browser.cookies.getAll({ url: url })
.then(function(cookies) {
return Promise.all(cookies.map(function(cookie) {
return browser.cookies.remove({ url: url, name: cookie.name });
}));
})
.then(onRemoved, onError);
}
browser.browserAction.onClicked.addListener(removeCookie);