opt user experience

This commit is contained in:
qcrao
2023-11-17 17:30:00 +08:00
parent fb20c480d4
commit 25096861d6
4 changed files with 22 additions and 47 deletions

View File

@@ -28,8 +28,8 @@ function preventEventPropagation(event) {
// 添加复选框到每个对话 // 添加复选框到每个对话
function addCheckboxes() { function addCheckboxes() {
console.log('Adding checkboxes to conversations...', window.getSelectors(), isPlusUser()); console.log('Adding checkboxes to conversations...', Selectors);
const conversations = document.querySelectorAll(window.getSelectors().CONVERSATION_SELECTOR); const conversations = document.querySelectorAll(Selectors.CONVERSATION_SELECTOR);
conversations.forEach((conversation, index) => { conversations.forEach((conversation, index) => {
let existingCheckbox = conversation.querySelector(`.${CHECKBOX_CLASS}`); let existingCheckbox = conversation.querySelector(`.${CHECKBOX_CLASS}`);
@@ -50,7 +50,7 @@ function addCheckboxes() {
conversation.insertAdjacentElement('afterbegin', checkbox); conversation.insertAdjacentElement('afterbegin', checkbox);
// 为对话标题添加点击事件 // 为对话标题添加点击事件
const titleElement = conversation.querySelector(window.getSelectors().TITLE_SELECTOR); const titleElement = conversation.querySelector(Selectors.TITLE_SELECTOR);
if (titleElement) { if (titleElement) {
titleElement.style.cursor = 'default'; titleElement.style.cursor = 'default';
if (!titleElement.dataset.hasClickListener) { // 检查是否已经添加了监听器 if (!titleElement.dataset.hasClickListener) { // 检查是否已经添加了监听器

View File

@@ -17,61 +17,58 @@ async function bulkDeleteConversations() {
} }
function getSelectedConversations() { function getSelectedConversations() {
return [...document.querySelectorAll(window.getSelectors().conversationsCheckbox)]; return [...document.querySelectorAll(Selectors.conversationsCheckbox)];
} }
function removeAllCheckboxes() { function removeAllCheckboxes() {
const allCheckboxes = document.querySelectorAll(window.getSelectors().conversationsCheckbox); const allCheckboxes = document.querySelectorAll(Selectors.conversationsCheckbox);
allCheckboxes.forEach(checkbox => checkbox.remove()); allCheckboxes.forEach(checkbox => checkbox.remove());
} }
async function deleteConversation(checkbox) { async function deleteConversation(checkbox) {
const conversationElement = checkbox.parentElement; const conversationElement = checkbox.parentElement;
await delay(300);
console.log("1. Clicking conversation...", conversationElement); console.log("1. Clicking conversation...", conversationElement);
conversationElement.click(); 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', { const pointerDownEvent = new PointerEvent('pointerdown', {
bubbles: true, bubbles: true,
cancelable: true, cancelable: true,
pointerType: 'mouse' pointerType: 'mouse'
}); });
const threeDotButton = await waitForElement(Selectors.threeDotButton, conversationElement.parentElement);
threeDotButton.dispatchEvent(pointerDownEvent); 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) { if (deleteButton) {
console.log("3. Clicking delete button...", deleteButton); console.log("3. Clicking delete button...", deleteButton);
deleteButton.click(); deleteButton.click();
const confirmButton = await waitForElement(window.getSelectors().confirmDeleteButton); const confirmButton = await waitForElement(Selectors.confirmDeleteButton);
if (confirmButton) { if (confirmButton) {
console.log("4. Clicking confirm button..."); console.log("4. Clicking confirm button...");
confirmButton.click(); confirmButton.click();
await waitForElementToDisappear(window.getSelectors().confirmDeleteButton); await waitForElementToDisappear(Selectors.confirmDeleteButton);
} }
} }
console.log("5. Deletion completed."); console.log("5. Deletion completed.");
} }
async function waitForElement(selector, timeout = 2000) { async function waitForElement(selector, parent = document, timeout = 2000) {
const startedAt = Date.now(); const startedAt = Date.now();
while ((Date.now() - startedAt) < timeout) { while ((Date.now() - startedAt) < timeout) {
const element = document.querySelector(selector); const element = parent.querySelector(selector);
if (element) return element; if (element) return element; // 返回找到的元素
await delay(100); await delay(100);
} }
console.log(`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`); throw new Error(`Element ${selector} not found within ${timeout}ms in the specified parent`);
} }
async function waitForElementToDisappear(selector, timeout = 2000) { async function waitForElementToDisappear(selector, timeout = 2000) {

View File

@@ -2,18 +2,7 @@ console.log('globals.js loaded');
let lastChecked = null; let lastChecked = null;
const standardSelectors = { let Selectors = {
// 标准用户的选择器
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 = {
// Plus 用户的选择器 // Plus 用户的选择器
conversationsCheckbox: '.conversation-checkbox:checked', conversationsCheckbox: '.conversation-checkbox:checked',
deleteButton: '[class*="text-red"]', deleteButton: '[class*="text-red"]',
@@ -24,16 +13,4 @@ const plusSelectors = {
TITLE_SELECTOR: '.relative.grow.overflow-hidden.whitespace-nowrap', TITLE_SELECTOR: '.relative.grow.overflow-hidden.whitespace-nowrap',
}; };
const CHECKBOX_CLASS = 'conversation-checkbox'; let 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;

View File

@@ -1,7 +1,7 @@
{ {
"manifest_version": 3, "manifest_version": 3,
"name": "ChatGPT Bulk Delete", "name": "ChatGPT Bulk Delete",
"version": "3.0", "version": "4.0",
"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"
@@ -24,7 +24,8 @@
"globals.js", "globals.js",
"addCheckboxes.js", "addCheckboxes.js",
"bulkDeleteConversations.js" "bulkDeleteConversations.js"
] ],
"run_at": "document_idle"
} }
] ]
} }