From 3338f78d9103d851146b0b8fe0d0d07bbe0016f6 Mon Sep 17 00:00:00 2001 From: jebbs Date: Wed, 15 Jan 2020 15:21:17 +0800 Subject: [PATCH] code optimize --- src/background/actions.ts | 14 +++++++------- src/background/messaging.ts | 11 ++++------- src/common.ts | 28 ++++++++++++++++++---------- src/content/index.ts | 20 ++++++++++---------- src/popup/index.ts | 8 ++++---- 5 files changed, 43 insertions(+), 38 deletions(-) diff --git a/src/background/actions.ts b/src/background/actions.ts index aa57631..cc1b0e9 100644 --- a/src/background/actions.ts +++ b/src/background/actions.ts @@ -1,4 +1,4 @@ -import { ACTION_GOTO_URL, ACTION_EXTRACT, ACTION_PING as ACTION_PING, ACTION_QUERY_URL, ACTION_SCROLL_BOTTOM } from "../common"; +import { Actions, Request } from "../common"; import { sendMessage } from "./messaging"; /** @@ -10,8 +10,8 @@ import { sendMessage } from "./messaging"; export function redirectTab(tab: chrome.tabs.Tab, url: string) { return queryUrl(tab).then(u => { if (url !== u) { - let req = { - action: ACTION_GOTO_URL, + let req: Request = { + action: Actions.GOTO_URL, url: url } let checker = async (u, err, tryCount): Promise => { @@ -43,7 +43,7 @@ export function redirectTab(tab: chrome.tabs.Tab, url: string) { */ export function extractTabData(tab, itemsSelector, fieldSelectors) { let req = { - action: ACTION_EXTRACT, + action: Actions.EXTRACT, itemsSelector: itemsSelector, fieldSelectors: fieldSelectors } @@ -67,7 +67,7 @@ export function extractTabData(tab, itemsSelector, fieldSelectors) { */ export async function ping(tab, count = 1) { let req = { - action: ACTION_PING + action: Actions.PING } let checker = (r: string, e, c) => r == "pong" ? r : undefined; let pong = await sendMessage(tab, req, 'Check tab availability...', checker, 1000, count).catch(() => { }); @@ -81,7 +81,7 @@ export async function ping(tab, count = 1) { */ export function queryUrl(tab: chrome.tabs.Tab) { let req = { - action: ACTION_QUERY_URL + action: Actions.QUERY_URL } return sendMessage(tab, req); } @@ -94,7 +94,7 @@ export function queryUrl(tab: chrome.tabs.Tab) { */ export function scrollToBottom(tab: chrome.tabs.Tab) { let req = { - action: ACTION_SCROLL_BOTTOM + action: Actions.SCROLL_BOTTOM } return sendMessage(tab, req, 'Scroll to page bottom...'); } diff --git a/src/background/messaging.ts b/src/background/messaging.ts index a49c8cb..5cec0f0 100644 --- a/src/background/messaging.ts +++ b/src/background/messaging.ts @@ -1,4 +1,4 @@ -import { EXT_NAME, ACTION_UPLOAD_STATE } from "../common"; +import { Request, Actions } from "../common"; import { getTabByID } from "./actions"; import { caches, logger } from "./common"; @@ -78,14 +78,11 @@ export function sendMessage( }); } -chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) { - if (!request.action || !request.action.startsWith(EXT_NAME)) { - return; - } +chrome.runtime.onMessage.addListener(function (request: Request, sender, sendResponse) { switch (request.action) { - case ACTION_UPLOAD_STATE: + case Actions.UPLOAD_STATE: sendResponse('recieved!'); - caches.setState(request.name, request.state) + caches.setState(request.fileName, request.state) break; default: sendResponse("Request not supported."); diff --git a/src/common.ts b/src/common.ts index 88b2922..421f040 100644 --- a/src/common.ts +++ b/src/common.ts @@ -1,11 +1,19 @@ +export enum Actions { + EXTRACT = 1, + GOTO_URL, + PING, + QUERY_URL, + SCROLL_BOTTOM, + UPLOAD_STATE, + SLEEP, + WAKEUP, +} -export const EXT_NAME = "DataExtracter"; - -export const ACTION_EXTRACT = `${EXT_NAME}:Extract`; -export const ACTION_GOTO_URL = `${EXT_NAME}:GoToTUL`; -export const ACTION_PING = `${EXT_NAME}:ReportIn`; -export const ACTION_QUERY_URL = `${EXT_NAME}:QueryURL`; -export const ACTION_SCROLL_BOTTOM = `${EXT_NAME}:ScrollToBottom`; -export const ACTION_UPLOAD_STATE = `${EXT_NAME}:UploadStateFile`; -export const ACTION_SLEEP = `${EXT_NAME}:Sleep`; -export const ACTION_WAKEUP = `${EXT_NAME}:WakeUp`; +export interface Request { + action: Actions + itemsSelector?: string + fieldSelectors?: string[] + url?: string + fileName?: string + state?: string +} \ No newline at end of file diff --git a/src/content/index.ts b/src/content/index.ts index ad68c26..0d00a28 100644 --- a/src/content/index.ts +++ b/src/content/index.ts @@ -1,11 +1,11 @@ -import { ACTION_WAKEUP, ACTION_EXTRACT, ACTION_GOTO_URL, ACTION_PING, ACTION_QUERY_URL, ACTION_SCROLL_BOTTOM, ACTION_SLEEP } from '../common'; +import { Request, Actions } from '../common'; import { scrollToBottom, extract } from './actions'; let asleep = false; chrome.runtime.onMessage.addListener( function (request, sender: chrome.runtime.MessageSender, sendResponse: (r: any) => void) { if (!request.action) return; - if (asleep && ACTION_WAKEUP != request.action) { + if (asleep && Actions.WAKEUP != request.action) { sendResponse && sendResponse(undefined); return; } @@ -16,26 +16,26 @@ chrome.runtime.onMessage.addListener( } ); -async function doAction(request: any, sender: chrome.runtime.MessageSender) { +async function doAction(request: Request, sender: chrome.runtime.MessageSender) { switch (request.action) { - case ACTION_EXTRACT: + case Actions.EXTRACT: let data = extract(request.itemsSelector, request.fieldSelectors); return data; - case ACTION_GOTO_URL: + case Actions.GOTO_URL: window.location.replace(request.url); // should not recieve any request until the page & script reload asleep = true; return request.url; - case ACTION_PING: + case Actions.PING: return "pong"; - case ACTION_QUERY_URL: + case Actions.QUERY_URL: return window.location.href; - case ACTION_SCROLL_BOTTOM: + case Actions.SCROLL_BOTTOM: return scrollToBottom(); - case ACTION_SLEEP: + case Actions.SLEEP: asleep = true; return "Content script is sleeping."; - case ACTION_WAKEUP: + case Actions.WAKEUP: asleep = false; return "Content script is available."; default: diff --git a/src/popup/index.ts b/src/popup/index.ts index 241ddd4..80efcb9 100644 --- a/src/popup/index.ts +++ b/src/popup/index.ts @@ -1,4 +1,4 @@ -import { ACTION_UPLOAD_STATE } from '../common'; +import { Request, Actions } from '../common'; window.onload = function () { document.querySelector('#link-extension-detail') @@ -21,10 +21,10 @@ window.onload = function () { reader.readAsText(this.files[0], "UTF-8"); reader.onload = function (evt) { var fileString = evt.target.result; - chrome.runtime.sendMessage({ - action: ACTION_UPLOAD_STATE, + chrome.runtime.sendMessage({ + action: Actions.UPLOAD_STATE, state: fileString, - name: fileName + fileName: fileName }, r => { if (r) console.log('State sent:', r); });