remove fail timeout logic
This commit is contained in:
@ -103,7 +103,7 @@ function extractTabData(tab, itemsSelector, fieldSelectors) {
|
|||||||
itemsSelector: itemsSelector,
|
itemsSelector: itemsSelector,
|
||||||
fieldSelectors: fieldSelectors
|
fieldSelectors: fieldSelectors
|
||||||
}
|
}
|
||||||
let cond = r => !!r;
|
let cond = r => r && r.length;
|
||||||
return sendMessage(tab, req, cond);
|
return sendMessage(tab, req, cond);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -139,31 +139,30 @@ function queryUrl(tab, urlExcluded) {
|
|||||||
* @param {object} tab the table where to send the message
|
* @param {object} tab the table where to send the message
|
||||||
* @param {object} req the request data.
|
* @param {object} req the request data.
|
||||||
* @param {function} cond success condition function, r:any=>boolean
|
* @param {function} cond success condition function, r:any=>boolean
|
||||||
* @param {number} failedTimeOut fail time out
|
* @param {number} interval interval for detecting
|
||||||
* @param {number} detectInterval interval for detecting
|
|
||||||
* @return {Promise} a promise of the response.
|
* @return {Promise} a promise of the response.
|
||||||
*/
|
*/
|
||||||
function sendMessage(tab, req, cond, failedTimeOut, detectInterval) {
|
function sendMessage(tab, req, cond, interval) {
|
||||||
req.from = "DataExtracter:" + req.from;
|
req.from = "DataExtracter:" + req.from;
|
||||||
failedTimeOut = failedTimeOut || 10000;
|
interval = interval || 500;
|
||||||
detectInterval = detectInterval || 500;
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
let timeOut;
|
|
||||||
let rejectTimeout = setTimeout(() => {
|
|
||||||
reject(`${req.from} failed after ${failedTimeOut/1000} seconds.`);
|
|
||||||
clearTimeout(timeOut);
|
|
||||||
}, failedTimeOut);
|
|
||||||
loop();
|
loop();
|
||||||
|
|
||||||
function loop() {
|
async function loop() {
|
||||||
|
console.log("request for", req.from);
|
||||||
|
let tabAvailable = await getTabByID(tab.id);
|
||||||
|
if (!tabAvailable) {
|
||||||
|
throw new Error("Task interupted due to the target tab is closed.");
|
||||||
|
}
|
||||||
|
|
||||||
chrome.tabs.sendMessage(tab.id, req, r => {
|
chrome.tabs.sendMessage(tab.id, req, r => {
|
||||||
if (!cond || cond(r)) {
|
if (!cond || cond(r)) {
|
||||||
clearTimeout(rejectTimeout);
|
|
||||||
resolve(r);
|
resolve(r);
|
||||||
} else {
|
} else {
|
||||||
timeOut = setTimeout(() => {
|
setTimeout(() => {
|
||||||
loop();
|
loop();
|
||||||
}, detectInterval);
|
}, interval);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -180,3 +179,11 @@ async function getActiveTab(currentWindow) {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function getTabByID(id) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
chrome.tabs.get(id, function (tab) {
|
||||||
|
resolve(tab);
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user