From 2856b3a3e4db6d4856871a2366d02eac27593e38 Mon Sep 17 00:00:00 2001 From: qcrao Date: Sat, 4 May 2024 17:23:27 +0800 Subject: [PATCH] fix delete button --- bulkDeleteConversations.js | 23 ++++++++++++++++++++++- globals.js | 1 - manifest.json | 2 +- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/bulkDeleteConversations.js b/bulkDeleteConversations.js index 447e2a1..e93b186 100644 --- a/bulkDeleteConversations.js +++ b/bulkDeleteConversations.js @@ -54,7 +54,7 @@ async function deleteConversation(checkbox) { await delay(300); console.log("2. Clicking three dot button...", threeDotButton); - const deleteButton = await waitForElement(Selectors.deleteButton); + const deleteButton = await waitForDeleteButton(); if (deleteButton) { console.log("3. Clicking delete button...", deleteButton); @@ -73,6 +73,27 @@ async function deleteConversation(checkbox) { console.log("5. Deletion completed."); } +async function waitForDeleteButton(parent = document, timeout = 2000) { + const selector = 'div[role="menuitem"]'; // 设定好选择器 + const textContent = "Delete chat"; // 指定文本内容 + 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; // 返回找到的元素 + await delay(100); + } + + console.log(`Delete button not found within ${timeout}ms`); + throw new Error(`Delete button not found within ${timeout}ms`); +} + +// Helper function to create a delay +function delay(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) { diff --git a/globals.js b/globals.js index 8dc2029..e8ad938 100644 --- a/globals.js +++ b/globals.js @@ -8,7 +8,6 @@ if (typeof window.globalsLoaded === 'undefined') { let Selectors = { // Plus 用户的选择器 conversationsCheckbox: '.conversation-checkbox:checked', - deleteButton: '[class*="text-red"]', confirmDeleteButton: 'button.btn.btn-danger', threeDotButton: '[id^="radix-"]', // 其他 Plus 用户选择器... diff --git a/manifest.json b/manifest.json index 66f90fa..66b7a4b 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 3, "name": "ChatGPT Bulk Delete", - "version": "4.3", + "version": "4.4", "description": "A Chrome extension to bulk delete ChatGPT conversations", "icons": { "48": "icon48.png"