From 7ec3639723c34f59090809f1edd22b383d33e124 Mon Sep 17 00:00:00 2001 From: jebbs Date: Wed, 23 May 2018 12:12:04 +0800 Subject: [PATCH] js doc and code optimize --- scripts/content.js | 29 +++++++++++++++++------------ scripts/extract.js | 31 +++++++++++++++++++++++++++---- 2 files changed, 44 insertions(+), 16 deletions(-) diff --git a/scripts/content.js b/scripts/content.js index 03c9385..01ea596 100644 --- a/scripts/content.js +++ b/scripts/content.js @@ -1,15 +1,23 @@ chrome.runtime.onMessage.addListener( function (request, sender, sendResponse) { // console.log(request); - if (request.from == "DataExtracter:Extract") { - let data = extractData(request.itemsSelector, request.fieldSelectors); - if (sendResponse) sendResponse(data); - } else if (request.from == "DataExtracter:GotoUrl") { - window.location.replace(request.url); - } else if (request.from == "DataExtracter:ReportIn") { - if (sendResponse) sendResponse(request.from); - } else if (request.from == "DataExtracter:QueryUrl") { - if (sendResponse) sendResponse(window.location.href); + switch (request.from) { + case "DataExtracter:Extract": + let data = extractData(request.itemsSelector, request.fieldSelectors); + if (sendResponse) sendResponse(data); + break; + case "DataExtracter:GotoUrl": + window.location.replace(request.url); + if (sendResponse) sendResponse(request.url); + break; + case "DataExtracter:ReportIn": + if (sendResponse) sendResponse(request.from); + break; + case "DataExtracter:QueryUrl": + if (sendResponse) sendResponse(window.location.href); + break; + default: + break; } } ); @@ -49,12 +57,9 @@ function extract(itemsSelector:string, fieldSelectors:string, url:string, pages: } function testArgs(...args) { - if (args.length < 2) return false; - if (args.length == 2) return (args[0] && args[1] && (typeof args[0] == "string") && (args[1] instanceof Array)) - let urls = []; if (args.length > 2) return (typeof args[2] == "string") && ( (args[3] instanceof Array) || diff --git a/scripts/extract.js b/scripts/extract.js index 4f8f23b..8afe6bc 100644 --- a/scripts/extract.js +++ b/scripts/extract.js @@ -1,5 +1,10 @@ -// function extract(itemsSelector, fieldSelectors, url, from, to, interval) -// function extract(itemsSelector, fieldSelectors, url, pages) +/** + * Extract data from current tab / multiple urls. + * @param {string} itemsSelector items selectors for selecting items (data rows) + * @param {Array} fieldSelectors fields selectors for selecting fields (data columns) under each item + * @param {string} url url template to generate urls by filling with page numers. + * @param {...number} args page numers, either [from, to, interval] or [...pages] + */ function extract(itemsSelector, fieldSelectors, url, ...args) { let urls = []; if (url) { @@ -65,6 +70,13 @@ function redirectTab(tab, url) { .then(() => reportIn(tab)); } +/** + * extract data in from the target tab, usually used to detect if the content script is ready. + * @param {any} tab target tab + * @param {string} itemsSelector items selectors for selecting items (data rows) + * @param {Array} fieldSelectors fields selectors for selecting fields (data columns) under each item + * @returns {Promise} a promise of extracted data + */ function extractData(tab, itemsSelector, fieldSelectors) { let req = { from: "DataExtracter:Extract", @@ -76,6 +88,11 @@ function extractData(tab, itemsSelector, fieldSelectors) { return sendMessageAndDetect(tab, req, cond, failMsg); } +/** + * get report in from the target tab, usually used to detect if the content script is ready. + * @param {any} tab target tab + * @returns {Promise} a promise of the report in message + */ function reportIn(tab) { let req = { from: "DataExtracter:ReportIn" @@ -85,6 +102,12 @@ function reportIn(tab) { return sendMessageAndDetect(tab, req, cond, failMsg); } +/** + * get the url of the target tab + * @param {any} tab target tab + * @param {string} urlExcluded if specified, queryUrl resolves only when response not equals to urlExcluded + * @returns {Promise} a promise of the url + */ function queryUrl(tab, urlExcluded) { let req = { from: "DataExtracter:QueryUrl" @@ -96,13 +119,13 @@ function queryUrl(tab, urlExcluded) { /** * Repeatedly sending a message to target tab until the response is detected good. - * The response is returned with the Promise. - * @param {chrome.tab} 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 {function} cond success condition function, r:any=>boolean * @param {string} failMsg message when failed after time out * @param {number} failedTimeOut fail time out * @param {number} detectInterval interval for detecting + * @return {Promise} a promise of the response. */ function sendMessageAndDetect(tab, req, cond, failMsg, failedTimeOut, detectInterval) { failedTimeOut = failedTimeOut || 10000;