fix not found el

This commit is contained in:
Admin 2026-04-24 13:37:23 +07:00
parent d96884ab75
commit 56744403c6
1 changed files with 24 additions and 25 deletions

View File

@ -2,15 +2,13 @@
import { messageApi } from "@/api/message-api.service"; import { messageApi } from "@/api/message-api.service";
export class TeamsChatService { export class TeamsChatService {
private readonly MY_NAME = "Apactech com"; // private readonly MY_NAME = "Apactech com";
public lastMessage?: IMessage; public lastMessage?: IMessage;
public initialHistories: IMessage[] = []; public initialHistories: IMessage[] = [];
public elTags = { public elTags = {
container_scroll: container_scroll: '//*[@data-testid="simple-collab-rail"]',
"/html/body/div[1]/div/div/div/div[5]/div[1]/div[1]/div[2]/div[1]/div[1]/div", conatainer_conversations: '//*[@data-testid="simple-collab-dnd-rail"]',
conatainer_conversations:
"/html/body/div[1]/div/div/div/div[5]/div[1]/div[1]/div[2]/div[1]/div[1]/div/div[1]",
container_chat: '[data-testid="message-wrapper"]', container_chat: '[data-testid="message-wrapper"]',
root_id: '[aria-selected="true"] [id^="chat-list-item"]', root_id: '[aria-selected="true"] [id^="chat-list-item"]',
@ -24,13 +22,13 @@ export class TeamsChatService {
private _getImageFormEl(el: HTMLElement): HTMLImageElement[] { private _getImageFormEl(el: HTMLElement): HTMLImageElement[] {
// Tìm tất cả img có data-gallery-src trong el // Tìm tất cả img có data-gallery-src trong el
let sharedImages = Array.from( let sharedImages = Array.from(
el.querySelectorAll("img[data-gallery-src]") el.querySelectorAll("img[data-gallery-src]"),
) as HTMLImageElement[]; ) as HTMLImageElement[];
// Nếu không tìm thấy thì thử tìm trong parentElement // Nếu không tìm thấy thì thử tìm trong parentElement
if (sharedImages.length === 0 && el.parentElement) { if (sharedImages.length === 0 && el.parentElement) {
sharedImages = Array.from( sharedImages = Array.from(
el.parentElement.querySelectorAll("img[data-gallery-src]") el.parentElement.querySelectorAll("img[data-gallery-src]"),
) as HTMLImageElement[]; ) as HTMLImageElement[];
} }
@ -174,7 +172,7 @@ export class TeamsChatService {
// Lấy emoji // Lấy emoji
const emojiImgs = Array.from( const emojiImgs = Array.from(
el.querySelectorAll("img[itemtype]") el.querySelectorAll("img[itemtype]"),
) as HTMLImageElement[]; ) as HTMLImageElement[];
const emojiAlts = emojiImgs const emojiAlts = emojiImgs
.map((img) => img.getAttribute("alt") || "") .map((img) => img.getAttribute("alt") || "")
@ -189,11 +187,11 @@ export class TeamsChatService {
public parseMessageElement(el: Element, isMine = false): IMessage | null { public parseMessageElement(el: Element, isMine = false): IMessage | null {
const timestampEl = el.querySelector( const timestampEl = el.querySelector(
isMine ? ".fui-ChatMyMessage__timestamp" : ".fui-ChatMessage__timestamp" isMine ? ".fui-ChatMyMessage__timestamp" : ".fui-ChatMessage__timestamp",
) as HTMLElement | null; ) as HTMLElement | null;
const authorEl = el.querySelector( const authorEl = el.querySelector(
isMine ? ".fui-ChatMyMessage__author" : ".fui-ChatMessage__author" isMine ? ".fui-ChatMyMessage__author" : ".fui-ChatMessage__author",
) as HTMLElement | null; ) as HTMLElement | null;
if (!timestampEl) return null; if (!timestampEl) return null;
@ -207,7 +205,7 @@ export class TeamsChatService {
: Number(timestampEl.id.replace("timestamp-", "")); : Number(timestampEl.id.replace("timestamp-", ""));
const contentEl = document.querySelector( const contentEl = document.querySelector(
`#content-${dateTime}` `#content-${dateTime}`,
) as HTMLElement | null; ) as HTMLElement | null;
(contentEl as any)["date_time"] = dateTime; (contentEl as any)["date_time"] = dateTime;
@ -242,13 +240,13 @@ export class TeamsChatService {
extractAllMessages(): IMessage[] { extractAllMessages(): IMessage[] {
const myMessages: IMessage[] = Array.from( const myMessages: IMessage[] = Array.from(
document.querySelectorAll(".fui-ChatMyMessage") document.querySelectorAll(".fui-ChatMyMessage"),
) )
.map((el) => this.parseMessageElement(el, true)) .map((el) => this.parseMessageElement(el, true))
.filter((msg): msg is IMessage => msg !== null); .filter((msg): msg is IMessage => msg !== null);
const otherMessages: IMessage[] = Array.from( const otherMessages: IMessage[] = Array.from(
document.querySelectorAll(".fui-ChatMessage") document.querySelectorAll(".fui-ChatMessage"),
) )
.map((el) => this.parseMessageElement(el, false)) .map((el) => this.parseMessageElement(el, false))
.filter((msg): msg is IMessage => msg !== null); .filter((msg): msg is IMessage => msg !== null);
@ -267,7 +265,7 @@ export class TeamsChatService {
const allMessages = this.extractAllMessages(); const allMessages = this.extractAllMessages();
const lastIndex = allMessages.findIndex( const lastIndex = allMessages.findIndex(
(msg) => msg.time === this.lastMessage?.time (msg) => msg.time === this.lastMessage?.time,
); );
const newMessages = allMessages.slice(lastIndex + 1); const newMessages = allMessages.slice(lastIndex + 1);
@ -302,14 +300,14 @@ export class TeamsChatService {
} }
private async _getConversationsInfo( private async _getConversationsInfo(
xpath: string = this.elTags.conatainer_conversations xpath: string = this.elTags.conatainer_conversations,
): Promise<ChatItem[]> { ): Promise<ChatItem[]> {
const result = document.evaluate( const result = document.evaluate(
xpath, xpath,
document, document,
null, null,
XPathResult.FIRST_ORDERED_NODE_TYPE, XPathResult.FIRST_ORDERED_NODE_TYPE,
null null,
).singleNodeValue as HTMLElement | null; ).singleNodeValue as HTMLElement | null;
if (!result) { if (!result) {
@ -321,15 +319,16 @@ export class TeamsChatService {
return []; return [];
} }
// Lọc phần tử con có role="none" const chatItems = Array.from(
const matchedChildren = Array.from(result.children).filter( result.querySelectorAll('[data-item-type="chat"]'),
(child: Element) => {
return child.getAttribute("role") === "none";
}
); );
const data: ChatItem[] = matchedChildren.map((child: Element): ChatItem => { const data: ChatItem[] = chatItems.map((child: Element): ChatItem => {
const id = child.id || null; const treeItemValue =
child.getAttribute("data-fui-tree-item-value") || "";
const lastSegment = treeItemValue.split("/").pop() || "";
const id = lastSegment.includes("|") ? lastSegment.split("|")[1] : null;
const titleId = `title-chat-list-item_${id}`; const titleId = `title-chat-list-item_${id}`;
const titleElement = document.getElementById(titleId); const titleElement = document.getElementById(titleId);
const spanText = titleElement?.innerText || null; const spanText = titleElement?.innerText || null;
@ -360,7 +359,7 @@ export class TeamsChatService {
maxStableRounds?: number; // Số vòng scroll không thay đổi trước khi dừng maxStableRounds?: number; // Số vòng scroll không thay đổi trước khi dừng
delay?: number; // Thời gian chờ giữa mỗi lần scroll (ms) delay?: number; // Thời gian chờ giữa mỗi lần scroll (ms)
maxScrolls?: number; // Giới hạn số lần scroll tối đa maxScrolls?: number; // Giới hạn số lần scroll tối đa
} },
): Promise<void> { ): Promise<void> {
const { const {
maxStableRounds = 5, maxStableRounds = 5,
@ -373,7 +372,7 @@ export class TeamsChatService {
document, document,
null, null,
XPathResult.FIRST_ORDERED_NODE_TYPE, XPathResult.FIRST_ORDERED_NODE_TYPE,
null null,
).singleNodeValue as HTMLElement | null; ).singleNodeValue as HTMLElement | null;
if (!container) { if (!container) {