From fb20c480d4929f8ff4c185ee10f4dcb37e4309c0 Mon Sep 17 00:00:00 2001 From: qcrao Date: Fri, 17 Nov 2023 14:53:38 +0800 Subject: [PATCH] support both plus user and common users --- addCheckboxes.js | 7 ++++--- bulkDeleteConversations.js | 43 +++++++++++++++++--------------------- globals.js | 12 +++++------ 3 files changed, 29 insertions(+), 33 deletions(-) diff --git a/addCheckboxes.js b/addCheckboxes.js index 4f56869..c784769 100644 --- a/addCheckboxes.js +++ b/addCheckboxes.js @@ -1,4 +1,5 @@ console.log('addCheckboxes.js loaded'); + // 查找带有特定选择器的祖先元素 function findAncestorWithCheckbox(el, selector) { while ((el = el.parentElement) && !el.querySelector(selector)); @@ -27,8 +28,8 @@ function preventEventPropagation(event) { // 添加复选框到每个对话 function addCheckboxes() { - console.log('Adding checkboxes to conversations...', Selectors, isPlusUser()); - const conversations = document.querySelectorAll(Selectors.CONVERSATION_SELECTOR); + console.log('Adding checkboxes to conversations...', window.getSelectors(), isPlusUser()); + const conversations = document.querySelectorAll(window.getSelectors().CONVERSATION_SELECTOR); conversations.forEach((conversation, index) => { let existingCheckbox = conversation.querySelector(`.${CHECKBOX_CLASS}`); @@ -49,7 +50,7 @@ function addCheckboxes() { conversation.insertAdjacentElement('afterbegin', checkbox); // 为对话标题添加点击事件 - const titleElement = conversation.querySelector(TITLE_SELECTOR); + const titleElement = conversation.querySelector(window.getSelectors().TITLE_SELECTOR); if (titleElement) { titleElement.style.cursor = 'default'; if (!titleElement.dataset.hasClickListener) { // 检查是否已经添加了监听器 diff --git a/bulkDeleteConversations.js b/bulkDeleteConversations.js index 65ccd61..8b5cc70 100644 --- a/bulkDeleteConversations.js +++ b/bulkDeleteConversations.js @@ -17,55 +17,50 @@ async function bulkDeleteConversations() { } function getSelectedConversations() { - return [...document.querySelectorAll(Selectors.conversationsCheckbox)]; + return [...document.querySelectorAll(window.getSelectors().conversationsCheckbox)]; } function removeAllCheckboxes() { - const allCheckboxes = document.querySelectorAll(Selectors.conversationsCheckbox); + const allCheckboxes = document.querySelectorAll(window.getSelectors().conversationsCheckbox); allCheckboxes.forEach(checkbox => checkbox.remove()); } async function deleteConversation(checkbox) { const conversationElement = checkbox.parentElement; - console.log("1. Clicking conversation", conversationElement); + console.log("1. Clicking conversation...", conversationElement); conversationElement.click(); + await delay(300); + + const threeDotButton = conversationElement.parentElement.querySelector(window.getSelectors().threeDotButton); await delay(500); - if (isPlusUser()) { - // Plus 用户的处理逻辑 - const threeDotButton = conversationElement.parentElement.querySelector('[id^="radix-"]'); - await delay(800); - console.log(threeDotButton); + console.log("2. Clicking three dot button...", conversationElement); + const pointerDownEvent = new PointerEvent('pointerdown', { + bubbles: true, + cancelable: true, + pointerType: 'mouse' + }); - // 创建一个指针事件对象 - const pointerDownEvent = new PointerEvent('pointerdown', { - bubbles: true, - cancelable: true, - pointerType: 'mouse' // 可以是 'mouse', 'pen', 'touch' - }); + threeDotButton.dispatchEvent(pointerDownEvent); - // 触发 onPointerDown 事件 - threeDotButton.dispatchEvent(pointerDownEvent); - } - - const deleteButton = await waitForElement(Selectors.deleteButton); + const deleteButton = await waitForElement(window.getSelectors().deleteButton); if (deleteButton) { - console.log("2. Clicking delete button", deleteButton); + console.log("3. Clicking delete button...", deleteButton); deleteButton.click(); - const confirmButton = await waitForElement(Selectors.confirmDeleteButton); + const confirmButton = await waitForElement(window.getSelectors().confirmDeleteButton); if (confirmButton) { - console.log("3. Clicking confirm button"); + console.log("4. Clicking confirm button..."); confirmButton.click(); - await waitForElementToDisappear(Selectors.confirmDeleteButton); + await waitForElementToDisappear(window.getSelectors().confirmDeleteButton); } } - console.log("4. Deletion completed"); + console.log("5. Deletion completed."); } async function waitForElement(selector, timeout = 2000) { diff --git a/globals.js b/globals.js index efd826f..918f66e 100644 --- a/globals.js +++ b/globals.js @@ -7,6 +7,7 @@ const standardSelectors = { conversationsCheckbox: '.conversation-checkbox:checked', deleteButton: 'nav div a>div>button:nth-child(2)', confirmDeleteButton: 'button.btn.btn-danger', + threeDotButton: '[id^="radix-"]', // 其他标准用户选择器... CONVERSATION_SELECTOR: 'div div span div ol li>a.flex', TITLE_SELECTOR: '.flex-1.text-ellipsis.max-h-5.overflow-hidden.break-all.relative', @@ -17,6 +18,7 @@ const plusSelectors = { conversationsCheckbox: '.conversation-checkbox:checked', deleteButton: '[class*="text-red"]', confirmDeleteButton: 'button.btn.btn-danger', + threeDotButton: '[id^="radix-"]', // 其他 Plus 用户选择器... CONVERSATION_SELECTOR: 'div > div > div > div > div > div > nav > div > div > div > span > div > ol > li > div > a', TITLE_SELECTOR: '.relative.grow.overflow-hidden.whitespace-nowrap', @@ -24,16 +26,14 @@ const plusSelectors = { const CHECKBOX_CLASS = 'conversation-checkbox'; -let Selectors; - function isPlusUser() { const spans = document.querySelectorAll('span.text-sm'); return Array.from(spans).some(span => span.textContent.trim() === "Explore"); } -function initializeSelectors() { - Selectors = isPlusUser() ? plusSelectors : standardSelectors; +function getSelectors() { + return isPlusUser() ? plusSelectors : plusSelectors; } -// 确保在文档加载完毕后初始化 Selectors -document.addEventListener('DOMContentLoaded', initializeSelectors); \ No newline at end of file +// 暴露 getSelectors 函数到全局作用域 +window.getSelectors = getSelectors; \ No newline at end of file