diff --git a/bulkArchiveCoversations.js b/bulkArchiveCoversations.js new file mode 100644 index 0000000..be4e0f8 --- /dev/null +++ b/bulkArchiveCoversations.js @@ -0,0 +1,100 @@ +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); + + for (const element of selectedConversations) { + await archiveConversation(element); + } +} + +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 startedAt = Date.now(); + + while (Date.now() - startedAt < timeout) { + const elements = parent.querySelectorAll(selector); + const element = Array.from(elements).find( + (el) => el.textContent.trim() === textContent + ); + 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(); diff --git a/popup.css b/popup.css index b493d13..030698a 100644 --- a/popup.css +++ b/popup.css @@ -1,10 +1,10 @@ body { - width: 240px; - padding: 15px; - font-family: Arial, sans-serif; - display: flex; - flex-direction: column; - align-items: center; + width: 240px; + padding: 15px; + font-family: Arial, sans-serif; + display: flex; + flex-direction: column; + align-items: center; } .header { @@ -33,9 +33,9 @@ button { padding: 8px 12px; font-size: 14px; cursor: pointer; - background-color: #1ABC9C; + background-color: #1abc9c; color: white; - border: 1px solid #16A085; + border: 1px solid #16a085; border-radius: 6px; text-align: center; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); @@ -44,17 +44,21 @@ button { } button:hover { - background-color: #16A085; - border-color: #14967F; + background-color: #16a085; + border-color: #14967f; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.2); } -button#bulk-delete, button#toggle-checkboxes { +button#bulk-delete, +button#toggle-checkboxes, +button#bulk-archive { font-size: 16px; background-color: #e74c3c; border-color: #c0392b; margin-top: 10px; - width: calc(200px + 40px); /* bulk-delete 宽度等于 body 的宽度减去左右内边距再加上两者之间的距离 */ + width: calc( + 200px + 40px + ); /* bulk-delete 宽度等于 body 的宽度减去左右内边距再加上两者之间的距离 */ } .buttons-wrapper { @@ -94,12 +98,17 @@ button#toggle-checkboxes { border-color: #2758ab; } +button#bulk-archive { + background-color: #da6922; + border-color: #dc661c; +} + #sponsorLink { display: inline-flex; align-items: center; padding: 10px 20px; - background-color: #383E46; - border-color: #78838F; + background-color: #383e46; + border-color: #78838f; border-radius: 5px; color: #fff; text-decoration: none; @@ -114,4 +123,4 @@ button#toggle-checkboxes { #sponsorLink:hover { background-color: #993366; -} \ No newline at end of file +} diff --git a/popup.html b/popup.html index 8d2cc6d..7ee88e6 100644 --- a/popup.html +++ b/popup.html @@ -1,34 +1,56 @@ -
- - + + +