fix selector bug when new conversation appears
This commit is contained in:
@@ -1,47 +1,57 @@
|
|||||||
|
// 查找带有特定选择器的祖先元素
|
||||||
function findAncestorWithCheckbox(el, selector) {
|
function findAncestorWithCheckbox(el, selector) {
|
||||||
while ((el = el.parentElement) && !el.querySelector(selector));
|
while ((el = el.parentElement) && !el.querySelector(selector));
|
||||||
return el;
|
return el;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 切换复选框的选中状态
|
||||||
function toggleCheckbox(event) {
|
function toggleCheckbox(event) {
|
||||||
const parentElement = findAncestorWithCheckbox(event.currentTarget, '.conversation-checkbox');
|
const parentElement = findAncestorWithCheckbox(event.currentTarget, `.${CHECKBOX_CLASS}`);
|
||||||
const checkbox = parentElement ? parentElement.querySelector('.conversation-checkbox') : null;
|
const checkbox = parentElement ? parentElement.querySelector(`.${CHECKBOX_CLASS}`) : null;
|
||||||
if (checkbox && checkbox.type === 'checkbox') {
|
if (checkbox) {
|
||||||
checkbox.checked = !checkbox.checked;
|
checkbox.checked = !checkbox.checked;
|
||||||
}
|
}
|
||||||
event.stopPropagation();
|
event.stopPropagation();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 阻止事件冒泡
|
||||||
function preventEventPropagation(event) {
|
function preventEventPropagation(event) {
|
||||||
event.stopPropagation();
|
event.stopPropagation();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 添加复选框到每个对话
|
||||||
function addCheckboxes() {
|
function addCheckboxes() {
|
||||||
const conversationSelectors = 'div div span div ol li>a.flex';
|
const conversations = document.querySelectorAll(CONVERSATION_SELECTOR);
|
||||||
const titleSelectors = '.flex-1.text-ellipsis.max-h-5.overflow-hidden.break-all.relative';
|
|
||||||
const conversations = document.querySelectorAll(conversationSelectors);
|
|
||||||
|
|
||||||
for (const [index, conversation] of conversations.entries()) {
|
conversations.forEach((conversation, index) => {
|
||||||
// 检查是否已经有复选框
|
let existingCheckbox = conversation.querySelector(`.${CHECKBOX_CLASS}`);
|
||||||
const existingCheckbox = conversation.querySelector('.conversation-checkbox');
|
|
||||||
|
// 如果复选框已存在,获取其选中状态并移除它
|
||||||
|
let isChecked = existingCheckbox ? existingCheckbox.checked : false;
|
||||||
if (existingCheckbox) {
|
if (existingCheckbox) {
|
||||||
return; // 如果已经有复选框,直接返回
|
existingCheckbox.remove();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 创建新的复选框并设置其属性
|
||||||
const checkbox = document.createElement('input');
|
const checkbox = document.createElement('input');
|
||||||
checkbox.type = 'checkbox';
|
checkbox.type = 'checkbox';
|
||||||
checkbox.className = 'conversation-checkbox';
|
checkbox.className = CHECKBOX_CLASS;
|
||||||
checkbox.dataset.index = index;
|
checkbox.dataset.index = index;
|
||||||
|
checkbox.checked = isChecked;
|
||||||
|
checkbox.addEventListener('click', preventEventPropagation);
|
||||||
conversation.insertAdjacentElement('afterbegin', checkbox);
|
conversation.insertAdjacentElement('afterbegin', checkbox);
|
||||||
|
|
||||||
const titleElement = conversation.querySelector(titleSelectors);
|
// 为对话标题添加点击事件
|
||||||
|
const titleElement = conversation.querySelector(TITLE_SELECTOR);
|
||||||
if (titleElement) {
|
if (titleElement) {
|
||||||
titleElement.style.cursor = 'default';
|
titleElement.style.cursor = 'default';
|
||||||
titleElement.addEventListener('click', toggleCheckbox);
|
if (!titleElement.dataset.hasClickListener) { // 检查是否已经添加了监听器
|
||||||
checkbox.addEventListener('click', preventEventPropagation);
|
titleElement.addEventListener('click', toggleCheckbox);
|
||||||
|
titleElement.dataset.hasClickListener = 'true'; // 标记已经添加了监听器
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 执行主函数
|
||||||
addCheckboxes();
|
addCheckboxes();
|
||||||
|
|||||||
@@ -5,4 +5,9 @@ const Selectors = {
|
|||||||
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',
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// 定义选择器和其他常量
|
||||||
|
const CONVERSATION_SELECTOR = 'div div span div ol li>a.flex';
|
||||||
|
const TITLE_SELECTOR = '.flex-1.text-ellipsis.max-h-5.overflow-hidden.break-all.relative';
|
||||||
|
const CHECKBOX_CLASS = 'conversation-checkbox';
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"manifest_version": 3,
|
"manifest_version": 3,
|
||||||
"name": "ChatGPT Bulk Delete",
|
"name": "ChatGPT Bulk Delete",
|
||||||
"version": "2.9",
|
"version": "3.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"
|
||||||
|
|||||||
Reference in New Issue
Block a user