diff --git a/addCheckboxes.js b/addCheckboxes.js index 6cf59ba..4b9b4e6 100644 --- a/addCheckboxes.js +++ b/addCheckboxes.js @@ -1,6 +1,11 @@ +function findAncestorWithCheckbox(el, selector) { + while ((el = el.parentElement) && !el.querySelector(selector)); + return el; +} + function toggleCheckbox(event) { - // 从标题元素获取其前一个兄弟元素,即复选框 - const checkbox = event.currentTarget.parentElement.querySelector('.conversation-checkbox'); + const parentElement = findAncestorWithCheckbox(event.currentTarget, '.conversation-checkbox'); + const checkbox = parentElement ? parentElement.querySelector('.conversation-checkbox') : null; if (checkbox && checkbox.type === 'checkbox') { checkbox.checked = !checkbox.checked; } @@ -17,6 +22,12 @@ function addCheckboxes() { const conversations = document.querySelectorAll(conversationSelectors); for (const [index, conversation] of conversations.entries()) { + // 检查是否已经有复选框 + const existingCheckbox = conversation.querySelector('.conversation-checkbox'); + if (existingCheckbox) { + return; // 如果已经有复选框,直接返回 + } + const checkbox = document.createElement('input'); checkbox.type = 'checkbox'; checkbox.className = 'conversation-checkbox'; diff --git a/bulkDeleteConversations.js b/bulkDeleteConversations.js index ec18b66..5ce250e 100644 --- a/bulkDeleteConversations.js +++ b/bulkDeleteConversations.js @@ -9,18 +9,14 @@ async function bulkDeleteConversations() { console.log("Selected Conversations:", selectedConversations); - await deleteConversation(selectedConversations[0]); - const updatedConversations = selectedConversations.map(conversation => conversation - 1); - - delay(1000); - await addCheckboxesWithState(updatedConversations.slice(1)); - - bulkDeleteConversations(); + // 使用 for 循环按顺序删除选中的对话 + for (const element of selectedConversations) { + await deleteConversation(element); + } } function getSelectedConversations() { - const selectedCheckboxes = document.querySelectorAll('.conversation-checkbox:checked'); - return Array.from(selectedCheckboxes).map(checkbox => parseInt(checkbox.dataset.index)); + return [...document.querySelectorAll('.conversation-checkbox:checked')]; } function removeAllCheckboxes() { @@ -28,25 +24,25 @@ function removeAllCheckboxes() { allCheckboxes.forEach(checkbox => checkbox.remove()); } -async function deleteConversation(conversationIndex) { - const checkbox = document.querySelector(`.conversation-checkbox[data-index='${conversationIndex}']`); +async function deleteConversation(checkbox) { const conversationElement = checkbox.closest('.flex.py-3.px-3.items-center.gap-3.relative.rounded-md'); - console.log("开始删除..."); - console.log("currentIndex:", conversationIndex); + console.log("1. 开始删除", conversationElement); conversationElement.click(); // 点击对话 - const deleteButton = await waitForElement('.absolute.flex.right-1.z-10.text-gray-300.visible button:nth-child(3)'); + // debugger + + const deleteButton = await waitForElement('.absolute.flex.right-1.z-10.text-gray-300.visible button:nth-child(2)'); if (deleteButton) { - console.log("点击删除按钮..."); + console.log("2. 点击删除按钮"); deleteButton.click(); // 点击删除按钮 // 等待确认删除对话框的按钮出现 const confirmButton = await waitForElement('button.btn.btn-danger'); if (confirmButton) { - console.log("点击确认按钮..."); + console.log("3. 点击确认按钮"); confirmButton.click(); // 等待删除按钮消失 @@ -54,7 +50,7 @@ async function deleteConversation(conversationIndex) { } } - console.log("完成删除。"); + console.log("4. 完成删除"); } // 用于等待某元素出现的函数 @@ -84,29 +80,4 @@ function delay(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } -async function addCheckboxesWithState(remainingConversations) { - removeAllCheckboxes(); - - const conversations = document.querySelectorAll('.flex.py-3.px-3.items-center.gap-3.relative.rounded-md.hover\\:bg-\\[\\#2A2B32\\].cursor-pointer.break-all.hover\\:pr-4.group'); - for (const [index, conversation] of conversations.entries()) { - const checkbox = document.createElement('input'); - checkbox.type = 'checkbox'; - checkbox.className = 'conversation-checkbox'; - checkbox.dataset.index = index; - - if (remainingConversations.includes(index)) { - checkbox.checked = true; - } - - conversation.insertAdjacentElement('afterbegin', checkbox); - } - - const remainingCheckboxes = document.querySelectorAll('.conversation-checkbox'); - for (let i = 0; i < remainingCheckboxes.length; i++) { - remainingCheckboxes[i].dataset.index = i; - } - - return Promise.resolve(); -} - bulkDeleteConversations(); diff --git a/manifest.json b/manifest.json index 35e5cc3..51a1bb7 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 3, "name": "ChatGPT Bulk Delete", - "version": "1.3", + "version": "1.4", "description": "A Chrome extension to bulk delete ChatGPT conversations", "icons": { "48": "icon48.png"