Created
March 23, 2023 16:15
-
-
Save flymop/bcbb6b2c3089b2203b199570ab259d74 to your computer and use it in GitHub Desktop.
Clean ChatGPT "New Chat" Conversations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Clean New Chat Conversations | |
// @version 1 | |
// @description Deletes all conversations with the title "New chat" in ChatGPT chat page | |
// @match https://chat.openai.com/* | |
// ==/UserScript== | |
const KEY_ACCESS_TOKEN = "chatgpt_access_token"; | |
(async function() { | |
const accessToken = await getAccessToken(); | |
const resp = await fetch("https://chat.openai.com/backend-api/conversations?offset=0&limit=20", { | |
headers: { | |
"Authorization": `Bearer ${accessToken}` | |
} | |
}); | |
const data = await resp.json(); | |
const conversations = data.items; | |
for (const conversation of conversations) { | |
if (conversation.title === "New chat") { | |
await deleteConversation(accessToken, conversation.id); | |
} | |
} | |
})(); | |
async function deleteConversation(accessToken, conversationID) { | |
try { | |
const resp = await fetch(`https://chat.openai.com/backend-api/conversation/${conversationID}`, { | |
method: "PATCH", | |
headers: { | |
"Content-Type": "application/json", | |
"Authorization": `Bearer ${accessToken}`, | |
}, | |
body: JSON.stringify({ is_visible: false }) | |
}); | |
const data = await resp.json(); | |
console.log("Conversation deleted", data); | |
} catch (err) { | |
console.error("Error deleting conversation", err); | |
} | |
} | |
async function getAccessToken() { | |
const resp = await fetch('https://chat.openai.com/api/auth/session'); | |
if (resp.status === 403) { | |
throw new Error('403 FORBIDDEN'); | |
} | |
const data = await resp.json().catch(() => ({})); | |
if (!data.accessToken) { | |
throw new Error('CAN NOT GET ACCESS TOKEN'); | |
} | |
return data.accessToken; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment