mirror of
https://github.com/primedigitaltech/azon_seeker.git
synced 2026-02-06 15:27:02 +08:00
refactor: update function names and improve singleton pattern for AmazonPageWorker
This commit is contained in:
parent
28dac88428
commit
986488fd4d
@ -17,7 +17,7 @@ if (USE_SIDE_PANEL) {
|
|||||||
.catch((error: unknown) => console.error(error));
|
.catch((error: unknown) => console.error(error));
|
||||||
}
|
}
|
||||||
|
|
||||||
browser.runtime.onInstalled.addListener((): void => {
|
browser.runtime.onInstalled.addListener(() => {
|
||||||
// eslint-disable-next-line no-console
|
// eslint-disable-next-line no-console
|
||||||
console.log('Azon Seeker installed');
|
console.log('Azon Seeker installed');
|
||||||
});
|
});
|
||||||
|
|||||||
@ -1,10 +1,10 @@
|
|||||||
/**
|
/**
|
||||||
*
|
* Execute Script on Document
|
||||||
* @param tabId
|
* @param tabId
|
||||||
* @param func
|
* @param func
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
export async function executeScript<T>(tabId: number, func: () => Promise<T>): Promise<T | null> {
|
export async function exec<T>(tabId: number, func: () => Promise<T>): Promise<T | null> {
|
||||||
const injectResults = await browser.scripting.executeScript({
|
const injectResults = await browser.scripting.executeScript({
|
||||||
target: { tabId },
|
target: { tabId },
|
||||||
func,
|
func,
|
||||||
|
|||||||
@ -1,9 +1,18 @@
|
|||||||
import Emittery from 'emittery';
|
import Emittery from 'emittery';
|
||||||
import type { AmazonGoodsLinkItem, AmazonPageWorker, AmazonPageWorkerEvents } from './types';
|
import type { AmazonGoodsLinkItem, AmazonPageWorker, AmazonPageWorkerEvents } from './types';
|
||||||
import Browser from 'webextension-polyfill';
|
import Browser from 'webextension-polyfill';
|
||||||
import { executeScript } from '../execute-script';
|
import { exec } from '../execute-script';
|
||||||
|
|
||||||
class AmazonPageWorkerImpl implements AmazonPageWorker {
|
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<AmazonPageWorkerEvents>();
|
readonly channel = new Emittery<AmazonPageWorkerEvents>();
|
||||||
|
|
||||||
public async doSearch(keywords: string): Promise<string> {
|
public async doSearch(keywords: string): Promise<string> {
|
||||||
@ -24,7 +33,7 @@ class AmazonPageWorkerImpl implements AmazonPageWorker {
|
|||||||
private async wanderSearchSinglePage(tab: Browser.Tabs.Tab) {
|
private async wanderSearchSinglePage(tab: Browser.Tabs.Tab) {
|
||||||
const tabId = tab.id!;
|
const tabId = tab.id!;
|
||||||
// #region Wait for the Next button to appear, indicating that the product items have finished loading
|
// #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())));
|
await new Promise((resolve) => setTimeout(resolve, 500 + ~~(500 * Math.random())));
|
||||||
while (!document.querySelector('.s-pagination-strip')) {
|
while (!document.querySelector('.s-pagination-strip')) {
|
||||||
window.scrollBy(0, ~~(Math.random() * 500) + 500);
|
window.scrollBy(0, ~~(Math.random() * 500) + 500);
|
||||||
@ -33,7 +42,7 @@ class AmazonPageWorkerImpl implements AmazonPageWorker {
|
|||||||
});
|
});
|
||||||
// #endregion
|
// #endregion
|
||||||
// #region Determine the type of product search page https://github.com/primedigitaltech/azon_seeker/issues/1
|
// #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 [
|
return [
|
||||||
...(document.querySelectorAll<HTMLDivElement>(
|
...(document.querySelectorAll<HTMLDivElement>(
|
||||||
'.a-section.a-spacing-small.puis-padding-left-small',
|
'.a-section.a-spacing-small.puis-padding-left-small',
|
||||||
@ -52,7 +61,7 @@ class AmazonPageWorkerImpl implements AmazonPageWorker {
|
|||||||
switch (pagePattern) {
|
switch (pagePattern) {
|
||||||
// 处理商品以列表形式展示的情况
|
// 处理商品以列表形式展示的情况
|
||||||
case 'pattern-1':
|
case 'pattern-1':
|
||||||
data = await executeScript(tabId, async () => {
|
data = await exec(tabId, async () => {
|
||||||
const items = [
|
const items = [
|
||||||
...(document.querySelectorAll<HTMLDivElement>(
|
...(document.querySelectorAll<HTMLDivElement>(
|
||||||
'.a-section.a-spacing-small.a-spacing-top-small:not(.a-text-right)',
|
'.a-section.a-spacing-small.a-spacing-top-small:not(.a-text-right)',
|
||||||
@ -71,7 +80,7 @@ class AmazonPageWorkerImpl implements AmazonPageWorker {
|
|||||||
break;
|
break;
|
||||||
// 处理商品以二维图片格展示的情况
|
// 处理商品以二维图片格展示的情况
|
||||||
case 'pattern-2':
|
case 'pattern-2':
|
||||||
data = await executeScript(tabId, async () => {
|
data = await exec(tabId, async () => {
|
||||||
const items = [
|
const items = [
|
||||||
...(document.querySelectorAll<HTMLDivElement>(
|
...(document.querySelectorAll<HTMLDivElement>(
|
||||||
'.a-section.a-spacing-small.puis-padding-left-small',
|
'.a-section.a-spacing-small.puis-padding-left-small',
|
||||||
@ -91,7 +100,7 @@ class AmazonPageWorkerImpl implements AmazonPageWorker {
|
|||||||
}
|
}
|
||||||
// #endregion
|
// #endregion
|
||||||
// #region Determine if it is the last page, otherwise navigate to the next page
|
// #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<HTMLLinkElement>('.s-pagination-next');
|
const nextButton = document.querySelector<HTMLLinkElement>('.s-pagination-next');
|
||||||
if (nextButton) {
|
if (nextButton) {
|
||||||
if (!nextButton.classList.contains('s-pagination-disabled')) {
|
if (!nextButton.classList.contains('s-pagination-disabled')) {
|
||||||
@ -134,8 +143,8 @@ class AmazonPageWorkerImpl implements AmazonPageWorker {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class PageWorkerFactory {
|
class PageWorkerFactory {
|
||||||
public createAmazonPageWorker(): AmazonPageWorker {
|
public useAmazonPageWorker(): AmazonPageWorker {
|
||||||
return new AmazonPageWorkerImpl();
|
return AmazonPageWorkerImpl.getInstance();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -5,7 +5,7 @@ import type { AmazonGoodsLinkItem } from '~/logic/page-worker/types';
|
|||||||
import { NButton, type DataTableColumns } from 'naive-ui';
|
import { NButton, type DataTableColumns } from 'naive-ui';
|
||||||
|
|
||||||
const message = useMessage();
|
const message = useMessage();
|
||||||
const worker = pageWorker.createAmazonPageWorker();
|
const worker = pageWorker.useAmazonPageWorker();
|
||||||
|
|
||||||
type TableData = AmazonGoodsLinkItem & { rank: number };
|
type TableData = AmazonGoodsLinkItem & { rank: number };
|
||||||
|
|
||||||
@ -36,7 +36,7 @@ const columns: DataTableColumns<TableData> = [
|
|||||||
active: true,
|
active: true,
|
||||||
currentWindow: true,
|
currentWindow: true,
|
||||||
})
|
})
|
||||||
.then((ts) => ts.pop());
|
.then((tabs) => tabs[0]);
|
||||||
if (tab) {
|
if (tab) {
|
||||||
await browser.tabs.update(tab.id, {
|
await browser.tabs.update(tab.id, {
|
||||||
url: row.link,
|
url: row.link,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user