{ "timestamp": "2024-10-05T04:58:23.690Z", "conversation": [ { "r": "user", "t": [ [ "
Here’s an updated version of the scriptlet. It now identifies assistant",
"and user messages by using the class selectors you specified. It also handles",
"the conversion of img
tags to Markdown format while retaining their",
"order in the text, by working with the entire turn's innerHTML:
javascriptjavascript:(function() {" ], [ "try {" ], [ "/* Wrap text at word boundaries */" ], [ "function wrapText(t) {" ], [ "return t.split(' ').reduce((l,w)=>(l[l.length-1]+w).length>80?(l.push(w+' '),l):(l[l.length-1]+=w+' ',l),['']).map(x=>x.trim()).filter(x=>x);" ], [ "}" ], [], [ "/* Process image tags to Markdown format", "*/" ], [ "function processImages(html) {" ], [ "var temp = document.createElement('div');" ], [ "temp.innerHTML = html;" ], [ "temp.querySelectorAll('img').forEach(function(img) {" ], [ "var alt = img.getAttribute('alt') || '';" ], [ "var src = img.getAttribute('src') || '';" ], [ "img.replaceWith(`![${alt}](${src})`);" ], [ "});" ], [ "return temp.innerHTML;" ], [ "}" ], [], [ "/* Extract messages */" ], [ "var c = [];" ], [ "document.querySelectorAll('.group\\\\/conversation-turn').forEach(function(turn) {" ], [ "var role = turn.classList.contains('agent-turn') ?", "'assistant' : 'user';" ], [ "var html = turn.innerHTML;" ], [ "html = processImages(html);" ], [ "var r = html.split('\\n').map(function(x) { return wrapText(x); });" ], [ "c.push({ r: role, t: r });" ], [ "});" ], [], [ "/* JSON output */" ], [ "var j = JSON.stringify(c, null, 2), w = window.open();" ], [ "w.document.body.innerHTML =", "'<pre>' + j + '</pre>';" ], [], [ "/* Copy to clipboard quietly */" ], [ "navigator.clipboard.writeText(j).catch(e => alert('Copy failed: ' +", "e.message));" ], [], [ "/* Download JSON file */" ], [ "var d = new Date().toISOString().slice(0, 10), b = new Blob([j], { type: 'application/json' }), a = document.createElement('a');" ], [ "a.href = URL.createObjectURL(b); a.download = 'conv_'", "+ d + '.json.txt'; a.click();" ], [ "} catch (e) {" ], [ "alert('Error: ' + e.message);" ], [ "}" ], [ "})();" ], [ "
.group\\\\/conversation-turn
",
"selector and checked for the .agent-turn
class to distinguish",
"between assistant and user messages.<img>
tags to Markdown format,",
"preserving their alt
and src
attributes in the",
"output.innerHTML
instead of innerText
to ensure images and",
"other inline HTML elements are captured properly.This script", "processes images and retains them in the correct order within the message. It", "also continues to wrap text at word boundaries and outputs the results in JSON", "format.
Here's an updated version of the scriptlet that:
innerHTML
without converting images to",
"Markdown.javascriptjavascript:(function() {" ], [ "try {" ], [ "/* Wrap text at word boundaries */" ], [ "function wrapText(t) {" ], [ "return t.split(' ').reduce((l,w)=>(l[l.length-1]+w).length>80?(l.push(w+' '),l):(l[l.length-1]+=w+' ',l),['']).map(x=>x.trim()).filter(x=>x);" ], [ "}" ], [], [ "/* Escape HTML content for display in the new", "window */" ], [ "function escapeHTML(html) {" ], [ "return html.replace(/</g, '<').replace(/>/g, '>');" ], [ "}" ], [], [ "/* Extract messages */" ], [ "var c = [];" ], [ "document.querySelectorAll('.group\\\\/conversation-turn').forEach(function(turn) {" ], [ "var role = turn.classList.contains('agent-turn') ?", "'assistant' : 'user';" ], [ "var html = turn.innerHTML;" ], [ "var r = html.split('\\n').map(function(x) { return wrapText(x); });" ], [ "c.push({ r: role, t: r });" ], [ "});" ], [], [ "/* JSON with timestamp */" ], [ "var output = {" ], [ "timestamp: new Date().toISOString()," ], [ "conversation: c" ], [ "};" ], [], [ "/* Stringify JSON */" ], [ "var j = JSON.stringify(output,", "null, 2);" ], [], [ "/* Open a new window with escaped HTML */" ], [ "var w = window.open();" ], [ "w.document.body.innerHTML =", "'<pre>' + escapeHTML(j) + '</pre>';" ], [], [ "/* Copy to clipboard quietly */" ], [ "navigator.clipboard.writeText(j).catch(e => alert('Copy failed: ' +", "e.message));" ], [], [ "/* Download JSON file */" ], [ "var d = new Date().toISOString().slice(0, 10), b = new Blob([j], { type: 'application/json' }), a = document.createElement('a');" ], [ "a.href = URL.createObjectURL(b); a.download = 'conv_'", "+ d + '.json.txt'; a.click();" ], [ "} catch (e) {" ], [ "alert('Error: ' + e.message);" ], [ "}" ], [ "})();" ], [ "
innerHTML
as-is and includes it in the",
"JSON output.escapeHTML
",
"function ensures that the HTML in the new window is safely escaped, so it is",
"displayed correctly as text.timestamp
field at the root of the JSON object to store the time",
"when the conversation was exported.This script preserves the raw", "HTML content and escapes it for safe display in the new window while keeping the", "JSON structure neat with a timestamp at the top", "level.
Here's an updated version of the scriptlet that removes",
"<svg>
objects from the innerHTML
before",
"outputting the content:
javascriptjavascript:(function() {" ], [ "try {" ], [ "/* Wrap text at word boundaries */" ], [ "function wrapText(t) {" ], [ "return t.split(' ').reduce((l,w)=>(l[l.length-1]+w).length>80?(l.push(w+' '),l):(l[l.length-1]+=w+' ',l),['']).map(x=>x.trim()).filter(x=>x);" ], [ "}" ], [], [ "/* Escape HTML content for display in the new", "window */" ], [ "function escapeHTML(html) {" ], [ "return html.replace(/</g, '<').replace(/>/g, '>');" ], [ "}" ], [], [ "/* Remove SVG objects from the HTML */" ], [ "function removeSVG(html) {" ], [ "var temp = document.createElement('div');" ], [ "temp.innerHTML = html;" ], [ "temp.querySelectorAll('svg').forEach(function(svg) {" ], [ "svg.remove();" ], [ "});" ], [ "return temp.innerHTML;" ], [ "}" ], [], [ "/* Extract messages */" ], [ "var c = [];" ], [ "document.querySelectorAll('.group\\\\/conversation-turn').forEach(function(turn) {" ], [ "var role = turn.classList.contains('agent-turn') ?", "'assistant' : 'user';" ], [ "var html = turn.innerHTML;" ], [ "html = removeSVG(html);" ], [ "var r = html.split('\\n').map(function(x) { return wrapText(x); });" ], [ "c.push({ r: role, t: r });" ], [ "});" ], [], [ "/* JSON with timestamp */" ], [ "var output = {" ], [ "timestamp: new Date().toISOString()," ], [ "conversation: c" ], [ "};" ], [], [ "/* Stringify JSON */" ], [ "var j = JSON.stringify(output,", "null, 2);" ], [], [ "/* Open a new window with escaped HTML */" ], [ "var w = window.open();" ], [ "w.document.body.innerHTML =", "'<pre>' + escapeHTML(j) + '</pre>';" ], [], [ "/* Copy to clipboard quietly */" ], [ "navigator.clipboard.writeText(j).catch(e => alert('Copy failed: ' +", "e.message));" ], [], [ "/* Download JSON file */" ], [ "var d = new Date().toISOString().slice(0, 10), b = new Blob([j], { type: 'application/json' }), a = document.createElement('a');" ], [ "a.href = URL.createObjectURL(b); a.download = 'conv_'", "+ d + '.json.txt'; a.click();" ], [ "} catch (e) {" ], [ "alert('Error: ' + e.message);" ], [ "}" ], [ "})();" ], [ "
removeSVG
function removes all <svg>
",
"elements from the innerHTML
before output.This updated version", "strips out all SVG objects while keeping the rest of the HTML intact. It also", "retains the escaping of HTML for safe display in a new", "window.