add delete conversations monitor

This commit is contained in:
qcrao
2024-07-11 13:26:08 +08:00
parent 96dd85edf5
commit 3feea445c1
5 changed files with 92 additions and 30 deletions

21
background.js Normal file
View File

@@ -0,0 +1,21 @@
console.log("Background script loaded");
chrome.runtime.onInstalled.addListener(() => {
console.log("Extension installed");
});
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "getUserInfo") {
chrome.identity.getProfileUserInfo({ accountStatus: "ANY" }, (userInfo) => {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError);
sendResponse({ error: chrome.runtime.lastError.message });
} else {
sendResponse({ userInfo: userInfo });
}
});
return true; // Will respond asynchronously
}
});
console.log("Background script setup complete");

View File

@@ -11,6 +11,8 @@ async function bulkDeleteConversations() {
console.log("Selected Conversations:", selectedConversations);
sendEventAsync(selectedConversations.length);
for (const element of selectedConversations) {
await deleteConversation(element);
}
@@ -118,4 +120,37 @@ async function waitForElementToDisappear(selector, timeout = 2000) {
throw new Error(`Element ${selector} did not disappear within ${timeout}ms`);
}
async function sendEventAsync(count) {
try {
const userInfo = await getUserInfo();
const timestamp = new Date().toISOString().replace("T", " ").substr(0, 19);
const data = {
user_id: userInfo.id || "unknown",
timestamp: timestamp,
action: "delete",
count: count,
};
const response = await fetch(
"https://bulk-delete-chatgpt-worker.qcrao.com/send-event",
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data),
}
);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
console.log("Event sent successfully");
} catch (error) {
console.error("Error sending event:", error);
}
}
bulkDeleteConversations();

View File

@@ -1,24 +1,40 @@
if (typeof window.globalsLoaded === 'undefined') {
console.log('globals.js loaded');
if (typeof window.globalsLoaded === "undefined") {
console.log("globals.js loaded");
window.globalsLoaded = true;
const lastChecked = null;
const Selectors = {
conversationsCheckbox: '.conversation-checkbox:checked',
confirmDeleteButton: 'button.btn.btn-danger',
conversationsCheckbox: ".conversation-checkbox:checked",
confirmDeleteButton: "button.btn.btn-danger",
threeDotButton: '[id^="radix-"]',
CONVERSATION_SELECTOR: 'div > div > div > div > div > div > nav > div > div > div > div > ol > li > div > a',
TITLE_SELECTOR: '.relative.grow.overflow-hidden.whitespace-nowrap',
CONVERSATION_SELECTOR:
"div > div > div > div > div > div > nav > div > div > div > div > ol > li > div > a",
TITLE_SELECTOR: ".relative.grow.overflow-hidden.whitespace-nowrap",
};
const CHECKBOX_CLASS = 'conversation-checkbox';
const CHECKBOX_CLASS = "conversation-checkbox";
// Define getUserInfo function
function getUserInfo() {
return new Promise((resolve, reject) => {
chrome.runtime.sendMessage({ action: "getUserInfo" }, (response) => {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError);
} else if (response.error) {
reject(new Error(response.error));
} else {
resolve(response.userInfo);
}
});
});
}
// Expose variables to the global scope
window.lastChecked = lastChecked;
window.Selectors = Selectors;
window.CHECKBOX_CLASS = CHECKBOX_CLASS;
} else {
console.log('globals.js already loaded, skipping re-initialization');
console.log("globals.js already loaded, skipping re-initialization");
}

View File

@@ -13,6 +13,9 @@
},
"permissions": ["scripting", "activeTab", "identity", "identity.email"],
"host_permissions": ["https://bulk-delete-chatgpt-worker.qcrao.com/*"],
"background": {
"service_worker": "background.js"
},
"content_scripts": [
{
"matches": ["*://chat.openai.com/*"],

View File

@@ -157,19 +157,6 @@ function showModal() {
});
}
function getUserInfo() {
return new Promise((resolve) => {
chrome.identity.getProfileUserInfo({ accountStatus: "ANY" }, (userInfo) => {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError);
resolve(null);
} else {
resolve(userInfo);
}
});
});
}
function updateCopyrightYear() {
const currentYear = new Date().getFullYear();
document.getElementById(