add bulk archive

This commit is contained in:
qcrao
2024-07-08 17:05:50 +08:00
parent b733dbe9c0
commit 2a2844eecc
3 changed files with 49 additions and 31 deletions

View File

@@ -66,12 +66,18 @@ async function archiveConversation(checkbox) {
async function waitForArchiveButton(parent = document, timeout = 2000) { async function waitForArchiveButton(parent = document, timeout = 2000) {
const selector = 'div[role="menuitem"]'; const selector = 'div[role="menuitem"]';
const textContent = "Archive"; const textContent = "Archive";
const textContentSimplifiedChinese = "归档";
const textContentTraditionalChinese = "封存";
const startedAt = Date.now(); const startedAt = Date.now();
while (Date.now() - startedAt < timeout) { while (Date.now() - startedAt < timeout) {
const elements = parent.querySelectorAll(selector); const elements = parent.querySelectorAll(selector);
const element = Array.from(elements).find( 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; if (element) return element;
await delay(100); await delay(100);

View File

@@ -1,4 +1,4 @@
console.log('bulkDeleteConversations.js loaded'); console.log("bulkDeleteConversations.js loaded");
async function bulkDeleteConversations() { async function bulkDeleteConversations() {
const selectedConversations = getSelectedConversations(); const selectedConversations = getSelectedConversations();
@@ -22,29 +22,32 @@ function getSelectedConversations() {
function removeAllCheckboxes() { function removeAllCheckboxes() {
const allCheckboxes = document.querySelectorAll(`.${CHECKBOX_CLASS}`); const allCheckboxes = document.querySelectorAll(`.${CHECKBOX_CLASS}`);
allCheckboxes.forEach(checkbox => checkbox.remove()); allCheckboxes.forEach((checkbox) => checkbox.remove());
} }
async function deleteConversation(checkbox) { async function deleteConversation(checkbox) {
await delay(100); await delay(100);
const conversationElement = checkbox.parentElement; const conversationElement = checkbox.parentElement;
const hoverEvent = new MouseEvent('mouseover', { const hoverEvent = new MouseEvent("mouseover", {
view: window, view: window,
bubbles: true, bubbles: true,
cancelable: true cancelable: true,
}); });
console.log("1. Hovering over conversation...", conversationElement); console.log("1. Hovering over conversation...", conversationElement);
conversationElement.dispatchEvent(hoverEvent); conversationElement.dispatchEvent(hoverEvent);
await delay(200); await delay(200);
const pointerDownEvent = new PointerEvent('pointerdown', { const pointerDownEvent = new PointerEvent("pointerdown", {
bubbles: true, bubbles: true,
cancelable: 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); console.log("2. Clicking three dot button...", threeDotButton);
threeDotButton.dispatchEvent(pointerDownEvent); threeDotButton.dispatchEvent(pointerDownEvent);
await delay(300); await delay(300);
@@ -73,10 +76,12 @@ async function waitForDeleteButton(parent = document, timeout = 2000) {
const textContent = "Delete"; const textContent = "Delete";
const startedAt = Date.now(); const startedAt = Date.now();
while ((Date.now() - startedAt) < timeout) { while (Date.now() - startedAt < timeout) {
const elements = parent.querySelectorAll(selector); const elements = parent.querySelectorAll(selector);
const element = Array.from(elements).find(el => const element = Array.from(elements).find(
el.textContent.trim() === textContent || el.querySelector('.text-token-text-error') (el) =>
el.textContent.trim() === textContent ||
el.querySelector(".text-token-text-error")
); );
if (element) return element; if (element) return element;
await delay(100); await delay(100);
@@ -86,23 +91,25 @@ async function waitForDeleteButton(parent = document, timeout = 2000) {
} }
function delay(ms) { 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) { async function waitForElement(selector, parent = document, timeout = 2000) {
const startedAt = Date.now(); const startedAt = Date.now();
while ((Date.now() - startedAt) < timeout) { while (Date.now() - startedAt < timeout) {
const element = parent.querySelector(selector); const element = parent.querySelector(selector);
if (element) return element; if (element) return element;
await delay(100); 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) { async function waitForElementToDisappear(selector, timeout = 2000) {
const startedAt = Date.now(); const startedAt = Date.now();
while ((Date.now() - startedAt) < timeout) { while (Date.now() - startedAt < timeout) {
const element = document.querySelector(selector); const element = document.querySelector(selector);
if (!element) return; if (!element) return;
await delay(100); await delay(100);

View File

@@ -1,17 +1,20 @@
function loadGlobalsThenExecute(tabId, secondaryScript) { function loadGlobalsThenExecute(tabId, secondaryScript) {
chrome.scripting.executeScript(
{
target: { tabId: tabId },
files: ["globals.js"],
},
() => {
chrome.scripting.executeScript({ chrome.scripting.executeScript({
target: { tabId: tabId }, target: { tabId: tabId },
files: ['globals.js'] files: [secondaryScript],
}, () => {
chrome.scripting.executeScript({
target: { tabId: tabId },
files: [secondaryScript]
});
}); });
} }
);
}
function addButtonListener(buttonId, scriptName) { function addButtonListener(buttonId, scriptName) {
document.getElementById(buttonId).addEventListener('click', () => { document.getElementById(buttonId).addEventListener("click", () => {
chrome.tabs.query({ active: true, currentWindow: true }, ([tab]) => { chrome.tabs.query({ active: true, currentWindow: true }, ([tab]) => {
if (tab) { if (tab) {
loadGlobalsThenExecute(tab.id, scriptName); loadGlobalsThenExecute(tab.id, scriptName);
@@ -21,16 +24,18 @@ function addButtonListener(buttonId, scriptName) {
} }
function initializeButtons() { function initializeButtons() {
addButtonListener('add-checkboxes', 'addCheckboxes.js'); addButtonListener("add-checkboxes", "addCheckboxes.js");
addButtonListener('bulk-delete', 'bulkDeleteConversations.js'); addButtonListener("bulk-delete", "bulkDeleteConversations.js");
addButtonListener('toggle-checkboxes', 'toggleCheckboxes.js'); addButtonListener("toggle-checkboxes", "toggleCheckboxes.js");
addButtonListener('remove-checkboxes', 'removeCheckboxes.js'); addButtonListener("remove-checkboxes", "removeCheckboxes.js");
addButtonListener("bulk-archive", "bulkArchiveConversations.js");
} }
function updateCopyrightYear() { function updateCopyrightYear() {
const currentYear = new Date().getFullYear(); const currentYear = new Date().getFullYear();
document.getElementById('copyright').innerHTML = document.getElementById(
`&copy; ${currentYear} <a href="https://github.com/qcrao/bulk-delete-chatGPT" target="_blank">qcrao@GitHub</a>`; "copyright"
).innerHTML = `&copy; ${currentYear} <a href="https://github.com/qcrao/bulk-delete-chatGPT" target="_blank">qcrao@GitHub</a>`;
} }
initializeButtons(); initializeButtons();