Skip to content

Commit

Permalink
refactor: searchUserCell
Browse files Browse the repository at this point in the history
  • Loading branch information
kawamataryo committed Jan 5, 2025
1 parent 3be16f4 commit dacfc41
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 48 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions src/lib/domHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
16 changes: 1 addition & 15 deletions src/lib/services/instagramService.ts
Original file line number Diff line number Diff line change
@@ -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<HTMLElement>;
Expand Down
16 changes: 1 addition & 15 deletions src/lib/services/threadsService.ts
Original file line number Diff line number Diff line change
@@ -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<HTMLElement>;
Expand Down
18 changes: 2 additions & 16 deletions src/lib/services/tikTokService.ts
Original file line number Diff line number Diff line change
@@ -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<HTMLElement>;
Expand Down Expand Up @@ -72,7 +58,7 @@ export class TikTokService implements IService {
displayName,
accountNameRemoveUnderscore,
accountNameReplaceUnderscore,
bskyHandle: "",
bskyHandleInDescription: "",
originalAvatar: avatarSrc,
originalProfileLink: `https://www.tiktok.com/@${_accountName}`,
};
Expand Down

0 comments on commit dacfc41

Please sign in to comment.