refactor: addcheckbox & delconversations
This commit is contained in:
@@ -1,6 +1,11 @@
|
|||||||
|
function findAncestorWithCheckbox(el, selector) {
|
||||||
|
while ((el = el.parentElement) && !el.querySelector(selector));
|
||||||
|
return el;
|
||||||
|
}
|
||||||
|
|
||||||
function toggleCheckbox(event) {
|
function toggleCheckbox(event) {
|
||||||
// 从标题元素获取其前一个兄弟元素,即复选框
|
const parentElement = findAncestorWithCheckbox(event.currentTarget, '.conversation-checkbox');
|
||||||
const checkbox = event.currentTarget.parentElement.querySelector('.conversation-checkbox');
|
const checkbox = parentElement ? parentElement.querySelector('.conversation-checkbox') : null;
|
||||||
if (checkbox && checkbox.type === 'checkbox') {
|
if (checkbox && checkbox.type === 'checkbox') {
|
||||||
checkbox.checked = !checkbox.checked;
|
checkbox.checked = !checkbox.checked;
|
||||||
}
|
}
|
||||||
@@ -17,6 +22,12 @@ function addCheckboxes() {
|
|||||||
const conversations = document.querySelectorAll(conversationSelectors);
|
const conversations = document.querySelectorAll(conversationSelectors);
|
||||||
|
|
||||||
for (const [index, conversation] of conversations.entries()) {
|
for (const [index, conversation] of conversations.entries()) {
|
||||||
|
// 检查是否已经有复选框
|
||||||
|
const existingCheckbox = conversation.querySelector('.conversation-checkbox');
|
||||||
|
if (existingCheckbox) {
|
||||||
|
return; // 如果已经有复选框,直接返回
|
||||||
|
}
|
||||||
|
|
||||||
const checkbox = document.createElement('input');
|
const checkbox = document.createElement('input');
|
||||||
checkbox.type = 'checkbox';
|
checkbox.type = 'checkbox';
|
||||||
checkbox.className = 'conversation-checkbox';
|
checkbox.className = 'conversation-checkbox';
|
||||||
|
|||||||
@@ -9,18 +9,14 @@ async function bulkDeleteConversations() {
|
|||||||
|
|
||||||
console.log("Selected Conversations:", selectedConversations);
|
console.log("Selected Conversations:", selectedConversations);
|
||||||
|
|
||||||
await deleteConversation(selectedConversations[0]);
|
// 使用 for 循环按顺序删除选中的对话
|
||||||
const updatedConversations = selectedConversations.map(conversation => conversation - 1);
|
for (const element of selectedConversations) {
|
||||||
|
await deleteConversation(element);
|
||||||
delay(1000);
|
}
|
||||||
await addCheckboxesWithState(updatedConversations.slice(1));
|
|
||||||
|
|
||||||
bulkDeleteConversations();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getSelectedConversations() {
|
function getSelectedConversations() {
|
||||||
const selectedCheckboxes = document.querySelectorAll('.conversation-checkbox:checked');
|
return [...document.querySelectorAll('.conversation-checkbox:checked')];
|
||||||
return Array.from(selectedCheckboxes).map(checkbox => parseInt(checkbox.dataset.index));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function removeAllCheckboxes() {
|
function removeAllCheckboxes() {
|
||||||
@@ -28,25 +24,25 @@ function removeAllCheckboxes() {
|
|||||||
allCheckboxes.forEach(checkbox => checkbox.remove());
|
allCheckboxes.forEach(checkbox => checkbox.remove());
|
||||||
}
|
}
|
||||||
|
|
||||||
async function deleteConversation(conversationIndex) {
|
async function deleteConversation(checkbox) {
|
||||||
const checkbox = document.querySelector(`.conversation-checkbox[data-index='${conversationIndex}']`);
|
|
||||||
const conversationElement = checkbox.closest('.flex.py-3.px-3.items-center.gap-3.relative.rounded-md');
|
const conversationElement = checkbox.closest('.flex.py-3.px-3.items-center.gap-3.relative.rounded-md');
|
||||||
|
|
||||||
console.log("开始删除...");
|
console.log("1. 开始删除", conversationElement);
|
||||||
console.log("currentIndex:", conversationIndex);
|
|
||||||
conversationElement.click(); // 点击对话
|
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) {
|
if (deleteButton) {
|
||||||
console.log("点击删除按钮...");
|
console.log("2. 点击删除按钮");
|
||||||
deleteButton.click(); // 点击删除按钮
|
deleteButton.click(); // 点击删除按钮
|
||||||
|
|
||||||
// 等待确认删除对话框的按钮出现
|
// 等待确认删除对话框的按钮出现
|
||||||
const confirmButton = await waitForElement('button.btn.btn-danger');
|
const confirmButton = await waitForElement('button.btn.btn-danger');
|
||||||
|
|
||||||
if (confirmButton) {
|
if (confirmButton) {
|
||||||
console.log("点击确认按钮...");
|
console.log("3. 点击确认按钮");
|
||||||
confirmButton.click();
|
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));
|
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();
|
bulkDeleteConversations();
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"manifest_version": 3,
|
"manifest_version": 3,
|
||||||
"name": "ChatGPT Bulk Delete",
|
"name": "ChatGPT Bulk Delete",
|
||||||
"version": "1.3",
|
"version": "1.4",
|
||||||
"description": "A Chrome extension to bulk delete ChatGPT conversations",
|
"description": "A Chrome extension to bulk delete ChatGPT conversations",
|
||||||
"icons": {
|
"icons": {
|
||||||
"48": "icon48.png"
|
"48": "icon48.png"
|
||||||
|
|||||||
Reference in New Issue
Block a user