From 2a2844eeccedcc90246e70ec4d222ee3e0e1954e Mon Sep 17 00:00:00 2001 From: qcrao Date: Mon, 8 Jul 2024 17:05:50 +0800 Subject: [PATCH] add bulk archive --- ...rsations.js => bulkArchiveConversations.js | 8 +++- bulkDeleteConversations.js | 37 +++++++++++-------- popup.js | 35 ++++++++++-------- 3 files changed, 49 insertions(+), 31 deletions(-) rename bulkArchiveCoversations.js => bulkArchiveConversations.js (90%) diff --git a/bulkArchiveCoversations.js b/bulkArchiveConversations.js similarity index 90% rename from bulkArchiveCoversations.js rename to bulkArchiveConversations.js index be4e0f8..292e58f 100644 --- a/bulkArchiveCoversations.js +++ b/bulkArchiveConversations.js @@ -66,12 +66,18 @@ async function archiveConversation(checkbox) { 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) => + el.textContent.trim() === textContent || + el.textContent.trim() === textContentSimplifiedChinese || + el.textContent.trim() === textContentTraditionalChinese ); if (element) return element; await delay(100); diff --git a/bulkDeleteConversations.js b/bulkDeleteConversations.js index 9ef07a0..205cde1 100644 --- a/bulkDeleteConversations.js +++ b/bulkDeleteConversations.js @@ -1,4 +1,4 @@ -console.log('bulkDeleteConversations.js loaded'); +console.log("bulkDeleteConversations.js loaded"); async function bulkDeleteConversations() { const selectedConversations = getSelectedConversations(); @@ -22,29 +22,32 @@ function getSelectedConversations() { function removeAllCheckboxes() { const allCheckboxes = document.querySelectorAll(`.${CHECKBOX_CLASS}`); - allCheckboxes.forEach(checkbox => checkbox.remove()); + allCheckboxes.forEach((checkbox) => checkbox.remove()); } async function deleteConversation(checkbox) { await delay(100); const conversationElement = checkbox.parentElement; - const hoverEvent = new MouseEvent('mouseover', { + const hoverEvent = new MouseEvent("mouseover", { view: window, bubbles: true, - cancelable: true + cancelable: true, }); console.log("1. Hovering over conversation...", conversationElement); - conversationElement.dispatchEvent(hoverEvent); + conversationElement.dispatchEvent(hoverEvent); await delay(200); - const pointerDownEvent = new PointerEvent('pointerdown', { + const pointerDownEvent = new PointerEvent("pointerdown", { bubbles: true, cancelable: true, - pointerType: 'mouse' + pointerType: "mouse", }); - const threeDotButton = await waitForElement(Selectors.threeDotButton, conversationElement.parentElement); + const threeDotButton = await waitForElement( + Selectors.threeDotButton, + conversationElement.parentElement + ); console.log("2. Clicking three dot button...", threeDotButton); threeDotButton.dispatchEvent(pointerDownEvent); await delay(300); @@ -73,10 +76,12 @@ async function waitForDeleteButton(parent = document, timeout = 2000) { const textContent = "Delete"; const startedAt = Date.now(); - while ((Date.now() - startedAt) < timeout) { + while (Date.now() - startedAt < timeout) { const elements = parent.querySelectorAll(selector); - const element = Array.from(elements).find(el => - el.textContent.trim() === textContent || el.querySelector('.text-token-text-error') + const element = Array.from(elements).find( + (el) => + el.textContent.trim() === textContent || + el.querySelector(".text-token-text-error") ); if (element) return element; await delay(100); @@ -86,23 +91,25 @@ async function waitForDeleteButton(parent = document, timeout = 2000) { } function delay(ms) { - return new Promise(resolve => setTimeout(resolve, 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) { + 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`); + throw new Error( + `Element ${selector} not found within ${timeout}ms in the specified parent` + ); } async function waitForElementToDisappear(selector, timeout = 2000) { const startedAt = Date.now(); - while ((Date.now() - startedAt) < timeout) { + while (Date.now() - startedAt < timeout) { const element = document.querySelector(selector); if (!element) return; await delay(100); diff --git a/popup.js b/popup.js index 6d31fc6..517094a 100644 --- a/popup.js +++ b/popup.js @@ -1,17 +1,20 @@ function loadGlobalsThenExecute(tabId, secondaryScript) { - chrome.scripting.executeScript({ - target: { tabId: tabId }, - files: ['globals.js'] - }, () => { - chrome.scripting.executeScript({ + chrome.scripting.executeScript( + { target: { tabId: tabId }, - files: [secondaryScript] - }); - }); + files: ["globals.js"], + }, + () => { + chrome.scripting.executeScript({ + target: { tabId: tabId }, + files: [secondaryScript], + }); + } + ); } function addButtonListener(buttonId, scriptName) { - document.getElementById(buttonId).addEventListener('click', () => { + document.getElementById(buttonId).addEventListener("click", () => { chrome.tabs.query({ active: true, currentWindow: true }, ([tab]) => { if (tab) { loadGlobalsThenExecute(tab.id, scriptName); @@ -21,16 +24,18 @@ function addButtonListener(buttonId, scriptName) { } function initializeButtons() { - addButtonListener('add-checkboxes', 'addCheckboxes.js'); - addButtonListener('bulk-delete', 'bulkDeleteConversations.js'); - addButtonListener('toggle-checkboxes', 'toggleCheckboxes.js'); - addButtonListener('remove-checkboxes', 'removeCheckboxes.js'); + addButtonListener("add-checkboxes", "addCheckboxes.js"); + addButtonListener("bulk-delete", "bulkDeleteConversations.js"); + addButtonListener("toggle-checkboxes", "toggleCheckboxes.js"); + addButtonListener("remove-checkboxes", "removeCheckboxes.js"); + addButtonListener("bulk-archive", "bulkArchiveConversations.js"); } function updateCopyrightYear() { const currentYear = new Date().getFullYear(); - document.getElementById('copyright').innerHTML = - `© ${currentYear} qcrao@GitHub`; + document.getElementById( + "copyright" + ).innerHTML = `© ${currentYear} qcrao@GitHub`; } initializeButtons();