bookmarklets icon indicating copy to clipboard operation
bookmarklets copied to clipboard

Current version broken. A fix proposed.

Open nw-git opened this issue 7 months ago • 1 comments

ChatGPT and Claude changed some DOM elements so the current 0.12 does not work. I also noticed that printing looks much better by default now, so maybe this bookmarklet isn't needed anymore? That said, I managed to fix it, at least today it works on ChatGPT, Grok, and Claude.

Here is what I had to do to get it working on 07 APR 2026:

javascript:(function () { /* v. 0.12, github.com/give-me/bookmarklets */ let dialog, events = [], extras = [], csp = false; switch (location.hostname) { case 'claude.ai': dialog = document.querySelector('div[data-test-render-count]').parentElement; events = dialog.querySelectorAll('div[data-testid="user-message"], div[data-test-render-count]>div>div>div.font-claude-response'); extras.push(document.querySelector('div.h-full.top-0 div.font-mono')); extras.push(document.querySelector('div.h-full.top-0 div#wiggle-file-content')); extras.push(document.querySelector('div.h-full.top-0 div#markdown-artifact')); csp = true; break; case 'chatgpt.com': dialog = document.querySelector('div.flex.flex-col.text-sm'); events = dialog.querySelectorAll('div[data-message-author-role]'); extras.push(document.querySelector('section.popover>section')); csp = true; break; case 'grok.com': dialog = document.querySelector('div#last-reply-container').parentElement; events = dialog.querySelectorAll('div.message-bubble'); extras.push(document.querySelector('aside')); csp = true; break; case 'gemini.google.com': dialog = document.querySelector('#chat-history'); events = dialog.querySelectorAll('user-query-content, message-content'); extras.push(document.querySelector('code-immersive-panel>div.container')); extras.push(document.querySelector('deep-research-immersive-panel>div.container')); extras.push(document.querySelector('extended-response-panel response-container')); csp = true; break; default: return alert(location.hostname + ' is not supported'); } events = [...events].filter(Boolean); extras = [...extras].filter(Boolean); console.group(Found elements at ${location.hostname}:); console.debug('dialog', dialog); console.debug('events', events); console.debug('extras', extras); console.groupEnd(); let blocks = [dialog, ...extras]; let ts = new Date().toISOString().replace(/[-:T.]/g, '').slice(0, 14); if (confirm('Export as PDF? (Cancel for plain text)')) { if (csp || confirm('Confirm if the PDF should be searchable')) { let temp = document.createElement('div'); temp.id = 'id-' + Math.random().toString(36).slice(2, 9); blocks.forEach(el => temp.appendChild(el.cloneNode(true))); let style = document.createElement('style'); style.textContent = @media print{body>*{display:none!important}#${temp.id}{display:flex!important;flex-direction:column}}; document.head.appendChild(style); document.body.appendChild(temp); print(); setTimeout(() => { document.head.removeChild(style); document.body.removeChild(temp); }, 1000); } else { let script = document.createElement('script'); script.src = 'https://cdnjs.cloudflare.com/ajax/libs/html2pdf.js/0.12.1/html2pdf.bundle.min.js'; script.onload = function () { let pdf = html2pdf().set({ margin: 5, filename: ${ts}.pdf, html2canvas: {scale: 2, logging: false} }).from(blocks.shift()); blocks.forEach(el => pdf = pdf.toPdf().get('pdf').then(pdfObj => pdfObj.addPage()).from(el).toContainer().toCanvas().toPdf()); pdf.save(); }; document.body.appendChild(script); } } else { let txt = events.map((e, i) => # ${i % 2 ? 'AI' : 'Me'}:\n\n${e.innerText.trim()}\n\n).join(''); txt += extras.map((e, i) => # Extra ${i + 1}:\n\n${e.innerText.trim()}\n\n).join(''); let href = URL.createObjectURL(new Blob(['\uFEFF', txt], {type: 'text/plain;charset=utf-8'})); let link = Object.assign(document.createElement('a'), {href: href, download: ${ts}.txt}); link.click(); URL.revokeObjectURL(link.href); } })();

nw-git avatar Apr 08 '26 02:04 nw-git

Fixed. Sorry for the delay.

give-me avatar May 08 '26 21:05 give-me