Google chrome 通过Chrome扩展访问Cookie
我想写一个Chrome扩展,根据我的网站设置的cookie值,在某些页面上显示一些内容。然而,我的扩展只是拒绝承认chrome.cookies对象的存在(是的,我在权限列表中包括了“cookies”)。 我通过在内容脚本中添加以下代码来测试这一点:Google chrome 通过Chrome扩展访问Cookie,google-chrome,cookies,google-chrome-extension,Google Chrome,Cookies,Google Chrome Extension,我想写一个Chrome扩展,根据我的网站设置的cookie值,在某些页面上显示一些内容。然而,我的扩展只是拒绝承认chrome.cookies对象的存在(是的,我在权限列表中包括了“cookies”)。 我通过在内容脚本中添加以下代码来测试这一点: $(document).ready(function() { console.log (chrome.cookies); }); chrome.extension.sendRequest("getID", function(response
$(document).ready(function() {
console.log (chrome.cookies);
});
chrome.extension.sendRequest("getID", function(response) {
// do something with the id
});
它总是打印“未定义”,我不知道为什么。
是否有一些我应该注意的使用限制,或者我遗漏了一些其他必要的细节?最终我找到了答案 在内容脚本中:
$(document).ready(function() {
console.log (chrome.cookies);
});
chrome.extension.sendRequest("getID", function(response) {
// do something with the id
});
在后台脚本中:
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
if (request == 'getID') {
chrome.cookies.getAll ({domain: 'whatever', name: 'id'}, function (cookies) {
sendResponse (cookies[0].value);
});
}
});
仅适用于扩展页,不适用于内容脚本。