2015-05-29 58 views

回答

1

您可以访问与铬API饼干,别忘了添加的权限在您的清单。

Api docs

这里的问题是,你只能从后台脚本访问它。 如果你想从弹出脚本或内容脚本访问它,你必须使用消息API来询问并从后台脚本接收到答案。

在弹出式窗口或内容脚本,您可以拨打:

var message = {name: "getCookie", params: {...}}; //params needed for get method 
var callback = function (response) { 
    //Do what you want with your cookie which is in response.cookie 
} 
chrome.runtime.sendMessage(message, callback); 

所以在你的背景脚本:

chrome.runtime.onMessage.addListener(
    function(message, sender, sendResponse) { 
     if(message.name == "getCookie") { // message.name from above 
      chrome.cookies.get(message.params, function (cookie) { 
      sendResponse({cookie: cookie}); 
      })   
     } 
    }); 
+0

非常感谢你的帮助。如果我将“cookie”添加到清单中的权限,chrome抱怨说“'cookie'只允许用于扩展和旧版打包应用程序,但这是一个打包的应用程序。” – Merijn

+1

在后台脚本中:“getCookies”应该是“getCookie”,对吧? – Merijn

+0

对不起,这是一个错误 –