From dacfc41cda52204533521bde1f94fedc03fc3464 Mon Sep 17 00:00:00 2001 From: kawamataryo Date: Mon, 30 Dec 2024 21:51:39 +0900 Subject: [PATCH] refactor: searchUserCell --- package-lock.json | 4 ++-- src/lib/domHelpers.ts | 15 +++++++++++++++ src/lib/services/instagramService.ts | 16 +--------------- src/lib/services/threadsService.ts | 16 +--------------- src/lib/services/tikTokService.ts | 18 ++---------------- 5 files changed, 21 insertions(+), 48 deletions(-) diff --git a/package-lock.json b/package-lock.json index 5db4239..45fc698 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "sky-follower-bridge", - "version": "2.3.2", + "version": "2.4.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "sky-follower-bridge", - "version": "2.3.2", + "version": "2.4.0", "dependencies": { "@atproto/api": "^0.13.12", "@changesets/cli": "^2.27.1", diff --git a/src/lib/domHelpers.ts b/src/lib/domHelpers.ts index ab1bcf0..7e604ba 100644 --- a/src/lib/domHelpers.ts +++ b/src/lib/domHelpers.ts @@ -28,3 +28,18 @@ export const scrapeListNameFromPage = (): string => { } return "Imported List from X"; }; + +export const searchUserCells = (userCell: HTMLElement): HTMLElement[] => { + if (!userCell) { + return []; + } + const cellTextCount = (userCell.innerText ?? "").split("\n").length; + const hasAvatar = !!userCell.querySelector("img"); + if (1 <= cellTextCount && cellTextCount <= 3 && hasAvatar) { + return [userCell]; + } + if (userCell.children.length === 0) { + return []; + } + return Array.from(userCell.children).flatMap(searchUserCells); +}; diff --git a/src/lib/services/instagramService.ts b/src/lib/services/instagramService.ts index e0ef9c6..cbacdbe 100644 --- a/src/lib/services/instagramService.ts +++ b/src/lib/services/instagramService.ts @@ -1,23 +1,9 @@ +import { searchUserCells } from "~lib/domHelpers"; import { findFirstScrollableElements } from "~lib/utils"; import type { CrawledUserInfo, IService, MessageName } from "~types"; const SCROLL_TARGET_SELECTOR = '[role="dialog"]'; -const searchUserCells = (userCell: HTMLElement): HTMLElement[] => { - if (!userCell) { - return []; - } - const cellTextCount = (userCell.innerText ?? "").split("\n").length; - const hasAvatar = !!userCell.querySelector("img"); - if (1 <= cellTextCount && cellTextCount <= 3 && hasAvatar) { - return [userCell]; - } - if (userCell.children.length === 0) { - return []; - } - return Array.from(userCell.children).flatMap(searchUserCells); -}; - export class InstagramService implements IService { messageName: MessageName; crawledUserCells: Set; diff --git a/src/lib/services/threadsService.ts b/src/lib/services/threadsService.ts index e87537b..eed6930 100644 --- a/src/lib/services/threadsService.ts +++ b/src/lib/services/threadsService.ts @@ -1,24 +1,10 @@ +import { searchUserCells } from "~lib/domHelpers"; import { findFirstScrollableElements } from "~lib/utils"; import type { CrawledUserInfo, IService, MessageName } from "~types"; const TARGET_PAGE_SELECTOR = '[role="dialog"] [role="tab"]>[role="button"]'; const SCROLL_TARGET_SELECTOR = '[role="dialog"]'; -const searchUserCells = (userCell: HTMLElement): HTMLElement[] => { - if (!userCell) { - return []; - } - const cellTextCount = (userCell.innerText ?? "").split("\n").length; - const hasAvatar = !!userCell.querySelector("img"); - if (1 <= cellTextCount && cellTextCount <= 3 && hasAvatar) { - return [userCell]; - } - if (userCell.children.length === 0) { - return []; - } - return Array.from(userCell.children).flatMap(searchUserCells); -}; - export class ThreadsService implements IService { messageName: MessageName; crawledUserCells: Set; diff --git a/src/lib/services/tikTokService.ts b/src/lib/services/tikTokService.ts index 9e83c8e..d9b04d9 100644 --- a/src/lib/services/tikTokService.ts +++ b/src/lib/services/tikTokService.ts @@ -1,23 +1,9 @@ +import { searchUserCells } from "~lib/domHelpers"; import { findFirstScrollableElements } from "~lib/utils"; import type { CrawledUserInfo, IService, MessageName } from "~types"; const SCROLL_TARGET_SELECTOR = '[data-e2e="follow-info-popup"]'; -const searchUserCells = (userCell: HTMLElement): HTMLElement[] => { - if (!userCell) { - return []; - } - const cellTextCount = (userCell.innerText ?? "").split("\n").length; - const hasAvatar = !!userCell.querySelector("img"); - if (1 <= cellTextCount && cellTextCount <= 3 && hasAvatar) { - return [userCell]; - } - if (userCell.children.length === 0) { - return []; - } - return Array.from(userCell.children).flatMap(searchUserCells); -}; - export class TikTokService implements IService { messageName: MessageName; crawledUserCells: Set; @@ -72,7 +58,7 @@ export class TikTokService implements IService { displayName, accountNameRemoveUnderscore, accountNameReplaceUnderscore, - bskyHandle: "", + bskyHandleInDescription: "", originalAvatar: avatarSrc, originalProfileLink: `https://www.tiktok.com/@${_accountName}`, };