Javascript Chrome扩展api没有';t检查cookie是否存在
我正试图用以下代码检查带有chrome扩展名的cookie content.jsJavascript Chrome扩展api没有';t检查cookie是否存在,javascript,google-chrome,debugging,cookies,google-chrome-extension,Javascript,Google Chrome,Debugging,Cookies,Google Chrome Extension,我正试图用以下代码检查带有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 n
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
应该在您的背景页面中抛出如下错误:无法读取null的属性“value”
。也许可以尝试在getCookies
函数中测试null
结果,而不是在消息响应中