Skip to content

Instantly share code, notes, and snippets.

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