Last active
May 12, 2025 21:06
-
-
Save ewinnington/74240829519b7e90199a8cfc847f4f33 to your computer and use it in GitHub Desktop.
bookmarklet to extract chatgpt bubble to markdown on clipboard
This file contains hidden or 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
(function () { | |
// Minimal inline HTML-to-Markdown converter. | |
function htmlToMarkdown(el) { | |
// Recursive function to process nodes. | |
function processNode(node, parentTag) { | |
if (node.nodeType === Node.TEXT_NODE) { | |
return node.textContent; | |
} | |
if (node.nodeType !== Node.ELEMENT_NODE) { | |
return ""; | |
} | |
const tag = node.tagName.toLowerCase(); | |
let content = ""; | |
// Process all child nodes. | |
node.childNodes.forEach((child) => { | |
content += processNode(child, tag); | |
}); | |
switch (tag) { | |
case "h1": | |
return "\n# " + content.trim() + "\n\n"; | |
case "h2": | |
return "\n## " + content.trim() + "\n\n"; | |
case "h3": | |
return "\n### " + content.trim() + "\n\n"; | |
case "h4": | |
return "\n#### " + content.trim() + "\n\n"; | |
case "h5": | |
return "\n##### " + content.trim() + "\n\n"; | |
case "h6": | |
return "\n###### " + content.trim() + "\n\n"; | |
case "p": | |
return "\n" + content.trim() + "\n\n"; | |
case "br": | |
return "\n"; | |
case "strong": | |
case "b": | |
return "**" + content.trim() + "**"; | |
case "em": | |
case "i": | |
return "*" + content.trim() + "*"; | |
case "code": | |
// If inside a <pre>, let the <pre> handle the formatting. | |
if (parentTag === "pre") { | |
return content; | |
} | |
return "`" + content.trim() + "`"; | |
case "pre": | |
return "\n```\n" + content.trim() + "\n```\n\n"; | |
case "blockquote": | |
return "\n> " + content.trim().replace(/\n/g, "\n> ") + "\n\n"; | |
case "ul": { | |
let list = ""; | |
node.childNodes.forEach((child) => { | |
if ( | |
child.nodeType === Node.ELEMENT_NODE && | |
child.tagName.toLowerCase() === "li" | |
) { | |
const liContent = processNode(child, tag).trim(); | |
list += "- " + liContent + "\n"; | |
} | |
}); | |
return "\n" + list + "\n"; | |
} | |
case "ol": { | |
let list = ""; | |
let i = 1; | |
node.childNodes.forEach((child) => { | |
if ( | |
child.nodeType === Node.ELEMENT_NODE && | |
child.tagName.toLowerCase() === "li" | |
) { | |
const liContent = processNode(child, tag).trim(); | |
list += i + ". " + liContent + "\n"; | |
i++; | |
} | |
}); | |
return "\n" + list + "\n"; | |
} | |
case "li": | |
return content.trim() + "\n"; | |
case "a": { | |
const href = node.getAttribute("href") || ""; | |
return "[" + content.trim() + "](" + href + ")"; | |
} | |
case "img": { | |
const src = node.getAttribute("src") || ""; | |
const alt = node.getAttribute("alt") || ""; | |
return ""; | |
} | |
default: | |
// For other tags (such as <div> or <span>), just return their content. | |
return content; | |
} | |
} | |
return processNode(el, null); | |
} | |
// 1. Find the ChatGPT output bubble. | |
// (If needed, adjust the selector below.) | |
const bubbles = document.querySelectorAll(".markdown"); | |
if (!bubbles.length) { | |
alert("No ChatGPT output bubble found. Please check your selector."); | |
return; | |
} | |
const bubble = bubbles[bubbles.length - 1]; // Use the last found bubble. | |
// 2. Convert its HTML content to Markdown. | |
const markdown = htmlToMarkdown(bubble); | |
// 3. Output the Markdown. | |
console.log("Converted Markdown:\n", markdown); | |
// Attempt to copy the Markdown to clipboard. | |
if (navigator.clipboard) { | |
navigator.clipboard | |
.writeText(markdown) | |
.then(() => { | |
console.log("Markdown copied to clipboard!"); | |
}) | |
.catch((err) => { | |
console.warn("Failed to copy to clipboard:", err); | |
}); | |
} | |
// Also display it in a prompt for manual copying. | |
prompt("Markdown:", markdown); | |
})(); |
This file contains hidden or 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
javascript:(function(){function e(n,t){if(n.nodeType===Node.TEXT_NODE)return n.textContent;if(n.nodeType!==Node.ELEMENT_NODE)return"";const r=n.tagName.toLowerCase();let o="";n.childNodes.forEach((c)=>{o+=e(c,r)});switch(r){case"h1":return"\n# "+o.trim()+"\n\n";case"h2":return"\n## "+o.trim()+"\n\n";case"h3":return"\n### "+o.trim()+"\n\n";case"h4":return"\n#### "+o.trim()+"\n\n";case"h5":return"\n##### "+o.trim()+"\n\n";case"h6":return"\n###### "+o.trim()+"\n\n";case"p":return"\n"+o.trim()+"\n\n";case"br":return"\n";case"strong":case"b":return"**"+o.trim()+"**";case"em":case"i":return"*"+o.trim()+"*";case"code":return t==="pre"?o:"`"+o.trim()+"`";case"pre":return"\n```\n"+o.trim()+"\n```\n\n";case"blockquote":return"\n> "+o.trim().replace(/\n/g,"\n> ")+"\n\n";case"ul":{let c="";n.childNodes.forEach((l)=>{if(l.nodeType===Node.ELEMENT_NODE&&l.tagName.toLowerCase()==="li"){const d=e(l,r).trim();c+="- "+d+"\n"}});return"\n"+c+"\n"}case"ol":{let c="";let l=1;n.childNodes.forEach((d)=>{if(d.nodeType===Node.ELEMENT_NODE&&d.tagName.toLowerCase()==="li"){const p=e(d,r).trim();c+=l+". "+p+"\n";l++}});return"\n"+c+"\n"}case"li":return o.trim()+"\n";case"a":{const c=n.getAttribute("href")||"";return"["+o.trim()+"]("+c+")"}case"img":{const c=n.getAttribute("src")||"",l=n.getAttribute("alt")||"";return""}default:return o}}const s=document.querySelectorAll(".markdown");if(!s.length){alert("No ChatGPT output bubble found. Please check your selector.");return}const i=s[s.length-1],a=e(i,null);console.log("Converted Markdown:\n",a);navigator.clipboard&&navigator.clipboard.writeText(a).then(()=>{console.log("Markdown copied to clipboard!")}).catch((n)=>{console.warn("Failed to copy to clipboard:",n)});prompt("Markdown:",a)})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can copy-paste the minified into a bookmark button as the "url" in the browser to export the last chat bubble.