Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@GaurangTandon
Created February 14, 2021 06:46
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 GaurangTandon/caa1f2d2ab7ff78f91c5da863a3111bd to your computer and use it in GitHub Desktop.
Save GaurangTandon/caa1f2d2ab7ff78f91c5da863a3111bd to your computer and use it in GitHub Desktop.
Userscript to allow cloning with SSH directly from GitHub UI
// ==UserScript==
// @name SSH clone button
// @version 0.1
// @description For those who have SSH default in their repo
// @author Gaurang
// @match https://github.com/*/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
function insertAfter(refNode, newNode) {
refNode.parentNode.insertBefore(newNode, refNode.nextSibling);
}
function onDocLoad() {
let url = window.location.href;
let match = url.match(/^https:\/\/github.com\/(\w+)\/(\w+)\/?/i);
if (match) {
let author = match[1];
let reponame = match[2];
let clonessh = `git@github.com:${author}/${reponame}.git`;
let element = document.createElement("a");
element.className = "btn ml-2 d-none d-md-block";
element.innerText = "ssh";
element.addEventListener("click", function() {
// if your browser doesn't support window.navigator, then please upgrade it!
window.navigator.clipboard.writeText(clonessh).then(function() {
console.log('Copying to clipboard was successful!');
}, function(err) {
console.error('Could not copy text: ', err);
});
});
let toolbarDetailsBtn = document.querySelector(".file-navigation .flex-auto").nextElementSibling.nextElementSibling;
insertAfter(toolbarDetailsBtn, element);
}
}
if (document.readyState != "complete") {
window.addEventListener("load", onDocLoad);
} else {
onDocLoad();
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment