Chrome扩展api;t检查cookie是否存在
Chrome extensions api doesn't check if cookie exists
我正在尝试使用以下代码检查带有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,cookie
就是null
,因此cookie.value
应该在后台页面中抛出一个错误,如下所示:Cannot read property 'value' of null
。也许可以尝试在getCookies
函数中测试null
结果,而不是在消息响应中测试。
相关文章:
- 域特定cookie是否易受CSRF攻击
- 谷歌cookie是否存在于chrome,火狐,即在phantomjs中发出某些请求时发送的浏览器
- Chrome扩展api;t检查cookie是否存在
- 如何检测 cookie 是否在浏览器中被禁用 AngularJS.
- 当网页存储在计算机上时,cookie 是否有效
- 如何检查cookie是否存在,即使它是在其他应用程序中创建的?(使用 JS 或 C#)
- 重复/循环javascript函数来检查cookie是否已更改
- http_only cookie 是否会与 AJAX 一起发送
- 客户端 Cookie 是否可供所有人访问,服务器端是否有 Cookie
- 检查 Cookie 是否存在 - Chrome 扩展程序
- 检查 Cookie 是否存在
- Cookie 是否只适合客户端使用
- Cookie 是否安全,无法在用户计算机上进行本地访问
- 检查cookie是否仍然有效
- JQuery Cookies创建..检查cookie是否已经有值
- 如何测试javascript cookie是否已过期
- Javascript检查cookie是否存在
- 检测特定cookie是否存在
- 检查cookie是否存在以进行年龄验证
- Jquery Cookie 插件,检查 cookie 是否未设置