嗨,我很抱歉问这个,我做了很多研究,但我无法解决这个问题,我无法获得我的 cookie 的值,我认为这是因为我的函数有一个回调,(我在代码),但我不知道修复它:/
const cookieUrl = 'http://urlCookie'
const cookieName = 'CookieName'
cookieValue = checkCookie(cookieUrl,cookieName)
console.log('cookieValue: ', cookieValue)
function checkCookie(url, name){
chrome.cookies.get({
url: url,
name: name
},
function (cookie) {
if (cookie) {
console.log(cookie.value)
return cookie.value
}
else {
console.log('Can\'t get cookie! Check the name!')
return 0
}
})
}
大多数 Chrome API 在我们调用它们时都没有返回承诺,我们必须为其提供回调来处理响应。所以在这种情况下,我们可以添加一个 promise 并在回调中解析它,这样我们就可以在 chrome 调用回调时有一个信号。
const cookieUrl = 'http://urlCookie'
const cookieName = 'CookieName'
cookieValue = checkCookie(cookieUrl,cookieName)
.then((cookie) => console.log(cookie))
.catch(function(error) {console.log(error)});
function checkCookie(url, name){
return new Promise((resolve, reject) => {
chrome.cookies.get({
url: url,
name: name
},
function (cookie) {
if (cookie) {
console.log(cookie.value)
resolve(cookie.value)
}
else {
console.log('Can\'t get cookie! Check the name!')
reject(0);
}
})
});
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句