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) {
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);

View File

@@ -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);

View File

@@ -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 =
`&copy; ${currentYear} <a href="https://github.com/qcrao/bulk-delete-chatGPT" target="_blank">qcrao@GitHub</a>`;
document.getElementById(
"copyright"
).innerHTML = `&copy; ${currentYear} <a href="https://github.com/qcrao/bulk-delete-chatGPT" target="_blank">qcrao@GitHub</a>`;
}
initializeButtons();