Skip to content

Commit

Permalink
Compile script and dependency because Open AI's Content-Security-Poli…
Browse files Browse the repository at this point in the history
…cy forbids loading external scripts.

Use turndown instead of html-to-markdown.
  • Loading branch information
yaph committed Dec 6, 2023
1 parent a480a1f commit 8cd7723
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 34 deletions.
63 changes: 31 additions & 32 deletions gpt2md.js
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();
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "chatgpt-export",
"version": "1.0.0",
"version": "2.0.0",
"main": "gpt2md.js",
"scripts": {
"build": "./build.js"
Expand All @@ -13,6 +13,6 @@
"dependencies": {
"esbuild": "0.17.18",
"esbuild-plugin-bookmarklet": "^1.0.0",
"html-to-md": "^0.8.3"
"turndown": "^7.1.2"
}
}

0 comments on commit 8cd7723

Please sign in to comment.