refactor
This commit is contained in:
		| @ -1,6 +1,6 @@ | ||||
| import { Actions, Request } from "../common"; | ||||
| import { sendMessage, ResponseChecker } from "./messaging"; | ||||
| import { logger } from "./logger"; | ||||
| import { logger } from "../common/logger"; | ||||
|  | ||||
| /** | ||||
|  * redirect tab to url. | ||||
|  | ||||
| @ -1,4 +1,4 @@ | ||||
| import { logger } from "./logger"; | ||||
| import { logger } from "../common/logger"; | ||||
| import { Actions } from "../common"; | ||||
| import { messageSubscribers } from "./messaging"; | ||||
|  | ||||
|  | ||||
| @ -2,7 +2,7 @@ import { Task } from "./task"; | ||||
| import { saveFile } from "./tools"; | ||||
| import { createTab, getActiveTab, ping } from "./actions"; | ||||
| import { ExtractResult } from "./result"; | ||||
| import { logger } from "./logger"; | ||||
| import { logger } from "../common/logger"; | ||||
| import { caches } from "./caches"; | ||||
|  | ||||
| export class Extractor { | ||||
| @ -12,6 +12,14 @@ export class Extractor { | ||||
|     constructor(options?) { | ||||
|         if (options) this._options = options; | ||||
|     } | ||||
|     static async ping(count: number = 1) { | ||||
|         let tab = await getActiveTab(true) || await getActiveTab(false); | ||||
|         let succ = await ping(tab, count); | ||||
|         if (!succ) { | ||||
|             logger.error('Cannot contact with active tab.'); | ||||
|             return; | ||||
|         } | ||||
|     } | ||||
|     /** | ||||
|      * Save current state, in case we restore it later. | ||||
|      */ | ||||
|  | ||||
| @ -1,75 +0,0 @@ | ||||
| export enum LOGGER_LEVEL { | ||||
|     DEBUG = 1, | ||||
|     INFO, | ||||
|     WARN, | ||||
|     ERROR, | ||||
|     DISABLED, | ||||
| }; | ||||
|  | ||||
| export class Logger { | ||||
|     private _notificationId = undefined; | ||||
|     private _log_level = LOGGER_LEVEL.INFO; | ||||
|     private _notify_level = LOGGER_LEVEL.ERROR; | ||||
|     constructor(logLevel, notifyLevel) { | ||||
|         if (logLevel) this._log_level = logLevel; | ||||
|         if (notifyLevel) this._notify_level = notifyLevel; | ||||
|         chrome.notifications.onClosed.addListener((id, byUser) => { this._notify_level = undefined }); | ||||
|     } | ||||
|     get logLevel() { | ||||
|         return this._log_level; | ||||
|     } | ||||
|     set logLevel(val: LOGGER_LEVEL) { | ||||
|         this._log_level = val; | ||||
|     } | ||||
|     get notifyLevel() { | ||||
|         return this._notify_level; | ||||
|     } | ||||
|     set notifyLevel(val: LOGGER_LEVEL) { | ||||
|         this._notify_level = val; | ||||
|     } | ||||
|     log(level: LOGGER_LEVEL, loggerFn: Function, ...msgs) { | ||||
|         if (level < this._log_level) return; | ||||
|         let time = new Date().toLocaleString(); | ||||
|         loggerFn(`${time} [${LOGGER_LEVEL[level]}]`, ...msgs); | ||||
|         if (level < this._notify_level) return; | ||||
|         this.notify(...msgs); | ||||
|     } | ||||
|     debug(...msgs) { | ||||
|         this.log(LOGGER_LEVEL.DEBUG, console.debug, ...msgs); | ||||
|     } | ||||
|     info(...msgs) { | ||||
|         this.log(LOGGER_LEVEL.INFO, console.info, ...msgs); | ||||
|     } | ||||
|     warn(...msgs) { | ||||
|         this.log(LOGGER_LEVEL.WARN, console.info, ...msgs); | ||||
|     } | ||||
|     error(...msgs) { | ||||
|         this.log(LOGGER_LEVEL.ERROR, console.info, ...msgs); | ||||
|     } | ||||
|     notify(...msgs) { | ||||
|         let msg = msgs.join(' '); | ||||
|         if (!this._notificationId) { | ||||
|             chrome.notifications.create( | ||||
|                 null, | ||||
|                 { | ||||
|                     "type": "basic", | ||||
|                     "iconUrl": chrome.extension.getURL('icon.png'), | ||||
|                     "title": "Data Extractor", | ||||
|                     "message": msg, | ||||
|                     "priority": 0, | ||||
|                     "requireInteraction": true, | ||||
|                 }, | ||||
|                 notificationId => { | ||||
|                     this._notificationId = notificationId; | ||||
|                 } | ||||
|             ); | ||||
|             return; | ||||
|         } | ||||
|         chrome.notifications.update( | ||||
|             this._notificationId, | ||||
|             { "message": msg } | ||||
|         ); | ||||
|     } | ||||
| } | ||||
|  | ||||
| export const logger = new Logger(LOGGER_LEVEL.DEBUG, LOGGER_LEVEL.DISABLED); | ||||
| @ -1,6 +1,6 @@ | ||||
| import { Request, Actions, Response } from "../common"; | ||||
| import { getTabByID } from "./actions"; | ||||
| import { logger } from "./logger"; | ||||
| import { logger } from "../common/logger"; | ||||
|  | ||||
|  | ||||
| export type ResponseCheckerSync<T> = (r: Response<T>, err: chrome.runtime.LastError, count: number) => T; | ||||
|  | ||||
| @ -26,6 +26,7 @@ export class ExtractResult { | ||||
|                 } | ||||
|                 let line = lineCells.reduce( | ||||
|                     (lineText, cell, idx) => { | ||||
|                         cell = cell || ""; | ||||
|                         cell = '"' + cell.trim().replace(/"/g, '""') + '"'; | ||||
|                         return lineText + cell + (idx == lineCells.length - 1 ? "" : ",") | ||||
|                     }, ""); | ||||
|  | ||||
| @ -4,7 +4,7 @@ import { testArgs, signitures } from "./signiture"; | ||||
| import { ExtractResult } from "./result"; | ||||
| import { messageSubscribers, ActionSubscriber } from "./messaging"; | ||||
| import { Actions } from "../common"; | ||||
| import { logger } from "./logger"; | ||||
| import { logger } from "../common/logger"; | ||||
|  | ||||
| export class Task { | ||||
|     private _data: { [key: string]: string[][] } = {}; | ||||
| @ -87,7 +87,7 @@ export class Task { | ||||
|             logger.info(`Watcher #${taskID} starts.`); | ||||
|             let pm = this.makeOptionalTasks(sender.tab); | ||||
|             return pm.then( | ||||
|                 () => extractTabData(sender.tab, this._itemsSelector, this._fieldSelectors, sender.tab.url, false) | ||||
|                 () => extractTabData(sender.tab, this._itemsSelector, this._fieldSelectors, sender.tab.url, true) | ||||
|             ).then( | ||||
|                 results => { | ||||
|                     if (results && results.length) { | ||||
|  | ||||
		Reference in New Issue
	
	Block a user