-
-
Save k8scat/beee5cb8284e831dd805e925a21060e6 to your computer and use it in GitHub Desktop.
Add a "Copy link" button to Github issue and pull requests
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
// ==UserScript== | |
// @name Copy Link for GitHub Issues and PRs | |
// @namespace http://tampermonkey.net/ | |
// @version 0.2 | |
// @description Add a "Copy link" button to Github issue and pull requests | |
// @author ye11ow, K8sCat <[email protected]> | |
// @match https://github.com/* | |
// @include https://github.com/*/issues/* | |
// @include https://github.com/*/pull/* | |
// @icon https://www.google.com/s2/favicons?domain=github.com | |
// @grant none | |
// ==/UserScript== | |
(function () { | |
"use strict"; | |
function capitalize(word) { | |
const lower = word.toLowerCase(); | |
return lower.charAt(0).toUpperCase() + lower.slice(1); | |
} | |
function add() { | |
if (!document.querySelector(".gh-header-actions")) { | |
return; | |
} | |
const copyBtn = document.createElement("button"); | |
copyBtn.innerText = "Copy link"; | |
copyBtn.classList.add("btn"); | |
copyBtn.classList.add("btn-sm"); | |
copyBtn.style.marginRight = "8px"; | |
copyBtn.addEventListener("click", () => { | |
const title = capitalize( | |
document.querySelector(".js-issue-title.markdown-title").innerText | |
); | |
const number = capitalize( | |
document.querySelector(".f1-light.color-fg-muted").innerText | |
) | |
const paths = location.pathname.split("/"); | |
if (paths.length < 5) { | |
return; | |
} | |
const project = capitalize(paths[2]); | |
const id = paths[4]; | |
const textBlob = new Blob([`${number} ${title} ${location.href}`], { type: "text/plain" }); | |
const htmlBlob = new Blob( | |
[`${title} [<a href="${location.href}">${project}#${id}</a>]`], | |
{ type: "text/html" } | |
); | |
const data = new ClipboardItem({ | |
"text/plain": textBlob, | |
"text/html": htmlBlob, | |
}); | |
navigator.clipboard.write([data]); | |
}); | |
document.querySelector(".gh-header-actions").prepend(copyBtn); | |
}; | |
add(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment