support both plus user and common users

This commit is contained in:
qcrao
2023-11-17 14:53:38 +08:00
parent ea7e23a765
commit fb20c480d4
3 changed files with 29 additions and 33 deletions

View File

@@ -1,4 +1,5 @@
console.log('addCheckboxes.js loaded'); console.log('addCheckboxes.js loaded');
// 查找带有特定选择器的祖先元素 // 查找带有特定选择器的祖先元素
function findAncestorWithCheckbox(el, selector) { function findAncestorWithCheckbox(el, selector) {
while ((el = el.parentElement) && !el.querySelector(selector)); while ((el = el.parentElement) && !el.querySelector(selector));
@@ -27,8 +28,8 @@ function preventEventPropagation(event) {
// 添加复选框到每个对话 // 添加复选框到每个对话
function addCheckboxes() { function addCheckboxes() {
console.log('Adding checkboxes to conversations...', Selectors, isPlusUser()); console.log('Adding checkboxes to conversations...', window.getSelectors(), isPlusUser());
const conversations = document.querySelectorAll(Selectors.CONVERSATION_SELECTOR); const conversations = document.querySelectorAll(window.getSelectors().CONVERSATION_SELECTOR);
conversations.forEach((conversation, index) => { conversations.forEach((conversation, index) => {
let existingCheckbox = conversation.querySelector(`.${CHECKBOX_CLASS}`); let existingCheckbox = conversation.querySelector(`.${CHECKBOX_CLASS}`);
@@ -49,7 +50,7 @@ function addCheckboxes() {
conversation.insertAdjacentElement('afterbegin', checkbox); conversation.insertAdjacentElement('afterbegin', checkbox);
// 为对话标题添加点击事件 // 为对话标题添加点击事件
const titleElement = conversation.querySelector(TITLE_SELECTOR); const titleElement = conversation.querySelector(window.getSelectors().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,55 +17,50 @@ async function bulkDeleteConversations() {
} }
function getSelectedConversations() { function getSelectedConversations() {
return [...document.querySelectorAll(Selectors.conversationsCheckbox)]; return [...document.querySelectorAll(window.getSelectors().conversationsCheckbox)];
} }
function removeAllCheckboxes() { function removeAllCheckboxes() {
const allCheckboxes = document.querySelectorAll(Selectors.conversationsCheckbox); const allCheckboxes = document.querySelectorAll(window.getSelectors().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;
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); await delay(500);
if (isPlusUser()) { console.log("2. Clicking three dot button...", conversationElement);
// Plus 用户的处理逻辑
const threeDotButton = conversationElement.parentElement.querySelector('[id^="radix-"]');
await delay(800);
console.log(threeDotButton);
// 创建一个指针事件对象
const pointerDownEvent = new PointerEvent('pointerdown', { const pointerDownEvent = new PointerEvent('pointerdown', {
bubbles: true, bubbles: true,
cancelable: true, cancelable: true,
pointerType: 'mouse' // 可以是 'mouse', 'pen', 'touch' pointerType: 'mouse'
}); });
// 触发 onPointerDown 事件
threeDotButton.dispatchEvent(pointerDownEvent); threeDotButton.dispatchEvent(pointerDownEvent);
}
const deleteButton = await waitForElement(Selectors.deleteButton); const deleteButton = await waitForElement(window.getSelectors().deleteButton);
if (deleteButton) { if (deleteButton) {
console.log("2. Clicking delete button", deleteButton); console.log("3. Clicking delete button...", deleteButton);
deleteButton.click(); deleteButton.click();
const confirmButton = await waitForElement(Selectors.confirmDeleteButton); const confirmButton = await waitForElement(window.getSelectors().confirmDeleteButton);
if (confirmButton) { if (confirmButton) {
console.log("3. Clicking confirm button"); console.log("4. Clicking confirm button...");
confirmButton.click(); confirmButton.click();
await waitForElementToDisappear(Selectors.confirmDeleteButton); await waitForElementToDisappear(window.getSelectors().confirmDeleteButton);
} }
} }
console.log("4. Deletion completed"); console.log("5. Deletion completed.");
} }
async function waitForElement(selector, timeout = 2000) { async function waitForElement(selector, timeout = 2000) {

View File

@@ -7,6 +7,7 @@ const standardSelectors = {
conversationsCheckbox: '.conversation-checkbox:checked', conversationsCheckbox: '.conversation-checkbox:checked',
deleteButton: 'nav div a>div>button:nth-child(2)', deleteButton: 'nav div a>div>button:nth-child(2)',
confirmDeleteButton: 'button.btn.btn-danger', confirmDeleteButton: 'button.btn.btn-danger',
threeDotButton: '[id^="radix-"]',
// 其他标准用户选择器... // 其他标准用户选择器...
CONVERSATION_SELECTOR: 'div div span div ol li>a.flex', CONVERSATION_SELECTOR: 'div div span div ol li>a.flex',
TITLE_SELECTOR: '.flex-1.text-ellipsis.max-h-5.overflow-hidden.break-all.relative', TITLE_SELECTOR: '.flex-1.text-ellipsis.max-h-5.overflow-hidden.break-all.relative',
@@ -17,6 +18,7 @@ const plusSelectors = {
conversationsCheckbox: '.conversation-checkbox:checked', conversationsCheckbox: '.conversation-checkbox:checked',
deleteButton: '[class*="text-red"]', deleteButton: '[class*="text-red"]',
confirmDeleteButton: 'button.btn.btn-danger', confirmDeleteButton: 'button.btn.btn-danger',
threeDotButton: '[id^="radix-"]',
// 其他 Plus 用户选择器... // 其他 Plus 用户选择器...
CONVERSATION_SELECTOR: 'div > div > div > div > div > div > nav > div > div > div > span > div > ol > li > div > a', CONVERSATION_SELECTOR: 'div > div > div > div > div > div > nav > div > div > div > span > div > ol > li > div > a',
TITLE_SELECTOR: '.relative.grow.overflow-hidden.whitespace-nowrap', TITLE_SELECTOR: '.relative.grow.overflow-hidden.whitespace-nowrap',
@@ -24,16 +26,14 @@ const plusSelectors = {
const CHECKBOX_CLASS = 'conversation-checkbox'; const CHECKBOX_CLASS = 'conversation-checkbox';
let Selectors;
function isPlusUser() { function isPlusUser() {
const spans = document.querySelectorAll('span.text-sm'); const spans = document.querySelectorAll('span.text-sm');
return Array.from(spans).some(span => span.textContent.trim() === "Explore"); return Array.from(spans).some(span => span.textContent.trim() === "Explore");
} }
function initializeSelectors() { function getSelectors() {
Selectors = isPlusUser() ? plusSelectors : standardSelectors; return isPlusUser() ? plusSelectors : plusSelectors;
} }
// 确保在文档加载完毕后初始化 Selectors // 暴露 getSelectors 函数到全局作用域
document.addEventListener('DOMContentLoaded', initializeSelectors); window.getSelectors = getSelectors;