From c766e8808c9e11b5288e746fbd86ece51b0ef6a2 Mon Sep 17 00:00:00 2001 From: "bob.rao" Date: Fri, 14 Apr 2023 15:59:21 +0800 Subject: [PATCH] init --- background.js | 29 ++++++++++++ content.js | 127 ++++++++++++++++++++++++++++++++++++++++++++++++++ icon48.png | Bin 0 -> 3750 bytes manifest.json | 28 +++++++++++ popup.html | 16 +++++++ popup.js | 14 ++++++ 6 files changed, 214 insertions(+) create mode 100644 background.js create mode 100644 content.js create mode 100644 icon48.png create mode 100644 manifest.json create mode 100644 popup.html create mode 100644 popup.js diff --git a/background.js b/background.js new file mode 100644 index 0000000..e004326 --- /dev/null +++ b/background.js @@ -0,0 +1,29 @@ +chrome.webRequest.onCompleted.addListener( + (details) => { + if ( + details.url.includes( + "https://chat.openai.com/backend-api/conversations?" + ) + ) { + chrome.tabs.sendMessage(details.tabId, { + message: "conversations_data_fetched", + }); + } + }, + { urls: [""] } + ); + + chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { + if (request.message === "fetch_conversations_data") { + fetch(request.url) + .then((response) => response.json()) + .then((data) => { + sendResponse({ data: data.items }); + }) + .catch((error) => { + console.error("Error fetching conversations data:", error); + }); + return true; + } + }); + \ No newline at end of file diff --git a/content.js b/content.js new file mode 100644 index 0000000..e553b33 --- /dev/null +++ b/content.js @@ -0,0 +1,127 @@ +console.log('content.js is running'); + +// 向每个会话前添加复选框 +// 向每个会话前添加复选框 +// function addCheckboxes() { +// const conversations = document.querySelectorAll( +// '.flex.flex-col.gap-2.pb-2.text-gray-100.text-sm > .flex.py-3.px-3.items-center.gap-3.relative.rounded-md.hover\\:bg-\\[\\#2A2B32\\].cursor-pointer.break-all.hover\\:pr-4.group' +// ); + +// conversations.forEach((conversation) => { +// const checkbox = document.createElement('input'); +// checkbox.type = 'checkbox'; +// checkbox.className = 'conversation-checkbox'; +// conversation.insertBefore(checkbox, conversation.firstChild); +// }); +// } + +document.addEventListener("fetch", (event) => { + const response = event.detail.response; + + if (response) { + console.log("Result found:", response); + // 在这里执行你的逻辑 + } + }); + + + +function addCheckboxes() { + console.log(`try to addCheckboxes...`); + const conversations = document.querySelectorAll( + '.flex.flex-col.gap-2.pb-2.text-gray-100.text-sm > .flex.py-3.px-3.items-center.gap-3.relative.rounded-md.hover\\:bg-\\[\\#2A2B32\\].cursor-pointer.break-all.hover\\:pr-4.group' + ); + + conversations.forEach((conversation) => { + const checkbox = document.createElement('input'); + checkbox.type = 'checkbox'; + checkbox.className = 'conversation-checkbox'; + checkbox.addEventListener('click', (event) => { + event.stopPropagation(); + }); + conversation.insertBefore(checkbox, conversation.firstChild); + }); + } + + + + + + +chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { + if (request.message === 'add_checkboxes') { + addCheckboxes(); + } else if (request.message === 'delete_selected_conversations') { + deleteSelectedConversations(); + } +}); + +async function fetchAllConversations(offset = 0, limit = 20) { + const response = await fetch( + `https://chat.openai.com/backend-api/conversations?offset=${offset}&limit=${limit}` + ); + const data = await response.json(); + const conversations = data.items; + + if (conversations.length === limit) { + const nextConversations = await fetchAllConversations(offset + limit, limit); + return [...conversations, ...nextConversations]; + } + + return conversations; + } + + async function buildTitleToIdMap() { + const conversations = await fetchAllConversations(); + const titleToIdMap = {}; + + conversations.forEach((conversation) => { + const title = conversation.title; + if (titleToIdMap[title]) { + titleToIdMap[title].push(conversation.id); + } else { + titleToIdMap[title] = [conversation.id]; + } + }); + + return titleToIdMap; + } + + async function deleteSelectedConversations() { + const titleToIdMap = await buildTitleToIdMap(); + const checkboxes = document.querySelectorAll('.conversation-checkbox:checked'); + const conversationIds = []; + + checkboxes.forEach((checkbox) => { + const titleElement = checkbox.parentElement.querySelector('.text-ellipsis'); + const title = titleElement.textContent; + const conversationId = titleToIdMap[title].shift(); + + if (conversationId) { + conversationIds.push(conversationId); + } + }); + + await Promise.all( + conversationIds.map(async (id) => { + console.log(`try to delete conversation with ID: ${id}`); + await fetch(`https://chat.openai.com/backend-api/conversation/${id}`, { + method: 'PATCH', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + status: 'deleted', + }), + }); + + console.log(`Deleted conversation with ID: ${id}`); + }) + ); + + location.reload(); + } + + + + diff --git a/icon48.png b/icon48.png new file mode 100644 index 0000000000000000000000000000000000000000..45eef6b851b8ac05e85d5ac4993662acf4e0bb84 GIT binary patch literal 3750 zcmeHK{Zms{8a_7xLuDZZgklK+O44){&`1Z8f<{D2vJ`Pzhv@Pl79={W1c@3F!9=b2 z6+uH4M+1@Rs&!O8>S_`-5Q|1)AW>HYM-5N{R}n=oMu=XMJ$8TD{R8@o#h#lVqc8V z$+^}*b@h(!Cpuieb1^tIyi`0ZPM#1+U^7TZ|6S@jA4M@fVRxywTQsu+5cS?2U4wdD z|2f6DmVJ$S)S~f*HUmJsJWd`Gh)^3kEKz#=K0+}za&**cizb|cP!FeC8g=-6=qQTQ zLal;e5JIi)xEZCxIRG-*zl=3lG(^a(O1;^n$2pOZ$v!@I7=l5_SVM<74ua5U6X(bn z^ch4!#tx^No1i)L8OJ#=27LxWW_3qroDTnMB%v>ubZ|J=Jp&S}QaitklTY6RHeW@G zX4Y}9F9sG)Ma&>Zq8R_~PZOZnE_4q#yy3BKRvov(n_>(swCZIYCrzOermMRrsob8S zy0Y2n)7MC5xL%D?lE<|p>a4T0))Q4g$24|m}w%eGlojuWj{gbVk)^dS`<{A*7o7YbOibe1+P5%UdFX;drKLGwr z;8B~*ICDR1>s+&>$&Qo;dgh_{<(_D3aTiA*V%M`w5e(_jXgFZLv~y6n(G3Z0-XCyf zK4{r}4DQ4Iq-$W!7gyjp6hhI0G4hsMemYxCH>i-fJX#dBuwTB3nT!ZvK+WZqGt0V)v zPM4gs6G}_77}DG734Kqh)fA@i%k>`>W{(5*Hk%IDMuCe%hk}j1=!#1eq@_Nqi3Umu z4JX1V;DNNr2^2vuVjk@#BPB)3mzjKbtU3IQ7EtfT^V=$|F3p);>Vt_OOi%5rIR`sb zL4?>PN(bwnM#Ctmed*a`c0AH@ZkGTCJJ)v0&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ripts": [ + { + "matches": ["https://chat.openai.com/*"], + "js": ["content.js"] + } + ] + } + \ No newline at end of file diff --git a/popup.html b/popup.html new file mode 100644 index 0000000..b023e5d --- /dev/null +++ b/popup.html @@ -0,0 +1,16 @@ + + + + + + Bulk Delete ChatGPT Conversations + + + + + + + + + + diff --git a/popup.js b/popup.js new file mode 100644 index 0000000..562b47b --- /dev/null +++ b/popup.js @@ -0,0 +1,14 @@ +// popup.js +document.getElementById('add_checkboxes').addEventListener('click', () => { + chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => { + chrome.tabs.sendMessage(tabs[0].id, { message: 'add_checkboxes' }); + }); + }); + + document.getElementById('delete_selected').addEventListener('click', () => { + chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => { + chrome.tabs.sendMessage(tabs[0].id, { message: 'delete_selected_conversations' }); + }); + }); + + \ No newline at end of file