在网路的世界中,我们都知道 Cookie 被用于用户行为数据的追踪,因此 Chrome Extension API 也提供了操作 Cookie 相关的内容,这一组的 API 可以查询和修改 Cookie,或在 Cookie 被修改时得到一个通知。
var GET_COOKIES = "GET_COOKIES";
var GET_CURRENT_COOKIES = "GET_CURRENT_COOKIES";
var SET_COOKIES = "SET_COOKIES";
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
var action = request.action;
var args = request.args;
switch (action) {
case GET_COOKIES:
chrome.cookies.getAllCookieStores(function(cookieStores){
sendResponse({ action: GET_COOKIES, args: cookieStores });
});
return true;
case GET_CURRENT_COOKIES:
chrome.cookies.getAll({ url: "https://icepy.me"}, function(cookies){
sendResponse({ action: GET_CURRENT_COOKIES, args: cookies });
});
return true;
case SET_COOKIES:
chrome.cookies.set({
url: "https://icepy.me",
name: "name",
value: "icepy",
domain: ".icepy.me"
})
break;
}
})
var GET_COOKIES = "GET_COOKIES";
var GET_CURRENT_COOKIES = "GET_CURRENT_COOKIES";
var SET_COOKIES = "SET_COOKIES";
chrome.runtime.sendMessage({
action: GET_COOKIES,
args: ''
}, (response) => {
// 处理
console.log(response);
});
chrome.runtime.sendMessage({
action: GET_CURRENT_COOKIES,
args: ''
}, (response) => {
// 处理
console.log(response);
});
chrome.runtime.sendMessage({
action: SET_COOKIES,
args: ''
}, (response) => {
// 处理
console.log(response);
});