Skip to content

Instantly share code, notes, and snippets.

@iethree
Last active October 24, 2024 19:48
Show Gist options
  • Select an option

  • Save iethree/9e39fd79e3d2e7f55aed39643066be59 to your computer and use it in GitHub Desktop.

Select an option

Save iethree/9e39fd79e3d2e7f55aed39643066be59 to your computer and use it in GitHub Desktop.
Github PR/Issue copy bookmarklet
javascript: (
function copyLink() {
const url = location.href;
const m = url.match(/https:\/\/github.com\/metabase\/metabase\/(issues|pull)\/(\d+)/);
if (m) {
let [_,type,id] = m;
const title = document.querySelector('.markdown-title').textContent;
const isDraft = !!document.querySelector('.gh-header-meta [title="Status: Draft"]');
const isMerged = !!document.querySelector('.gh-header-meta [title="Status: Merged"]');
const emoji = {
issues: ":github-issue:",
pull: ":pull-request:",
draft: ":draft-pull-request:",
merged: ":merged:"
};
if (isDraft) type = 'draft';
if (isMerged) type = 'merged';
const link = `${emoji[type]} <a href="${url}" target="_blank">${title}</a>`;
function copyToClip(str) {
function listener(e) {
e.clipboardData.setData("text/html", str);
e.clipboardData.setData("text/plain", str);
e.preventDefault();
}
document.addEventListener("copy", listener);
document.execCommand("copy");
document.removeEventListener("copy", listener);
};
copyToClip(link);
}
} )()
@iethree
Copy link
Copy Markdown
Author

iethree commented Sep 9, 2024

@appleby
Copy link
Copy Markdown

appleby commented Sep 9, 2024

Apparently in Safari you have to select some text first in order for the document.execCommand("copy"); to actually fire the event listener.

https://bugs.webkit.org/show_bug.cgi?id=156529
https://stackoverflow.com/questions/71340178/combination-of-document-addeventlistener-and-document-execcommandcopy-does-n

@nemanjaglumac
Copy link
Copy Markdown

I can't get copyToClip to work in Brave, either (even when I select the text first. It copies the whole script instead of the contents of the title.

What does work is: navigator.clipboard.writeText(link)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment