diff --git a/background.js b/background.js new file mode 100644 index 0000000..ed79c37 --- /dev/null +++ b/background.js @@ -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"); diff --git a/bulkDeleteConversations.js b/bulkDeleteConversations.js index 205cde1..1ff48b2 100644 --- a/bulkDeleteConversations.js +++ b/bulkDeleteConversations.js @@ -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(); diff --git a/globals.js b/globals.js index 7f89c6a..f2c50a8 100644 --- a/globals.js +++ b/globals.js @@ -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; + window.globalsLoaded = true; - const lastChecked = null; + const lastChecked = null; - const Selectors = { - 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', - }; + const Selectors = { + 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", + }; - const CHECKBOX_CLASS = 'conversation-checkbox'; + const CHECKBOX_CLASS = "conversation-checkbox"; - // Expose variables to the global scope - window.lastChecked = lastChecked; - window.Selectors = Selectors; - window.CHECKBOX_CLASS = CHECKBOX_CLASS; + // 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"); } diff --git a/manifest.json b/manifest.json index b625ae5..0534372 100644 --- a/manifest.json +++ b/manifest.json @@ -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/*"], diff --git a/popup.js b/popup.js index 0b2b55f..fe1ccbe 100644 --- a/popup.js +++ b/popup.js @@ -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(