-
-
Save iethree/9e39fd79e3d2e7f55aed39643066be59 to your computer and use it in GitHub Desktop.
Github PR/Issue copy bookmarklet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| } | |
| } )() |
Author
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
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
How to add a bookmarklet: https://mreidsma.github.io/bookmarklets/installing.html