如何使用Google Chrome扩展程序为其他域设置Cookie

时间:2022-08-23 10:07:11

I am developing a Google Chrome Extension. It schould be possible afterwards to set a cookie for a domain which is not mine.

我正在开发Google Chrome扩展程序。之后可能会为不属于我的域设置cookie。

How is this possible with javascript?

如何用javascript实现这一目标?

1 个解决方案

#1


4  

This is a sample implementation for cookies, with this you can set cookies

这是cookie的示例实现,您可以使用它来设置cookie

如何使用Google Chrome扩展程序为其他域设置Cookie

manifest.json

的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.js

popup.js

function cookieinfo(){
    /*chrome.cookies.getAll({},function (cookie){
        console.log(cookie.length);
        for(i=0;i<cookie.length;i++){
            console.log(JSON.stringify(cookie[i]));
        }
    });
    chrome.cookies.getAllCookieStores(function (cookiestores){
        for(i=0;i<cookiestores.length;i++){
            console.log(JSON.stringify(cookiestores[i]));
        }
    });*/
    chrome.cookies.set({"name":"Sample1","url":"http://developer.chrome.com/extensions/cookies.html","value":"Dummy Data"},function (cookie){
        console.log(JSON.stringify(cookie));
        console.log(chrome.extension.lastError);
        console.log(chrome.runtime.lastError);
    });
    /*chrome.cookies.onChanged.addListener(function (changeInfo){
        console.log(JSON.stringify(changeInfo));
    });*/
}
window.onload=cookieinfo;

popup.html

popup.html

<html>
<head>
<script src="popup.js"></script>
</head>
<body>
</body>
</html>

#1


4  

This is a sample implementation for cookies, with this you can set cookies

这是cookie的示例实现,您可以使用它来设置cookie

如何使用Google Chrome扩展程序为其他域设置Cookie

manifest.json

的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.js

popup.js

function cookieinfo(){
    /*chrome.cookies.getAll({},function (cookie){
        console.log(cookie.length);
        for(i=0;i<cookie.length;i++){
            console.log(JSON.stringify(cookie[i]));
        }
    });
    chrome.cookies.getAllCookieStores(function (cookiestores){
        for(i=0;i<cookiestores.length;i++){
            console.log(JSON.stringify(cookiestores[i]));
        }
    });*/
    chrome.cookies.set({"name":"Sample1","url":"http://developer.chrome.com/extensions/cookies.html","value":"Dummy Data"},function (cookie){
        console.log(JSON.stringify(cookie));
        console.log(chrome.extension.lastError);
        console.log(chrome.runtime.lastError);
    });
    /*chrome.cookies.onChanged.addListener(function (changeInfo){
        console.log(JSON.stringify(changeInfo));
    });*/
}
window.onload=cookieinfo;

popup.html

popup.html

<html>
<head>
<script src="popup.js"></script>
</head>
<body>
</body>
</html>