Skip to content

Instantly share code, notes, and snippets.

@oaustegard
Last active March 15, 2023 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oaustegard/055dab3b917b7ede0f4462056b61522c to your computer and use it in GitHub Desktop.
Save oaustegard/055dab3b917b7ede0f4462056b61522c to your computer and use it in GitHub Desktop.
Bookmarklets
//javascript:
(function () {
if (typeof turndownService === 'undefined') {
const script = document.createElement('script');
script.onload = function () {
convertSelectedHtmlToMarkdown();
};
script.src = 'https://cdn.jsdelivr.net/npm/turndown/dist/turndown.js';
document.head.appendChild(script);
} else {
convertSelectedHtmlToMarkdown();
}
function convertSelectedHtmlToMarkdown() {
const html = window.getSelection().toString();
const div = document.createElement('div');
div.innerHTML = html.trim();
const turndownService = new TurndownService();
const md = turndownService.turndown(div.innerHTML);
navigator.clipboard.writeText(md);
alert('Copied HTML as Markdown to clipboard!');
}
})();
//javascript:
(function(){
const linkText = document.title;
const linkUrl = location.href;
const postBody = document.querySelector('meta[property="og:description"]').content;
const postTime = document.querySelector('meta[property="og:published_time"]').content;
const markdown = `[${linkText}](${linkUrl})\n\n>${postBody}\n>*${postTime}*`;
navigator.clipboard.writeText(markdown);
alert("Markdown preview copied to clipboard!");
})();
//javascript:
(function () {
const urlRegex = /^https?:\/\/twitter\.com\/.+\/status\/\d+/;
const titleRegex = /^(.+) on Twitter: "(.+)" \/ Twitter$/;
const url = window.location.href;
const title = window.document.title;
if (!urlRegex.test(url)) {
alert("This bookmarklet only works on Twitter tweet pages");
return;
}
const match = title.match(titleRegex);
const personName = match[1];
const tweetBody = match[2].replace(/https?:\/\/t\.co\/\w+/, "").trim();
const markdown = `[${personName} on Twitter:](${url})\n> ${tweetBody}`;
navigator.clipboard.writeText(markdown);
alert("Converted tweet to markdown and copied to clipboard!");
})();
//javascript:
(function() {
const githubAccessToken = 'github_pat_here';
const dom = document.querySelector('main > .flex-1 > .h-full .flex:has(> .w-full)');
const template = document.createElement('template');
template.innerHTML = dom.innerHTML;
['.items-end', 'img', 'svg', 'button', ':empty'].forEach(selector => {
template.content.querySelectorAll(selector).forEach(node => {
node.remove();
});
});
const title = document.title.replace(/[^a-z0-9]/gi, '_');
const content = `<!DOCTYPE html>
<html lang="en">
<head>
<title>Chat GPT: ${title}</title>
<meta name="generator" content="chatGPT Saving Bookmark"/>
<style>
body {
background-color: rgb(32,33,35);
color: rgb(236,236,241);
font-size: 20px;
font-family: Calibri,Arial, sans-serif;
margin: -10px;
}
body > .w-full {
padding: 40px;
}
/* prompt */
body > .w-full:nth-child(2n+1) {
background: rgb(52,53,65);
}
/* response */
body > .w-full:nth-child(2n+2) {
background: rgb(68,70,84);
}
.whitespace-pre-wrap {
white-space: pre-wrap;
}
.flex-col {
max-width: 1000px;
margin: 0px auto;
}
</style>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.7.0/build/styles/default.min.css"/>
</head>
<body>${template.innerHTML}</body>
</html>`;
const gistDescription = `Chat GPT transcript: ${title}`;
const gistFilename = `${title}.html`;
const gistContent = {};
gistContent[gistFilename] = { content };
const data = {
description: gistDescription,
public: false,
files: gistContent,
};
fetch('https://api.github.com/gists', {
method: 'POST',
headers: {
'Authorization': `token ${githubAccessToken}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
})
.then(response => response.json())
.then(gist => {
const gistId = gist.id;
const previewUrl = `https://austegard.com/pv?${gistId}`;
navigator.clipboard.writeText(previewUrl).then(() => {
console.log('Preview url copied to clipboard:', previewUrl);
}).catch(error => {
console.error('Failed to copy preview url to clipboard:', error);
});
window.open(previewUrl);
})
.catch(error => {
console.error(error);
alert('An error occurred while creating the Gist.');
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment