Skip to content

Instantly share code, notes, and snippets.

@sportshead
Last active January 27, 2024 13:13
Show Gist options
  • Save sportshead/751617fda4939b81f732e50da470b26e to your computer and use it in GitHub Desktop.
Save sportshead/751617fda4939b81f732e50da470b26e to your computer and use it in GitHub Desktop.
Github Copy Repo
// ==UserScript==
// @name Github Copy Repo
// @namespace sportshead.dev
// @version 2024-01-27
// @description Press ⌘+⌃+C to copy the current repo (e.g. `sportshead/dotfiles`)
// @author sportshead
// @match https://github.com/*
// @icon https://github.githubassets.com/favicons/favicon-dark.png
// @grant none
// @run-at document-start
// @updateURL https://gist.github.com/sportshead/751617fda4939b81f732e50da470b26e/raw/github-copy-repo.user.js
// @downloadURL https://gist.github.com/sportshead/751617fda4939b81f732e50da470b26e/raw/github-copy-repo.user.js
// ==/UserScript==
(function () {
"use strict";
const TOAST_TTL = 2000;
document.addEventListener("keydown", (e) => {
if (e.metaKey && e.ctrlKey && e.key === "c") {
const repo =
window.location.pathname.split("/")[1] +
"/" +
window.location.pathname.split("/")[2];
navigator.clipboard.writeText(repo).then(() => {
if (!document.getElementById("__primerPortalRoot__")) {
const div = document.createElement("div");
div.id = "__primerPortalRoot__";
div.style.position = "absolute";
div.style.top = "0px";
div.style.left = "0px";
document.body.appendChild(div);
}
document.getElementById("__primerPortalRoot__").innerHTML =
`<div style="position:relative;z-index:1"><div class="p-1 position-fixed bottom-0 left-0 mb-3 ml-3"><div class="Toast Toast--animateIn" id="ui-app-toast" data-testid="ui-app-toast-info" role="log"><span class="Toast-icon"><svg aria-hidden="true" focusable="false" role="img" class="Octicon-sc-9kayk9-0" viewBox="0 0 16 16" width="16" height="16" fill="currentColor" style="display:inline-block;user-select:none;vertical-align:text-bottom;overflow:visible"><path d="M13.78 4.22a.75.75 0 0 1 0 1.06l-7.25 7.25a.75.75 0 0 1-1.06 0L2.22 9.28a.751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018L6 10.94l6.72-6.72a.75.75 0 0 1 1.06 0Z"></path></svg></span><span class="Toast-content">Copied "${repo}" to clipboard</span></div></div></div>`;
setTimeout(() => {
document
.getElementById("ui-app-toast")
.classList.replace(
"Toast--animateIn",
"Toast--animateOut",
);
}, TOAST_TTL - 300);
setTimeout(() => {
document.getElementById("__primerPortalRoot__").innerHTML =
"";
}, TOAST_TTL);
});
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment