improved log

This commit is contained in:
2018-09-29 15:25:03 +08:00
parent 69ea3cf69b
commit 51da68fee5

View File

@ -92,7 +92,7 @@ function parseUrls(...args) {
function redirectTab(tab, url) {
let curUrl = "";
return queryUrl(tab)
return queryUrl(tab, undefined, 'Query current url...')
.then(u => {
if (url !== u) {
curUrl = u;
@ -100,11 +100,10 @@ function redirectTab(tab, url) {
from: "GotoUrl",
url: url
}
sendMessage(tab, req);
sendMessage(tab, req, `Goto url: ${url}`);
}
})
.then(() => queryUrl(tab, curUrl))
.then(() => reportIn(tab));
.then(() => queryUrl(tab, curUrl, 'Check if tab url matches expected...'))
}
/**
@ -121,7 +120,7 @@ function extractTabData(tab, itemsSelector, fieldSelectors) {
fieldSelectors: fieldSelectors
}
let cond = r => r && r.length;
return sendMessage(tab, req, cond);
return sendMessage(tab, req, 'Extract data from the tab...', cond);
}
/**
@ -134,7 +133,7 @@ function reportIn(tab) {
from: "ReportIn"
}
let cond = r => r == req.from;
return sendMessage(tab, req, cond);
return sendMessage(tab, req, 'Check tab availability...', cond);
}
/**
@ -143,12 +142,12 @@ function reportIn(tab) {
* @param {string} urlExcluded if specified, queryUrl resolves only when response not equals to urlExcluded
* @returns {Promise<string>} a promise of the url
*/
function queryUrl(tab, urlExcluded) {
function queryUrl(tab, urlExcluded, log) {
let req = {
from: "QueryUrl"
}
let cond = url => url && (!urlExcluded || (urlExcluded && urlExcluded != url));
return sendMessage(tab, req, cond);
return sendMessage(tab, req, log, cond);
}
/**
@ -157,9 +156,10 @@ function queryUrl(tab, urlExcluded) {
* @param {object} req the request data.
* @param {function} cond success condition function, r:any=>boolean
* @param {number} interval interval for detecting
* @param {string} log messages logged to console.
* @return {Promise} a promise of the response.
*/
function sendMessage(tab, req, cond, interval) {
function sendMessage(tab, req, log, cond, interval) {
req.from = "DataExtracter:" + req.from;
interval = interval || 500;
return new Promise((resolve, reject) => {
@ -167,7 +167,7 @@ function sendMessage(tab, req, cond, interval) {
loop();
async function loop() {
console.log("request for", req.from);
// console.log("request for", req.from);
let tabAvailable = await getTabByID(tab.id);
if (!tabAvailable) {
reject("Task interrupted due to the target tab is closed.");
@ -175,7 +175,9 @@ function sendMessage(tab, req, cond, interval) {
}
chrome.tabs.sendMessage(tab.id, req, r => {
if (!cond || cond(r)) {
let flag = !cond || cond(r);
if (log) console.log(log, flag ? '(OK)' : '(failed)');
if (flag) {
resolve(r);
} else {
setTimeout(() => {