refactor
This commit is contained in:
@ -1,6 +1,6 @@
|
|||||||
import { Actions, Request } from "../common";
|
import { Actions, Request } from "../common";
|
||||||
import { sendMessage, ResponseChecker } from "./messaging";
|
import { sendMessage, ResponseChecker } from "./messaging";
|
||||||
import { logger } from "./logger";
|
import { logger } from "../common/logger";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* redirect tab to url.
|
* redirect tab to url.
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import { logger } from "./logger";
|
import { logger } from "../common/logger";
|
||||||
import { Actions } from "../common";
|
import { Actions } from "../common";
|
||||||
import { messageSubscribers } from "./messaging";
|
import { messageSubscribers } from "./messaging";
|
||||||
|
|
||||||
|
|||||||
@ -2,7 +2,7 @@ import { Task } from "./task";
|
|||||||
import { saveFile } from "./tools";
|
import { saveFile } from "./tools";
|
||||||
import { createTab, getActiveTab, ping } from "./actions";
|
import { createTab, getActiveTab, ping } from "./actions";
|
||||||
import { ExtractResult } from "./result";
|
import { ExtractResult } from "./result";
|
||||||
import { logger } from "./logger";
|
import { logger } from "../common/logger";
|
||||||
import { caches } from "./caches";
|
import { caches } from "./caches";
|
||||||
|
|
||||||
export class Extractor {
|
export class Extractor {
|
||||||
@ -12,6 +12,14 @@ export class Extractor {
|
|||||||
constructor(options?) {
|
constructor(options?) {
|
||||||
if (options) this._options = 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.
|
* Save current state, in case we restore it later.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
import { Request, Actions, Response } from "../common";
|
import { Request, Actions, Response } from "../common";
|
||||||
import { getTabByID } from "./actions";
|
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;
|
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(
|
let line = lineCells.reduce(
|
||||||
(lineText, cell, idx) => {
|
(lineText, cell, idx) => {
|
||||||
|
cell = cell || "";
|
||||||
cell = '"' + cell.trim().replace(/"/g, '""') + '"';
|
cell = '"' + cell.trim().replace(/"/g, '""') + '"';
|
||||||
return lineText + cell + (idx == lineCells.length - 1 ? "" : ",")
|
return lineText + cell + (idx == lineCells.length - 1 ? "" : ",")
|
||||||
}, "");
|
}, "");
|
||||||
|
|||||||
@ -4,7 +4,7 @@ import { testArgs, signitures } from "./signiture";
|
|||||||
import { ExtractResult } from "./result";
|
import { ExtractResult } from "./result";
|
||||||
import { messageSubscribers, ActionSubscriber } from "./messaging";
|
import { messageSubscribers, ActionSubscriber } from "./messaging";
|
||||||
import { Actions } from "../common";
|
import { Actions } from "../common";
|
||||||
import { logger } from "./logger";
|
import { logger } from "../common/logger";
|
||||||
|
|
||||||
export class Task {
|
export class Task {
|
||||||
private _data: { [key: string]: string[][] } = {};
|
private _data: { [key: string]: string[][] } = {};
|
||||||
@ -87,7 +87,7 @@ export class Task {
|
|||||||
logger.info(`Watcher #${taskID} starts.`);
|
logger.info(`Watcher #${taskID} starts.`);
|
||||||
let pm = this.makeOptionalTasks(sender.tab);
|
let pm = this.makeOptionalTasks(sender.tab);
|
||||||
return pm.then(
|
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(
|
).then(
|
||||||
results => {
|
results => {
|
||||||
if (results && results.length) {
|
if (results && results.length) {
|
||||||
|
|||||||
@ -13,7 +13,7 @@ export class Logger {
|
|||||||
constructor(logLevel, notifyLevel) {
|
constructor(logLevel, notifyLevel) {
|
||||||
if (logLevel) this._log_level = logLevel;
|
if (logLevel) this._log_level = logLevel;
|
||||||
if (notifyLevel) this._notify_level = notifyLevel;
|
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() {
|
get logLevel() {
|
||||||
return this._log_level;
|
return this._log_level;
|
||||||
Reference in New Issue
Block a user