Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Chrome扩展api没有';t检查cookie是否存在_Javascript_Google Chrome_Debugging_Cookies_Google Chrome Extension - Fatal编程技术网

Javascript Chrome扩展api没有';t检查cookie是否存在

Javascript 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

我正试图用以下代码检查带有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
应该在您的背景页面中抛出如下错误:
无法读取null的属性“value”
。也许可以尝试在
getCookies
函数中测试
null
结果,而不是在消息响应中