Skip to content

Instantly share code, notes, and snippets.

@maestrow
Created September 13, 2022 02:27
Show Gist options
  • Save maestrow/b87f9a28a849e7602c534eee8dadaedb to your computer and use it in GitHub Desktop.
Save maestrow/b87f9a28a849e7602c534eee8dadaedb to your computer and use it in GitHub Desktop.
Tempermonkey script for github: Adds clone command to Code clone menu
// ==UserScript==
// @name Custom Clone Command
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://github.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=github.com
// @grant none
// ==/UserScript==
(function() {
'use strict';
function injectIf() {
//console.log('Tempermonkey: Custom Clone Command');
if (/^https?:\/\/github.com\/[^/]+\/[^/]+$/.test (location.href)) {
tryInject();
}
}
var handle;
function clearHandler(){
clearInterval(handle);
handle = 0;
}
function tryInject() {
handle = setInterval(inject, 100);
}
function inject() {
const tabpanel = document.querySelector('.Layout-main get-repo div[role=tabpanel]:nth-child(3)')
//console.log(tabpanel);
if (!tabpanel) {
return;
}
clearHandler()
const ig = tabpanel.querySelector('.input-group')
const p = tabpanel.querySelector('p')
const ssh = ig.children[0].value;
const user = ssh.match(/:(.+)\//)[1]
const repo = ssh.match(/\/(.+).git$/)[1]
const commandText = `cd /srv/repos/gh && mkdir -p ${user} && cd ${user} && git clone ${ssh} && cd ${repo}`;
const ig2 = ig.cloneNode(true)
ig2.children[0].setAttribute('value', commandText)
ig2.querySelector('clipboard-copy').setAttribute('value', commandText)
tabpanel.insertBefore(ig2, p)
ig.style['marginBottom'] = '10px';
//console.log(commandText);
}
let lastUrl = location.href;
new MutationObserver(() => {
const url = location.href;
if (url !== lastUrl) {
lastUrl = url;
onUrlChange();
}
}).observe(document, {subtree: true, childList: true});
function onUrlChange() {
//console.log('onUrlChange', location.href);
clearHandler();
injectIf();
}
window.addEventListener('load', function() {
//console.log('load');
clearHandler();
injectIf();
}, false);
})();
@maestrow
Copy link
Author

tempermonkey-gc-code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment