support all languages && improved code readability and maintainability

This commit is contained in:
qcrao
2024-05-15 12:03:10 +08:00
parent 0267d05e83
commit 6fc1a93ac2
6 changed files with 87 additions and 101 deletions

View File

@@ -21,18 +21,14 @@ function getSelectedConversations() {
}
function removeAllCheckboxes() {
const allCheckboxes = document.querySelectorAll(Selectors.conversationsCheckbox);
const allCheckboxes = document.querySelectorAll(`.${CHECKBOX_CLASS}`);
allCheckboxes.forEach(checkbox => checkbox.remove());
}
async function deleteConversation(checkbox) {
const conversationElement = checkbox.parentElement;
await delay(100);
// console.log("1. Clicking conversation...", conversationElement);
// conversationElement.click();
// 将 click 替换为悬停
// 创建一个鼠标悬停事件
const conversationElement = checkbox.parentElement;
const hoverEvent = new MouseEvent('mouseover', {
view: window,
bubbles: true,
@@ -40,7 +36,6 @@ async function deleteConversation(checkbox) {
});
console.log("1. Hovering over conversation...", conversationElement);
// 触发鼠标悬停事件
conversationElement.dispatchEvent(hoverEvent);
await delay(200);
@@ -50,9 +45,9 @@ async function deleteConversation(checkbox) {
pointerType: 'mouse'
});
const threeDotButton = await waitForElement(Selectors.threeDotButton, conversationElement.parentElement);
console.log("2. Clicking three dot button...", threeDotButton);
threeDotButton.dispatchEvent(pointerDownEvent);
await delay(300);
console.log("2. Clicking three dot button...", threeDotButton);
const deleteButton = await waitForDeleteButton();
@@ -60,8 +55,8 @@ async function deleteConversation(checkbox) {
console.log("3. Clicking delete button...", deleteButton);
deleteButton.click();
const confirmButton = await waitForElement(Selectors.confirmDeleteButton);
const confirmButton = await waitForElement(Selectors.confirmDeleteButton);
if (confirmButton) {
console.log("4. Clicking confirm button...");
confirmButton.click();
@@ -74,22 +69,22 @@ async function deleteConversation(checkbox) {
}
async function waitForDeleteButton(parent = document, timeout = 2000) {
const selector = 'div[role="menuitem"]'; // 设定好选择器
const textContent = "Delete"; // 指定文本内容
const selector = 'div[role="menuitem"]';
const textContent = "Delete";
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; // 返回找到的元素
const element = Array.from(elements).find(el =>
el.textContent.trim() === textContent || el.querySelector('.text-token-text-error')
);
if (element) return element;
await delay(100);
}
console.log(`Delete button not found within ${timeout}ms`);
throw new Error(`Delete button not found within ${timeout}ms`);
return null;
}
// Helper function to create a delay
function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
@@ -98,10 +93,10 @@ 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; // 返回找到的元素
if (element) return element;
await delay(100);
}
console.log(`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`);
}
@@ -112,11 +107,8 @@ async function waitForElementToDisappear(selector, timeout = 2000) {
if (!element) return;
await delay(100);
}
throw new Error(`Element ${selector} did not disappear within ${timeout}ms`);
}
function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
bulkDeleteConversations();