diff --git a/src/background/actions.ts b/src/background/actions.ts index 5ae145b..70d5226 100644 --- a/src/background/actions.ts +++ b/src/background/actions.ts @@ -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. diff --git a/src/background/caches.ts b/src/background/caches.ts index c51f2ea..e085d6e 100644 --- a/src/background/caches.ts +++ b/src/background/caches.ts @@ -1,4 +1,4 @@ -import { logger } from "./logger"; +import { logger } from "../common/logger"; import { Actions } from "../common"; import { messageSubscribers } from "./messaging"; diff --git a/src/background/extractor.ts b/src/background/extractor.ts index 56a5a81..6eaac2d 100644 --- a/src/background/extractor.ts +++ b/src/background/extractor.ts @@ -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. */ diff --git a/src/background/messaging.ts b/src/background/messaging.ts index d5c3357..06b7eaa 100644 --- a/src/background/messaging.ts +++ b/src/background/messaging.ts @@ -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 = (r: Response, err: chrome.runtime.LastError, count: number) => T; diff --git a/src/background/result.ts b/src/background/result.ts index b3e795a..75cd840 100644 --- a/src/background/result.ts +++ b/src/background/result.ts @@ -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 ? "" : ",") }, ""); diff --git a/src/background/task.ts b/src/background/task.ts index 93230c5..be49c36 100644 --- a/src/background/task.ts +++ b/src/background/task.ts @@ -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) { diff --git a/src/common.ts b/src/common/index.ts similarity index 100% rename from src/common.ts rename to src/common/index.ts diff --git a/src/background/logger.ts b/src/common/logger.ts similarity index 94% rename from src/background/logger.ts rename to src/common/logger.ts index 4f2ff68..2c32c38 100644 --- a/src/background/logger.ts +++ b/src/common/logger.ts @@ -13,7 +13,7 @@ export class Logger { 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 }); + if (chrome.notifications) chrome.notifications.onClosed.addListener((id, byUser) => { this._notify_level = undefined }); } get logLevel() { return this._log_level;