Last active
February 24, 2022 02:32
-
-
Save ye11ow/45e65b79a9575430b3c189e1003b2e90 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 Github | |
// @namespace http://tampermonkey.net/ | |
// @version 0.1 | |
// @description N/A | |
// @author You | |
// @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"; | |
// let lastUrl = location.href; | |
// new MutationObserver(() => { | |
// const url = location.href; | |
// if (url !== lastUrl) { | |
// lastUrl = url; | |
// onUrlChange(); | |
// } | |
// }).observe(document, { subtree: true, childList: true }); | |
// function onUrlChange() { | |
// add(); | |
// } | |
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 paths = location.pathname.split("/"); | |
if (paths.length < 5) { | |
return; | |
} | |
const project = capitalize(paths[2]); | |
const id = paths[4]; | |
const textBlob = new Blob([title], { 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