diff --git a/addCheckboxes.js b/addCheckboxes.js index c784769..ec1575d 100644 --- a/addCheckboxes.js +++ b/addCheckboxes.js @@ -28,8 +28,8 @@ function preventEventPropagation(event) { // 添加复选框到每个对话 function addCheckboxes() { - console.log('Adding checkboxes to conversations...', window.getSelectors(), isPlusUser()); - const conversations = document.querySelectorAll(window.getSelectors().CONVERSATION_SELECTOR); + console.log('Adding checkboxes to conversations...', Selectors); + const conversations = document.querySelectorAll(Selectors.CONVERSATION_SELECTOR); conversations.forEach((conversation, index) => { let existingCheckbox = conversation.querySelector(`.${CHECKBOX_CLASS}`); @@ -50,7 +50,7 @@ function addCheckboxes() { conversation.insertAdjacentElement('afterbegin', checkbox); // 为对话标题添加点击事件 - const titleElement = conversation.querySelector(window.getSelectors().TITLE_SELECTOR); + const titleElement = conversation.querySelector(Selectors.TITLE_SELECTOR); if (titleElement) { titleElement.style.cursor = 'default'; if (!titleElement.dataset.hasClickListener) { // 检查是否已经添加了监听器 diff --git a/bulkDeleteConversations.js b/bulkDeleteConversations.js index 8b5cc70..99a885f 100644 --- a/bulkDeleteConversations.js +++ b/bulkDeleteConversations.js @@ -17,61 +17,58 @@ async function bulkDeleteConversations() { } function getSelectedConversations() { - return [...document.querySelectorAll(window.getSelectors().conversationsCheckbox)]; + return [...document.querySelectorAll(Selectors.conversationsCheckbox)]; } function removeAllCheckboxes() { - const allCheckboxes = document.querySelectorAll(window.getSelectors().conversationsCheckbox); + const allCheckboxes = document.querySelectorAll(Selectors.conversationsCheckbox); allCheckboxes.forEach(checkbox => checkbox.remove()); } async function deleteConversation(checkbox) { const conversationElement = checkbox.parentElement; - + await delay(300); console.log("1. Clicking conversation...", conversationElement); conversationElement.click(); - await delay(300); - const threeDotButton = conversationElement.parentElement.querySelector(window.getSelectors().threeDotButton); - await delay(500); - - console.log("2. Clicking three dot button...", conversationElement); const pointerDownEvent = new PointerEvent('pointerdown', { bubbles: true, cancelable: true, pointerType: 'mouse' }); - + const threeDotButton = await waitForElement(Selectors.threeDotButton, conversationElement.parentElement); threeDotButton.dispatchEvent(pointerDownEvent); + await delay(300); + console.log("2. Clicking three dot button...", threeDotButton); - const deleteButton = await waitForElement(window.getSelectors().deleteButton); + const deleteButton = await waitForElement(Selectors.deleteButton); if (deleteButton) { console.log("3. Clicking delete button...", deleteButton); deleteButton.click(); - const confirmButton = await waitForElement(window.getSelectors().confirmDeleteButton); + const confirmButton = await waitForElement(Selectors.confirmDeleteButton); if (confirmButton) { console.log("4. Clicking confirm button..."); confirmButton.click(); - await waitForElementToDisappear(window.getSelectors().confirmDeleteButton); + await waitForElementToDisappear(Selectors.confirmDeleteButton); } } console.log("5. Deletion completed."); } -async function waitForElement(selector, timeout = 2000) { +async function waitForElement(selector, parent = document, timeout = 2000) { const startedAt = Date.now(); while ((Date.now() - startedAt) < timeout) { - const element = document.querySelector(selector); - if (element) return element; + const element = parent.querySelector(selector); + if (element) return element; // 返回找到的元素 await delay(100); } - console.log(`Element ${selector} not found within ${timeout}ms`); - throw new Error(`Element ${selector} not found within ${timeout}ms`); + 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`); } async function waitForElementToDisappear(selector, timeout = 2000) { diff --git a/globals.js b/globals.js index 918f66e..e20b728 100644 --- a/globals.js +++ b/globals.js @@ -2,18 +2,7 @@ console.log('globals.js loaded'); let lastChecked = null; -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', -}; - -const plusSelectors = { +let Selectors = { // Plus 用户的选择器 conversationsCheckbox: '.conversation-checkbox:checked', deleteButton: '[class*="text-red"]', @@ -24,16 +13,4 @@ const plusSelectors = { TITLE_SELECTOR: '.relative.grow.overflow-hidden.whitespace-nowrap', }; -const CHECKBOX_CLASS = 'conversation-checkbox'; - -function isPlusUser() { - const spans = document.querySelectorAll('span.text-sm'); - return Array.from(spans).some(span => span.textContent.trim() === "Explore"); -} - -function getSelectors() { - return isPlusUser() ? plusSelectors : plusSelectors; -} - -// 暴露 getSelectors 函数到全局作用域 -window.getSelectors = getSelectors; \ No newline at end of file +let CHECKBOX_CLASS = 'conversation-checkbox'; \ No newline at end of file diff --git a/manifest.json b/manifest.json index 3114f4f..6194059 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 3, "name": "ChatGPT Bulk Delete", - "version": "3.0", + "version": "4.0", "description": "A Chrome extension to bulk delete ChatGPT conversations", "icons": { "48": "icon48.png" @@ -24,7 +24,8 @@ "globals.js", "addCheckboxes.js", "bulkDeleteConversations.js" - ] + ], + "run_at": "document_idle" } ] }