From 986488fd4d91ea6ef2b7382cb441c9d627438583 Mon Sep 17 00:00:00 2001 From: johnathan <952508490@qq.com> Date: Fri, 18 Apr 2025 17:08:08 +0800 Subject: [PATCH] refactor: update function names and improve singleton pattern for AmazonPageWorker --- src/background/main.ts | 2 +- src/logic/execute-script.ts | 4 ++-- src/logic/page-worker/index.ts | 25 +++++++++++++++++-------- src/sidepanel/Sidepanel.vue | 4 ++-- 4 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/background/main.ts b/src/background/main.ts index 4886fb3..62986c5 100644 --- a/src/background/main.ts +++ b/src/background/main.ts @@ -17,7 +17,7 @@ if (USE_SIDE_PANEL) { .catch((error: unknown) => console.error(error)); } -browser.runtime.onInstalled.addListener((): void => { +browser.runtime.onInstalled.addListener(() => { // eslint-disable-next-line no-console console.log('Azon Seeker installed'); }); diff --git a/src/logic/execute-script.ts b/src/logic/execute-script.ts index b32620b..91f6197 100644 --- a/src/logic/execute-script.ts +++ b/src/logic/execute-script.ts @@ -1,10 +1,10 @@ /** - * + * Execute Script on Document * @param tabId * @param func * @returns */ -export async function executeScript(tabId: number, func: () => Promise): Promise { +export async function exec(tabId: number, func: () => Promise): Promise { const injectResults = await browser.scripting.executeScript({ target: { tabId }, func, diff --git a/src/logic/page-worker/index.ts b/src/logic/page-worker/index.ts index eac5949..692a086 100644 --- a/src/logic/page-worker/index.ts +++ b/src/logic/page-worker/index.ts @@ -1,9 +1,18 @@ import Emittery from 'emittery'; import type { AmazonGoodsLinkItem, AmazonPageWorker, AmazonPageWorkerEvents } from './types'; import Browser from 'webextension-polyfill'; -import { executeScript } from '../execute-script'; +import { exec } from '../execute-script'; class AmazonPageWorkerImpl implements AmazonPageWorker { + private static _instance: AmazonPageWorker | null = null; + public static getInstance() { + if (this._instance === null) { + this._instance = new AmazonPageWorkerImpl(); + } + return this._instance; + } + private constructor() {} + readonly channel = new Emittery(); public async doSearch(keywords: string): Promise { @@ -24,7 +33,7 @@ class AmazonPageWorkerImpl implements AmazonPageWorker { private async wanderSearchSinglePage(tab: Browser.Tabs.Tab) { const tabId = tab.id!; // #region Wait for the Next button to appear, indicating that the product items have finished loading - await executeScript(tabId, async () => { + await exec(tabId, async () => { await new Promise((resolve) => setTimeout(resolve, 500 + ~~(500 * Math.random()))); while (!document.querySelector('.s-pagination-strip')) { window.scrollBy(0, ~~(Math.random() * 500) + 500); @@ -33,7 +42,7 @@ class AmazonPageWorkerImpl implements AmazonPageWorker { }); // #endregion // #region Determine the type of product search page https://github.com/primedigitaltech/azon_seeker/issues/1 - const pagePattern = await executeScript(tabId, async () => { + const pagePattern = await exec(tabId, async () => { return [ ...(document.querySelectorAll( '.a-section.a-spacing-small.puis-padding-left-small', @@ -52,7 +61,7 @@ class AmazonPageWorkerImpl implements AmazonPageWorker { switch (pagePattern) { // 处理商品以列表形式展示的情况 case 'pattern-1': - data = await executeScript(tabId, async () => { + data = await exec(tabId, async () => { const items = [ ...(document.querySelectorAll( '.a-section.a-spacing-small.a-spacing-top-small:not(.a-text-right)', @@ -71,7 +80,7 @@ class AmazonPageWorkerImpl implements AmazonPageWorker { break; // 处理商品以二维图片格展示的情况 case 'pattern-2': - data = await executeScript(tabId, async () => { + data = await exec(tabId, async () => { const items = [ ...(document.querySelectorAll( '.a-section.a-spacing-small.puis-padding-left-small', @@ -91,7 +100,7 @@ class AmazonPageWorkerImpl implements AmazonPageWorker { } // #endregion // #region Determine if it is the last page, otherwise navigate to the next page - const hasNextPage = await executeScript(tabId, async () => { + const hasNextPage = await exec(tabId, async () => { const nextButton = document.querySelector('.s-pagination-next'); if (nextButton) { if (!nextButton.classList.contains('s-pagination-disabled')) { @@ -134,8 +143,8 @@ class AmazonPageWorkerImpl implements AmazonPageWorker { } class PageWorkerFactory { - public createAmazonPageWorker(): AmazonPageWorker { - return new AmazonPageWorkerImpl(); + public useAmazonPageWorker(): AmazonPageWorker { + return AmazonPageWorkerImpl.getInstance(); } } diff --git a/src/sidepanel/Sidepanel.vue b/src/sidepanel/Sidepanel.vue index 4cc3b87..f04cc57 100644 --- a/src/sidepanel/Sidepanel.vue +++ b/src/sidepanel/Sidepanel.vue @@ -5,7 +5,7 @@ import type { AmazonGoodsLinkItem } from '~/logic/page-worker/types'; import { NButton, type DataTableColumns } from 'naive-ui'; const message = useMessage(); -const worker = pageWorker.createAmazonPageWorker(); +const worker = pageWorker.useAmazonPageWorker(); type TableData = AmazonGoodsLinkItem & { rank: number }; @@ -36,7 +36,7 @@ const columns: DataTableColumns = [ active: true, currentWindow: true, }) - .then((ts) => ts.pop()); + .then((tabs) => tabs[0]); if (tab) { await browser.tabs.update(tab.id, { url: row.link,