Skip to content

Instantly share code, notes, and snippets.

@nithinbekal
Last active September 11, 2023 13:55
Show Gist options
  • Save nithinbekal/89b53444942b1a3e8abb3934e3193b50 to your computer and use it in GitHub Desktop.
Save nithinbekal/89b53444942b1a3e8abb3934e3193b50 to your computer and use it in GitHub Desktop.
Bookmarklet to copy urls as markdown

Readme

Originally based on this bookmarklet. Updated to only link the issue/PR id for Github urls

Usage

  • Right click bookmarks bar and click "add page"
  • Change name to "Copy URL as markdown"
  • Change url to javascript:, followed by the bookmarklet script.
(function() {
function copyToClipboard(text) {
if (document.queryCommandSupported && document.queryCommandSupported("copy")) {
var textarea = document.createElement("textarea");
textarea.textContent = text;
textarea.style.position = "fixed"; /* Prevent scrolling to bottom of page in MS Edge.*/
document.body.appendChild(textarea);
textarea.select();
try {
return document.execCommand("copy"); /* Security exception may be thrown by some browsers.*/
} catch (ex) {
console.warn("Copy to clipboard failed.", ex);
return false;
} finally {
document.body.removeChild(textarea);
}
}
}
var markdown = null;
var urlObject = new URL(window.location.href);
urlObject.searchParams.delete('utm_source');
var url = urlObject.toString();
if (window.location.origin === "https://github.com") {
var titleBlock = document.querySelector(".gh-header-title");
if (titleBlock) {
var title = titleBlock.querySelector(".markdown-title").innerText;
var issue = titleBlock.querySelector(".color-fg-muted").innerText;
markdown = `[${title} - ${issue}](${url})`;
}
}
if (!markdown) {
markdown = `[${document.title}](${url})`;
}
copyToClipboard(markdown);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment