This is a little bookmarklet that copies a nicely formatted link to the current page to the clipboard. It sets text/html value on the clipboard, and also sets a text/plain value with a markdown representation of the link.
I’ve found it helpful when writing in markdown. I’ve used it converted to a Chrome extension, which has been nice too.
javascript: (() => {
async function copyToClipboard(text, html) {
const blobText = new Blob([text], { type: "text/plain" });
const blobHtml = new Blob([html], { type: "text/html" });
const data = [new ClipboardItem({
["text/plain"]: blobText,
["text/html"]: blobHtml,
})];
navigator.clipboard.write(data).then(
() => {},
() => {alert('Copy to clipboard failed for some reason.');}
);
}
/* Grab the URL of the current page */
const href = window.location.href;
/* Grab the title of the current page */
const rawTitle = document.title;
let title = rawTitle;
/* Optionally tweak the title based on some janky matching criteria */
/* For GitHub, I like having the repo an pr number first */
const ghPrRegex = /(.*) by (.*)Pull Request #(\d*)(.*)\/(.*)/;
if (ghPrRegex.test(title)) {
const [_0, prTitle, _2, prNumber, _4, prRepo] = ghPrRegex.exec(title);
title = `${prRepo}#${prNumber} - ${prTitle}`;
}
/* Set up the HTML and text parts */
const text = decodeURIComponent(`[${title}](${href})`);
const html = decodeURIComponent(`<a href='${href}'>${title}</a>`);
/* Copy the text and HTML to the clipboard */
copyToClipboard(text, html);
})();