Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ashtonmeuser/6056a4cd0329b6ff02045c1b282f7131 to your computer and use it in GitHub Desktop.
Save ashtonmeuser/6056a4cd0329b6ff02045c1b282f7131 to your computer and use it in GitHub Desktop.
Bookmarklet that lets you render a full HTML page with any included css and javascript that is currently copied to your clipboard. Also works for SVG code. Useful with ChatGPT Canvas
javascript:(function(){try{navigator.clipboard.readText().then(function(t){if(t){var e=window.open("","_blank","width=800,height=600");e.document.open(),e.document.write(t),e.document.close()}else alert("Clipboard is empty. Please copy some text to the clipboard first.")}).catch(function(t){console.error("Failed to read clipboard contents: ",t),alert("An error occurred while trying to access the clipboard. Please ensure your browser allows clipboard access.")})}catch(t){console.error("An error occurred:",t),alert("An error occurred while trying to open the new window with the clipboard content.")}})();//bookmarklet_title: HTML Preview from Clipboard
// bookmarklet_title: HTML Preview from Clipboard
// bookmarketl_about: Bookmarklet that lets you render a full HTML page with any included css and javascript that is currently copied to your clipboard. Also works for SVG code. Useful with ChatGPT Canvas.
const PROMPT = false; // bookmarklet_var(boolean): PROMPT
const handleText = (text?: string | null): void => {
if (!text) return alert("Clipboard is empty. Please copy some text to the clipboard first.");
var newWindow = window.open("", "_blank", "width=800,height=600");
newWindow!.document.open();
newWindow!.document.write(text);
newWindow!.document.close();
};
try {
if (PROMPT || !navigator.clipboard?.readText) {
handleText(prompt("Please paste the HTML content here:"));
} else {
navigator.clipboard.readText()
.then(handleText)
.catch(function (err) {
console.error("Failed to read clipboard contents: ", err);
alert("An error occurred while trying to access the clipboard. Please ensure your browser allows clipboard access.");
});
}
} catch (e) {
console.error("An error occurred:", e);
alert("An error occurred while trying to open the new window with the clipboard content.");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment