【问题标题】:How to set a cookie for another domain with Google Chrome Extension如何使用 Google Chrome 扩展程序为另一个域设置 cookie
【发布时间】:2012-11-14 20:37:07
【问题描述】:

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

用 javascript 怎么可能?

【问题讨论】:

  • 只是好奇,但你为什么要这样做?
  • 这对我来说会更容易,而不是一直打开设置。
  • 有谁知道为什么某些具有特定 URL 的网站(例如:“www.something.com”)具有另一个域(例如:“.gstatic.com”、“sth.com”等)的 cookie?

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


【解决方案1】:

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

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>

【讨论】:

  • 如果我们将域设置为:chrome.cookies.set({"name":"Sample1","url":"developer.chrome.com/extensions/…Data", "domain": "sth.com" }); ?请注意,某事与 chrome.com 不同!
猜你喜欢
  • 2014-03-08
  • 2019-10-11
  • 2017-02-04
  • 2011-10-09
  • 2016-03-06
相关资源
最近更新 更多