我一直在遵循文档,但操作不正确。有人可以看看我的代码,看看我在做什么错吗?
function getCookies(){
var cookies = {
art_rfp : '',
art_token: '',
hire_rfp: '',
hire_token: '',
corp_rfp: '',
corp_token: '',
}
chrome.cookies.getAll({"url":"https://url.com"}, function (cookies) {
var string = ''
for(var i=0; i<cookies.length; i++){
var name = cookies[i].name
// console.log(name)
if (name == 'a-sso_rfp') {
console.log('ART- '+cookies[i].name+': '+cookies[i].value)
cookies.art_rfp = cookies[i].value
}
else if (name == 'b-sso_token') {
console.log('ART- '+cookies[i].name+': '+cookies[i].value)
cookies.art_token = cookies[i].value
}
var value = cookies[i].value
string += name+"="+value+"; "
}
});
chrome.cookies.getAll({"url":"https://url2.com"}, function (cookies) {
var string = ''
for(var i=0; i<cookies.length; i++){
var name = cookies[i].name
if (name == 'a-sso_rfp') {
console.log('HIRE- '+cookies[i].name+': '+cookies[i].value)
cookies.hire_rfp = cookies[i].value
}
else if (name == 'b-sso_token') {
console.log('HIRE- '+cookies[i].name+': '+cookies[i].value)
cookies.hire_token = cookies[i].value
}
}
});
chrome.cookies.getAll({"url":"https://url3.com"}, function (cookies) {
var string = ''
for(var i=0; i<cookies.length; i++){
var name = cookies[i].name
if (name == 'e-sso_rfp') {
console.log('CORP- '+cookies[i].name+': '+cookies[i].value)
cookies.corp_rfp = cookies[i].value
}
else if (name == 'f-sso_token') {
console.log('CORP- '+cookies[i].name+': '+cookies[i].value)
cookies.corp_token = cookies[i].value
}
}
});
return cookies
}
function sendCookies(cookies){
console.log(cookies)
}
console.log('this started')
async function run(){
let cookies = await getCookies()
let resposne = await sendCookies(cookies)
}
run()
当我在cookies
中打印sendCookies()
时,它是在浏览器获取cookie数据之前打印的。我正在尝试让它等到完成为止。
答案 0 :(得分:3)
您的getCookies()
函数不是异步的。它会进行一些异步调用(chrome.cookies.getAll()
,但不等待结果,而是立即返回cookies
对象。
您需要使通话变大,例如
const getAllCookiesPromisified = (url) => new Promise(resolve => {
chrome.cookies.getAll({url}, resolve);
});
const p1 = getAllCookiesPromisified("https://url.com");
const p2 = getAllCookiesPromisified("https://url2.com");
return Promise.all([p1, p2]);
答案 1 :(得分:0)