我正在尝试使用以下代码从特定域名获取 cookie:
<script language="javascript" type="text/javascript">
var ID;
function getCookies(domain, name) {
chrome.cookies.get({"url": domain, "name": name}, function(cookie) {
ID = cookie.value;
});
}
getCookies("http://www.example.com", "id")
alert(ID);
</script>
问题是弹出警告框始终显示为undefined。但是,如果我更改
ID = cookie.value;
alert(cookie.value);
它正常工作。我如何将这个值保存以便稍后使用?
更新:看起来如果我在脚本运行后从Chrome控制台调用alert(ID),它就可以工作。如何设置我的代码等待chrome.cookies.get运行完成?