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

时间:2012-11-27 09:03:16

标签: javascript google-chrome cookies google-chrome-extension

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

使用javascript可以实现这一目标吗?

1 个答案:

答案 0 :(得分:5)

这是Cookie的示例实现,您可以使用此设置Cookie

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

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