-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support JSON export and export All
- Loading branch information
Showing
17 changed files
with
540 additions
and
64 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import JSZip from 'jszip' | ||
import { type ApiConversationWithId, fetchConversation, getCurrentChatId, processConversation } from '../api' | ||
import { downloadFile, getFileNameWithFormat } from '../utils/download' | ||
import { checkIfConversationStarted, getConversationChoice } from '../page' | ||
|
||
export async function exportToJson(fileNameFormat: string) { | ||
if (!checkIfConversationStarted()) { | ||
alert('Please start a conversation first.') | ||
return false | ||
} | ||
|
||
const chatId = await getCurrentChatId() | ||
const rawConversation = await fetchConversation(chatId) | ||
const conversationChoices = getConversationChoice() | ||
const conversation = processConversation(rawConversation, conversationChoices) | ||
|
||
const fileName = getFileNameWithFormat(fileNameFormat, 'json', { title: conversation.title }) | ||
const content = conversationToJson(rawConversation) | ||
downloadFile(fileName, 'application/json', content) | ||
|
||
return true | ||
} | ||
|
||
export async function exportAllToJson(fileNameFormat: string, conversationIds: string[]) { | ||
const conversations = await Promise.all( | ||
conversationIds.map(async (id) => { | ||
const rawConversation = await fetchConversation(id) | ||
const conversation = processConversation(rawConversation) | ||
return { conversation, rawConversation } | ||
}), | ||
) | ||
|
||
const zip = new JSZip() | ||
conversations.forEach(({ conversation, rawConversation }) => { | ||
const fileName = getFileNameWithFormat(fileNameFormat, 'json', { title: conversation.title }) | ||
const content = conversationToJson(rawConversation) | ||
zip.file(fileName, content) | ||
}) | ||
|
||
const blob = await zip.generateAsync({ type: 'blob' }) | ||
downloadFile('chatgpt-export.zip', 'application/zip', blob) | ||
|
||
return true | ||
} | ||
|
||
function conversationToJson(conversation: ApiConversationWithId) { | ||
return JSON.stringify(conversation) | ||
} |
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
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
Oops, something went wrong.