availablity check before run on active tab
This commit is contained in:
		| @ -4,12 +4,15 @@ | ||||
|  * @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 {number} interval interval for detecting | ||||
|  * @param {number} interval retry interval, default: 500ms. | ||||
|  * @param {number} limit retry limit, default: 0, no limit. | ||||
|  * @param {string} log messages logged to console. | ||||
|  * @return {Promise} a promise of the response. | ||||
|  */ | ||||
| function sendMessage(tab, req, log, cond, interval) { | ||||
| function sendMessage(tab, req, log, cond, interval, limit = 0) { | ||||
|     interval = interval || 500; | ||||
|     limit = limit && !isNaN(limit) ? limit : 0; | ||||
|     count = 0; | ||||
|     return new Promise((resolve, reject) => { | ||||
|  | ||||
|         loop(); | ||||
| @ -22,6 +25,11 @@ function sendMessage(tab, req, log, cond, interval) { | ||||
|                 return; | ||||
|             } | ||||
|  | ||||
|             if (limit && count >= limit) { | ||||
|                 reject(`sendMessage loop limit ${limit} reached.`); | ||||
|                 return; | ||||
|             } | ||||
|             count++; | ||||
|             chrome.tabs.sendMessage(tab.id, req, r => { | ||||
|                 // check error but do nothing. | ||||
|                 // do not interrupt promise chains even if error, or the task always fail when: | ||||
|  | ||||
		Reference in New Issue
	
	Block a user