js doc and code optimize

This commit is contained in:
2018-05-23 12:12:04 +08:00
parent 2cbcae6951
commit 7ec3639723
2 changed files with 44 additions and 16 deletions

View File

@ -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<string>} 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<string>} fieldSelectors fields selectors for selecting fields (data columns) under each item
* @returns {Promise<string[]>} 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<string>} 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<string>} 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;