-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
bulkArchiveConversations.js
119 lines (96 loc) · 3.27 KB
/
bulkArchiveConversations.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
console.log("bulkArchiveConversations.js loaded");
async function bulkArchiveConversations() {
const selectedConversations = getSelectedConversations();
if (selectedConversations.length === 0) {
console.log("No conversations to archive.");
removeAllCheckboxes();
return;
}
console.log("Selected Conversations:", selectedConversations);
sendEventAsync("archive", selectedConversations.length);
for (let i = 0; i < selectedConversations.length; i++) {
await archiveConversation(selectedConversations[i]);
const progress = Math.round(((i + 1) / selectedConversations.length) * 100);
chrome.runtime.sendMessage({
action: "updateProgress",
buttonId: "bulk-archive",
progress: progress,
});
}
chrome.runtime.sendMessage({
action: "operationComplete",
buttonId: "bulk-archive",
});
}
function getSelectedConversations() {
return [...document.querySelectorAll(Selectors.conversationsCheckbox)];
}
function removeAllCheckboxes() {
const allCheckboxes = document.querySelectorAll(`.${CHECKBOX_CLASS}`);
allCheckboxes.forEach((checkbox) => checkbox.remove());
}
async function archiveConversation(checkbox) {
await delay(100);
const conversationElement = checkbox.parentElement;
const hoverEvent = new MouseEvent("mouseover", {
view: window,
bubbles: true,
cancelable: true,
});
console.log("1. Hovering over conversation...", conversationElement);
conversationElement.dispatchEvent(hoverEvent);
await delay(200);
const pointerDownEvent = new PointerEvent("pointerdown", {
bubbles: true,
cancelable: true,
pointerType: "mouse",
});
const threeDotButton = await waitForElement(
Selectors.threeDotButton,
conversationElement.parentElement
);
console.log("2. Clicking three dot button...", threeDotButton);
threeDotButton.dispatchEvent(pointerDownEvent);
await delay(300);
const archiveButton = await waitForArchiveButton();
if (archiveButton) {
console.log("3. Clicking archive button...", archiveButton);
archiveButton.click();
await delay(500);
}
console.log("4. Archiving completed.");
}
async function waitForArchiveButton(parent = document, timeout = 2000) {
const selector = 'div[role="menuitem"]';
const textContent = "Archive";
const textContentSimplifiedChinese = "归档";
const textContentTraditionalChinese = "封存";
const startedAt = Date.now();
while (Date.now() - startedAt < timeout) {
const elements = parent.querySelectorAll(selector);
const element = Array.from(elements).find(
(el) =>
el.textContent.trim() === textContent ||
el.textContent.trim() === textContentSimplifiedChinese ||
el.textContent.trim() === textContentTraditionalChinese
);
if (element) return element;
await delay(100);
}
return null;
}
function delay(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async function waitForElement(selector, parent = document, timeout = 2000) {
const startedAt = Date.now();
while (Date.now() - startedAt < timeout) {
const element = parent.querySelector(selector);
if (element) return element;
await delay(100);
}
throw new Error(
`Element ${selector} not found within ${timeout}ms in the specified parent`
);
}
bulkArchiveConversations();