我正在尝试使用以下代码检查带有chrome扩展名的cookie
content.js
if (hostName == "google.com") {
chrome.runtime.sendMessage({greeting: "hello"}, function(response) {
console.log(response.farewell);
if (response.farewell == null) {console.log("cookie is null");}
});
}
background.js
function getCookies(domain, name, callback) {
chrome.cookies.get({"url": domain, "name": name}, function(cookie) {
if(callback) {
callback(cookie.value);
}
});
}
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
if (message.greeting == "hello") {
getCookies("http://www.google.com", "cookie_name", function(id) {
if (id) {
alert("cookie "+id);
sendResponse({farewell: id});
} else {
alert("cookie "+id);
sendResponse({farewell: id});
}
});
return true;
}
});
如果设置了cookie,则此代码有效。但是,如果没有cookie,就不会发出警报和响应。如何检查是否没有Cookie?我究竟做错了什么?
综观文档(这里)cookie
是null
,如果没有一个cookie,所以cookie.value
应该在这样的你的背景页面抛出一个错误:Cannot read property 'value' of null
。也许尝试null
在getCookies
功能而不是消息响应中测试结果。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句