forked from yaph/chatgpt-export
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Compile script and dependency because Open AI's Content-Security-Poli…
…cy forbids loading external scripts. Use turndown instead of html-to-markdown.
- Loading branch information
Showing
2 changed files
with
33 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,36 @@ | ||
const script = document.createElement('script'); | ||
script.src = 'https://unpkg.com/[email protected]'; | ||
script.onload = function () { | ||
// Clone to not modify the actual `document.body` in the code that follows | ||
const body = document.body.cloneNode(true); | ||
const TurndownService = require('turndown'); | ||
const ts = new TurndownService(); | ||
|
||
// Remove code box headers | ||
body.querySelectorAll('pre .text-xs').forEach(n => n.parentNode?.removeChild(n)); | ||
// Clone to not modify the actual `document.body` in the code that follows | ||
const body = document.body.cloneNode(true); | ||
|
||
// Remove prompt/response numbers | ||
body.querySelectorAll('div .text-xs.gap-1').forEach(n => n.parentNode?.removeChild(n)); | ||
// Remove code box headers | ||
body.querySelectorAll('pre .text-xs').forEach(n => n.parentNode?.removeChild(n)); | ||
|
||
// Remove footer | ||
body.querySelector('.absolute.bottom-0').remove() | ||
// Remove prompt/response numbers | ||
body.querySelectorAll('div .text-xs.gap-1').forEach(n => n.parentNode?.removeChild(n)); | ||
|
||
// Iterate through main text containers and create text to export | ||
let text = `# ${document.title}\n\n`; | ||
body.querySelectorAll('.p-4.text-base').forEach((n, i) => { | ||
const num = Math.trunc(i / 2) + 1; | ||
if (n.querySelector('img[alt="User"]')) { | ||
// Keep prompt text as it was entered | ||
text += `## PROMPT ${num}\n\n${n.querySelector('.empty\\:hidden').innerHTML}\n\n`; | ||
} else { | ||
// Only convert response markup to markdown | ||
text += `## RESPONSE ${num}\n\n${html2md(n.querySelector('.prose').innerHTML)}\n\n`; | ||
} | ||
}); | ||
// Remove footer | ||
body.querySelector('.absolute.bottom-0').remove() | ||
|
||
// Download | ||
const a = document.createElement('a'); | ||
a.download = `${document.title}.md`; | ||
a.href = URL.createObjectURL(new Blob([text])); | ||
a.style.display = 'none'; | ||
document.body.appendChild(a); | ||
a.click(); | ||
}; | ||
document.body.appendChild(script); | ||
// Iterate through main text containers and create text to export | ||
let text = `# ${document.title}\n\n`; | ||
body.querySelectorAll('.text-message').forEach((n, i) => { | ||
const num = Math.trunc(i / 2) + 1; | ||
const prose = n.querySelector('.prose'); | ||
if (prose) { | ||
// Only convert response markup to markdown | ||
text += `## RESPONSE ${num}\n\n${ts.turndown(prose.innerHTML)}\n\n`; | ||
} else { | ||
// Keep prompt text as it was entered | ||
text += `## PROMPT ${num}\n\n${n.querySelector('div').innerHTML}\n\n`; | ||
} | ||
}); | ||
|
||
// Download | ||
const a = document.createElement('a'); | ||
a.download = `${document.title}.md`; | ||
a.href = URL.createObjectURL(new Blob([text])); | ||
a.style.display = 'none'; | ||
document.body.appendChild(a); | ||
a.click(); |
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