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) { function redirectTab(tab, url) {
let curUrl = ""; let curUrl = "";
return queryUrl(tab) return queryUrl(tab, undefined, 'Query current url...')
.then(u => { .then(u => {
if (url !== u) { if (url !== u) {
curUrl = u; curUrl = u;
@ -100,11 +100,10 @@ function redirectTab(tab, url) {
from: "GotoUrl", from: "GotoUrl",
url: url url: url
} }
sendMessage(tab, req); sendMessage(tab, req, `Goto url: ${url}`);
} }
}) })
.then(() => queryUrl(tab, curUrl)) .then(() => queryUrl(tab, curUrl, 'Check if tab url matches expected...'))
.then(() => reportIn(tab));
} }
/** /**
@ -121,7 +120,7 @@ function extractTabData(tab, itemsSelector, fieldSelectors) {
fieldSelectors: fieldSelectors fieldSelectors: fieldSelectors
} }
let cond = r => r && r.length; 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" from: "ReportIn"
} }
let cond = r => r == req.from; 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 * @param {string} urlExcluded if specified, queryUrl resolves only when response not equals to urlExcluded
* @returns {Promise<string>} a promise of the url * @returns {Promise<string>} a promise of the url
*/ */
function queryUrl(tab, urlExcluded) { function queryUrl(tab, urlExcluded, log) {
let req = { let req = {
from: "QueryUrl" from: "QueryUrl"
} }
let cond = url => url && (!urlExcluded || (urlExcluded && urlExcluded != url)); 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 {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} interval interval for detecting * @param {number} interval interval for detecting
* @param {string} log messages logged to console.
* @return {Promise} a promise of the response. * @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; req.from = "DataExtracter:" + req.from;
interval = interval || 500; interval = interval || 500;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@ -167,7 +167,7 @@ function sendMessage(tab, req, cond, interval) {
loop(); loop();
async function loop() { async function loop() {
console.log("request for", req.from); // console.log("request for", req.from);
let tabAvailable = await getTabByID(tab.id); let tabAvailable = await getTabByID(tab.id);
if (!tabAvailable) { if (!tabAvailable) {
reject("Task interrupted due to the target tab is closed."); 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 => { 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); resolve(r);
} else { } else {
setTimeout(() => { setTimeout(() => {