以下骨架有助于实现它;我将所有cookie
信息存储在 chrome 扩展本地存储中,如下所示;
示例代码
清单.json
{
"name" : "Cookie API Demo",
"version" : "1",
"description" : "This is demonstration of Cookie API",
"permissions": [ "cookies","<all_urls>"],
"browser_action": {
"default_icon": "screen.png",
"default_popup":"popup.html"
},
"manifest_version": 2
}
popup.html
<html>
<head>
<script src="popup.js"></script>
</head>
<body>
</body>
</html>
popup.js
function cookieinfo(){
chrome.cookies.getAll({},function (cookie){
console.log(cookie.length);
allCookieInfo = "";
for(i=0;i<cookie.length;i++){
console.log(JSON.stringify(cookie[i]));
allCookieInfo = allCookieInfo + JSON.stringify(cookie[i]);
}
localStorage.allCookieInfo = allCookieInfo;
});
}
window.onload=cookieinfo;
更多API的检查这个
骨架仅适用于当前页面中的 Cookie
如此处所示,您将在当前页面中只有 cookie 信息
清单.json
{
"name" : "Cookie API Demo",
"version" : "1",
"description" : "This is demonstration of Cookie API",
"permissions": [ "cookies","<all_urls>","tabs"],
"browser_action": {
"default_icon": "screen.png",
"default_popup":"popup.html"
},
"manifest_version": 2
}
popup.html
<html>
<head>
<script src="popup.js"></script>
</head>
<body>
</body>
</html>
popup.js
function cookieinfo(){
chrome.tabs.query({"status":"complete","windowId":chrome.windows.WINDOW_ID_CURRENT,"active":true}, function(tab){
console.log(JSON.stringify(tab));
chrome.cookies.getAll({"url":tab[0].url},function (cookie){
console.log(cookie.length);
allCookieInfo = "";
for(i=0;i<cookie.length;i++){
console.log(JSON.stringify(cookie[i]));
allCookieInfo = allCookieInfo + JSON.stringify(cookie[i]);
}
localStorage.currentCookieInfo = allCookieInfo;
});
});
}
window.onload=cookieinfo;