js doc and code optimize
This commit is contained in:
		| @ -1,15 +1,23 @@ | |||||||
| chrome.runtime.onMessage.addListener( | chrome.runtime.onMessage.addListener( | ||||||
|     function (request, sender, sendResponse) { |     function (request, sender, sendResponse) { | ||||||
|         // console.log(request); |         // console.log(request); | ||||||
|         if (request.from == "DataExtracter:Extract") { |         switch (request.from) { | ||||||
|             let data = extractData(request.itemsSelector, request.fieldSelectors); |             case "DataExtracter:Extract": | ||||||
|             if (sendResponse) sendResponse(data); |                 let data = extractData(request.itemsSelector, request.fieldSelectors); | ||||||
|         } else if (request.from == "DataExtracter:GotoUrl") { |                 if (sendResponse) sendResponse(data); | ||||||
|             window.location.replace(request.url); |                 break; | ||||||
|         } else if (request.from == "DataExtracter:ReportIn") { |             case "DataExtracter:GotoUrl": | ||||||
|             if (sendResponse) sendResponse(request.from); |                 window.location.replace(request.url); | ||||||
|         } else if (request.from == "DataExtracter:QueryUrl") { |                 if (sendResponse) sendResponse(request.url); | ||||||
|             if (sendResponse) sendResponse(window.location.href); |                 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) { | function testArgs(...args) { | ||||||
|  |  | ||||||
|     if (args.length < 2) return false; |     if (args.length < 2) return false; | ||||||
|  |  | ||||||
|     if (args.length == 2) |     if (args.length == 2) | ||||||
|         return (args[0] && args[1] && (typeof args[0] == "string") && (args[1] instanceof Array)) |         return (args[0] && args[1] && (typeof args[0] == "string") && (args[1] instanceof Array)) | ||||||
|  |  | ||||||
|     let urls = []; |     let urls = []; | ||||||
|     if (args.length > 2) return (typeof args[2] == "string") && ( |     if (args.length > 2) return (typeof args[2] == "string") && ( | ||||||
|         (args[3] instanceof Array) || |         (args[3] instanceof Array) || | ||||||
|  | |||||||
| @ -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) { | function extract(itemsSelector, fieldSelectors, url, ...args) { | ||||||
|     let urls = []; |     let urls = []; | ||||||
|     if (url) { |     if (url) { | ||||||
| @ -65,6 +70,13 @@ function redirectTab(tab, url) { | |||||||
|         .then(() => reportIn(tab)); |         .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) { | function extractData(tab, itemsSelector, fieldSelectors) { | ||||||
|     let req = { |     let req = { | ||||||
|         from: "DataExtracter:Extract", |         from: "DataExtracter:Extract", | ||||||
| @ -76,6 +88,11 @@ function extractData(tab, itemsSelector, fieldSelectors) { | |||||||
|     return sendMessageAndDetect(tab, req, cond, failMsg); |     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) { | function reportIn(tab) { | ||||||
|     let req = { |     let req = { | ||||||
|         from: "DataExtracter:ReportIn" |         from: "DataExtracter:ReportIn" | ||||||
| @ -85,6 +102,12 @@ function reportIn(tab) { | |||||||
|     return sendMessageAndDetect(tab, req, cond, failMsg); |     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) { | function queryUrl(tab, urlExcluded) { | ||||||
|     let req = { |     let req = { | ||||||
|         from: "DataExtracter:QueryUrl" |         from: "DataExtracter:QueryUrl" | ||||||
| @ -96,13 +119,13 @@ function queryUrl(tab, urlExcluded) { | |||||||
|  |  | ||||||
| /** | /** | ||||||
|  * Repeatedly sending a message to target tab until the response is detected good. |  * Repeatedly sending a message to target tab until the response is detected good. | ||||||
|  * The response is returned with the Promise. |  * @param {object} tab the table where to send the message | ||||||
|  * @param {chrome.tab} 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 {string} failMsg message when failed after time out |  * @param {string} failMsg message when failed after time out | ||||||
|  * @param {number} failedTimeOut fail time out |  * @param {number} failedTimeOut fail time out | ||||||
|  * @param {number} detectInterval interval for detecting |  * @param {number} detectInterval interval for detecting | ||||||
|  |  * @return {Promise} a promise of the response. | ||||||
|  */ |  */ | ||||||
| function sendMessageAndDetect(tab, req, cond, failMsg, failedTimeOut, detectInterval) { | function sendMessageAndDetect(tab, req, cond, failMsg, failedTimeOut, detectInterval) { | ||||||
|     failedTimeOut = failedTimeOut || 10000; |     failedTimeOut = failedTimeOut || 10000; | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user