Skip to content

Instantly share code, notes, and snippets.

@ye11ow
Last active February 24, 2022 02:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ye11ow/45e65b79a9575430b3c189e1003b2e90 to your computer and use it in GitHub Desktop.
Save ye11ow/45e65b79a9575430b3c189e1003b2e90 to your computer and use it in GitHub Desktop.
Add a "Copy link" button to Github issue and pull requests
// ==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