Created
February 14, 2021 06:46
-
-
Save GaurangTandon/caa1f2d2ab7ff78f91c5da863a3111bd to your computer and use it in GitHub Desktop.
Userscript to allow cloning with SSH directly from GitHub UI
This file contains 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 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 = `[email protected]:${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