gpt4 book ai didi

javascript - Chrome 扩展 : get current page cookie and same to plugin local storage

转载 作者:行者123 更新时间:2023-11-29 16:17:13 25 4
gpt4 key购买 nike

我尝试了以下但不确定如何操作它。

list .json

  "permissions": [
"tabs",
"http://*/*",
"https://*/*"
],
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"js": ["cookie.js"]
}
]

cookie.js

console.log(document.cookie);

只是显示在当前页面的 console.log 中,而不是在扩展的实际控制台中。

是否可以获取您当前所在站点的 cookie 并将其设置在扩展程序的本地存储中?所以那时,我可以在任何页面上,扩展名仍然具有值(value)。

最佳答案

以下骨架有助于实现它;我将所有 cookie 信息存储在 chrome 扩展本地存储中,如下所示;

示例代码

enter image description here

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
}

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 的检查 THIS

Skeleton For only Cookies in current Page

如这里所示,您将只有当前页面中的 cookie 信息

enter image description here

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
}

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;

关于javascript - Chrome 扩展 : get current page cookie and same to plugin local storage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13607528/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com