2 javascript cookies local-storage google-chrome-extension
我尝试了以下但不确定如何操纵它.
的manifest.json
"permissions": [
"tabs",
"http://*/*",
"https://*/*"
],
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"js": ["cookie.js"]
}
]
Run Code Online (Sandbox Code Playgroud)
cookie.js
console.log(document.cookie);
Run Code Online (Sandbox Code Playgroud)
只是在currentrnt页面console.log中显示,而不是在扩展的实际控制台中.
是否有可能获取您当前所在站点的cookie并将其设置在扩展的本地存储中?所以在这一点上,我可以在任何页面上,扩展程序仍然具有该值.
以下Skeleton有助于实现它; 我将所有cookie信息存储在Chrome扩展本地存储中,如下所示;
示例代码

的manifest.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
}
Run Code Online (Sandbox Code Playgroud)
popup.html
<html>
<head>
<script src="popup.js"></script>
</head>
<body>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
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;
Run Code Online (Sandbox Code Playgroud)
欲了解更多API的支票本
骨架仅适用于当前页面中的Cookie
如此处所示,您当前页面中只有cookie信息

的manifest.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
}
Run Code Online (Sandbox Code Playgroud)
popup.html
<html>
<head>
<script src="popup.js"></script>
</head>
<body>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
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;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5560 次 |
| 最近记录: |