[英]Tracking down an uncaught error in a promise
我正在為 Firefox 編寫一個擴展,以允許我從網站上抓取一些數據。
一個簡單的描述是該站點包含一個索引頁面,其中包含指向包含我想要的數據的子頁面的鏈接列表。 我瀏覽到擴展程序檢測到 URL 的索引頁面,並詢問我是否要抓取它。 如果我說是,內容腳本會抓取附屬頁面 URL 列表並將它們傳遞給后台進程,后台進程應該在新選項卡中依次打開每個頁面,抓取數據,然后關閉選項卡。 為了避免服務器出現問題,我設置了一系列超時來創建新選項卡,目前間隔為 10 秒。
抓取所有數據后,需要將其傳遞給服務器上的 PHP 腳本,但這超出了本問題的范圍。
所有這些都按描述工作,除了我看到此消息出現在控制台上打開和關閉每個選項卡(選項卡編號各不相同):
15:06:27.426 未捕獲(承諾)錯誤:無效的標簽 ID:171
我一天中的大部分時間都對此感到困惑,但我無法追蹤此錯誤的根源。 這是雙重混亂,因為代碼正是我想要的。
問題:這個錯誤來自哪里,我需要做什么來修復它?
這是代碼:
async function backgroundProcess() {
"use strict";
// delay in ms between each new tab. Set to some reasonable value after testing.
const newTabDelay = 5000;
let tabList = [];
let resultList = [];
async function createTab(url, resolve, reject) {
try {
//Create a tab and open the link. Return a promise so that we can wait for everything to complete
let tabId = await browser.tabs.create({url: url});
console.log("executing script in tab " + tabId.id);
let tabData = await browser.tabs.executeScript(
tabId.id,
{
file: 'scrapeMileage.js'
});
console.log("Content script executed");
console.log(tabData);
//resultList.push(tabData);
let tabNumber = tabId.id;
console.log("Removing tab Tab ID " + tabNumber);
await browser.tabs.remove(tabNumber);
console.log("Removed tab Tab ID " + tabNumber);
resolve(tabData);
} catch (e) {
console.log("createTab catch "+e);
reject(e);
}
}
async function tabOpener(linkList) {
try {
console.log("Tab opener now running");
//linkList.forEach((el)=>{console.log("(background) Link found:"+el)});
// Loop through the list opening upo a new tab every few seconds
for (let i = 0; i < Math.min(5, linkList.length); i++) {
console.log("Creating tab for " + linkList[i]);
tabList.push(new Promise((resolve, reject) => {
setTimeout(function () {
createTab(linkList[i], resolve, reject);
}, newTabDelay * i);
}));
}
resultList = await Promise.all(tabList);
console.log("Scraping complete");
console.log(resultList);
} catch (e) {
console.log("tabOpener catch:"+e);
}
}
function listener(message, sender, respond) {
console.log("Received message: "+message.messageType);
console.log(message);
switch (message.messageType) {
case 'mileageData':
break;
case 'scrapeRequest':
console.log("Calling tab opener")
tabOpener(message.data)
break;
}
}
console.log("Setting up message listener");
browser.runtime.onMessage.addListener(listener);
}
console.log("Running background process");
backgroundProcess();
代碼中大量添加了console.log()
以幫助調試。 這是控制台輸出。
15:05:57.459 Webconsole context has changed
15:05:57.462 Running background process background.js:81:9
15:05:57.463 Setting up message listener background.js:76:13
15:06:05.357 Received message: scrapeRequest background.js:62:17
15:06:05.357
Object { messageType: "scrapeRequest", data: (17) […] }
background.js:63:17
15:06:05.358 Calling tab opener background.js:69:25
15:06:05.358 Tab opener now running background.js:40:21
15:06:05.358 Creating tab for https://drivers.uber.com/p3/payments/trips/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx background.js:45:25
15:06:05.359 Creating tab for https://drivers.uber.com/p3/payments/trips/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx background.js:45:25
15:06:05.359 Creating tab for https://drivers.uber.com/p3/payments/trips/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx background.js:45:25
15:06:05.359 Creating tab for https://drivers.uber.com/p3/payments/trips/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx background.js:45:25
15:06:05.359 Creating tab for https://drivers.uber.com/p3/payments/trips/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx background.js:45:25
15:06:05.387 executing script in tab 167 background.js:16:21
15:06:06.895 Content script executed background.js:23:21
15:06:06.895
Array [ {…} ]
background.js:24:21
15:06:06.896 Removing tab Tab ID 167 background.js:27:21
15:06:06.905 Removed tab Tab ID 167 background.js:29:21
15:06:06.906 Uncaught (in promise) Error: Invalid tab ID: 167 undefined
15:06:10.371 executing script in tab 168 background.js:16:21
15:06:11.451 Content script executed background.js:23:21
15:06:11.451
Array [ {…} ]
background.js:24:21
15:06:11.452 Removing tab Tab ID 168 background.js:27:21
15:06:11.461 Removed tab Tab ID 168 background.js:29:21
15:06:11.461 Uncaught (in promise) Error: Invalid tab ID: 168 undefined
15:06:15.372 executing script in tab 169 background.js:16:21
15:06:16.751 Content script executed background.js:23:21
15:06:16.751
Array [ {…} ]
background.js:24:21
15:06:16.752 Removing tab Tab ID 169 background.js:27:21
15:06:16.762 Removed tab Tab ID 169 background.js:29:21
15:06:16.765 Uncaught (in promise) Error: Invalid tab ID: 169 undefined
15:06:20.385 executing script in tab 170 background.js:16:21
15:06:21.481 Content script executed background.js:23:21
15:06:21.481
Array [ {…} ]
background.js:24:21
15:06:21.482 Removing tab Tab ID 170 background.js:27:21
15:06:21.489 Removed tab Tab ID 170 background.js:29:21
15:06:21.490 Uncaught (in promise) Error: Invalid tab ID: 170 undefined
15:06:25.382 executing script in tab 171 background.js:16:21
15:06:27.414 Content script executed background.js:23:21
15:06:27.414
Array [ {…} ]
background.js:24:21
15:06:27.414 Removing tab Tab ID 171 background.js:27:21
15:06:27.423 Removed tab Tab ID 171 background.js:29:21
15:06:27.423 Scraping complete background.js:53:21
15:06:27.423
Array(5) [ (1) […], (1) […], (1) […], (1) […], (1) […] ]
background.js:54:21
15:06:27.426 Uncaught (in promise) Error: Invalid tab ID: 171 undefined
一些注意事項:
scrapeMileage.js
腳本除了返回一個固定值之外什么都不做。從完整的日志來看,錯誤肯定是由 browser.tabs.remove 產生的。 事實上,在 devtools 控制台中使用不存在的選項卡 id 手動運行它會產生相同的錯誤消息。 例如,如果其他東西已經關閉了選項卡,就會發生這種情況。
就像您已經做的那樣,使用異步代碼中的標准 try/catch 可以輕松攔截此錯誤。 如果您的代碼沒有攔截此錯誤,我看到的唯一解釋是 Firefox 中的錯誤或不正確的browser
polyfill。 在 Firefox 中你不需要這個 polyfill。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.