Skip to content

Instantly share code, notes, and snippets.

@mpontus
Last active July 30, 2021 22:00
Show Gist options
  • Save mpontus/b0e70434452acedca0d93b07387ed19e to your computer and use it in GitHub Desktop.
Save mpontus/b0e70434452acedca0d93b07387ed19e to your computer and use it in GitHub Desktop.
Copy current page link as markdown

Grab the bookmarklet here: Bookmarklet Generator - ShareProgress


In a much simpler form it can also work like this:

javascript:prompt("Copy page link", `[${document.title}](${window.location.href})`)
/* Extract canonical page url */
const { href } =
document.querySelector("link[href][rel=shortlink]") ||
document.querySelector("link[href][rel=canonical]") ||
window.location;
const url = href.replace(/(?:^|&)utm_[^&]+/g, "").replace(/^&/, "");
const link = `[${document.title}](${url})`;
/* Copy markdown contents to clipboard */
try {
const element = document.createElement("textarea");
const selection = document.getSelection();
element.textContent = link;
document.body.appendChild(element);
selection.removeAllRanges();
element.select();
document.execCommand("copy");
selection.removeAllRanges();
element.remove();
} catch (e) {
prompt("Failed to copy to clipboard", link);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment