請不要洗掉我的問題。我已經沖刷網頁,該檔案,我不知道為什么發生這種情況,并洗掉問題確實不是幫助。沒有其他與我類似的問題。
開始。以下是我相信它應該發生的方式。但即使我把所有的 async away promis 語法放在那里,它也不會發生。當異步函式被呼叫時,沒有人等待它完成。我想知道除了菊花鏈之外我試圖避免的異步功能下的所有內容還能做什么。請幫忙。
我試過使用tabs= await getcurrenttab(),但這會導致一個錯誤,說它只適用于異步函式。聽眾不是。

清單檔案
{
"manifest_version": 3,
"name": "DHL Helper",
"description": "This helper helps DHL's quality of life, improves mental health.",
"version": "1.0",
"action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"content_scripts": [{
"matches":["*://servicenow.*.com/*",
"http://*/*",
"https://*/*"],
"js": ["content_helper.js"]
}],
"permissions": [
"activeTab"
],
"host_permissions": [
"http://www.blogger.com/",
"*://*/*"
]
}
彈出視窗.js
document.addEventListener('DOMContentLoaded', function() {
console.log('got tabs id');
tab = getCurrentTab();
console.log('Active tab ' tab.id);
});
async function getCurrentTab(){
console.log('trying to get tabs id');
let queryOptions = {active: true, currentWindow: true};
return await chrome.tabs.query(queryOptions)
.then((tabs) => {
console.log('Obtained tab ID');
console.log(tabs);
return tabs[0];
})
.catch((Error) => {
console.log('it failed');
console.error;
return;
})
}
uj5u.com熱心網友回復:
document.addEventListener('DOMContentLoaded', async function() {
console.log('got tabs id');
tab = await getCurrentTab();
console.log('Active tab ' tab.id);
});
你的方法已經是正確的,但是如果你想在函式 await 中使用 await,你還必須使執行函式異步。
uj5u.com熱心網友回復:
用戶提到嘗試使用=await getCurrentTab()但遇到錯誤,等待僅適用于異步函式......
添加async到事件偵聽器功能修復了問題
改變這個:
document.addEventListener('DOMContentLoaded', function() {
對此:
document.addEventListener('DOMContentLoaded', async function() {
uj5u.com熱心網友回復:
標簽未定義
var tab = getCurrentTab();
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/328975.html
下一篇:Chrome94&95畫布錯誤
