Created
February 23, 2023 04:31
-
-
Save Quorafind/7520767dda43c6e7abbb5751dd5c5f53 to your computer and use it in GitHub Desktop.
Use Quickadd to copy ChatGPT result
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
module.exports = {"Copy Chat History": copyChatWithHistory, "Copy Last Answer": copyLastAnswer}; | |
const ExportMD = (function () { | |
if (!window.TurndownService) return; | |
const hljsREG = /^.*(hljs).*(language-[a-z0-9]+).*$/i; | |
const turndownService = new window.TurndownService({ | |
headingStyle: "atx", | |
hr: "---", | |
bulletListMarker: "-", | |
codeBlockStyle: "fenced", | |
fence: "```", | |
linkStyle: "inlined" | |
}) | |
.addRule('code', { | |
filter: (node) => { | |
if (node.nodeName === 'CODE' && hljsREG.test(node.classList.value)) { | |
return 'code'; | |
} | |
}, | |
replacement: (content, node) => { | |
const classStr = node.getAttribute('class'); | |
if (hljsREG.test(classStr)) { | |
const lang = classStr.match(/.*language-(\w+)/)[1]; | |
if (lang) { | |
return `\`\`\`${lang}\n${content}\n\`\`\``; | |
} | |
return `\`\`\`\n${content}\n\`\`\``; | |
} | |
} | |
}) | |
.addRule('ignore', { | |
filter: ['button', 'img'], | |
replacement: () => '', | |
}) | |
.addRule('table', { | |
filter: 'table', | |
replacement: function(content, node) { | |
return `\`\`\`${content}\n\`\`\``; | |
}, | |
}); | |
return turndownService; | |
}({})); | |
async function copyChatWithHistory(params) { | |
const {app} = params; | |
const chatViewEl = app.workspace.getLeavesOfType("surfing-view").find((item)=>{ return /chat\.openai\.com/.test(item?.view?.currentUrl)}).view.webviewEl; | |
let content = ""; | |
chatViewEl.executeJavaScript(` | |
var content = ""; | |
content = Array.from(document.querySelectorAll('main .items-center>div')).map(i => { | |
let j = i.cloneNode(true); | |
if (/dark\:bg-gray-800/.test(i.getAttribute('class'))) { | |
j.innerHTML = "<blockquote>" + i.innerHTML + "</blockquote>"; | |
} | |
return j.innerHTML; | |
}).join('<hr />'); | |
`, true).then((result) => { | |
content = ExportMD.turndown(result).split("\n").map((line)=> { | |
return line.replace(/^ {4}(?=[\w\W])/gm, "").trim(); | |
}).join("\n"); | |
electron.clipboard.writeText(content); | |
}); | |
} | |
async function copyLastAnswer(params) { | |
const {app} = params; | |
const chatViewEl = app.workspace.getLeavesOfType("surfing-view").find((item)=>{ return /chat\.openai\.com/.test(item?.view?.currentUrl)}).view.webviewEl; | |
let content = ""; | |
chatViewEl.executeJavaScript(` | |
var content = ""; | |
content = Array.from(document.querySelectorAll('main .items-center>div')).map(i => { | |
let j = i.cloneNode(true); | |
if (/dark\:bg-gray-800/.test(i.getAttribute('class'))) { | |
j.innerHTML = "<blockquote>" + i.innerHTML + "</blockquote>"; | |
} | |
return j.innerHTML; | |
}); | |
`, true).then((result) => { | |
content = ExportMD.turndown(result.slice(-2)[0]).split("\n").map((line)=> { | |
return line.replace(/^ {4}(?=[\w\W])/gm, "").trim(); | |
}).join("\n"); | |
electron.clipboard.writeText(content); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment