code optimize
This commit is contained in:
@ -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<string> => {
|
||||
@ -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<string>(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<string>(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...');
|
||||
}
|
||||
|
||||
@ -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<T>(
|
||||
});
|
||||
}
|
||||
|
||||
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.");
|
||||
|
||||
@ -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
|
||||
}
|
||||
@ -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:
|
||||
|
||||
@ -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(<Request>{
|
||||
action: Actions.UPLOAD_STATE,
|
||||
state: fileString,
|
||||
name: fileName
|
||||
fileName: fileName
|
||||
}, r => {
|
||||
if (r) console.log('State sent:', r);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user