Created
March 29, 2024 12:36
-
-
Save a1678991/5f2a4925b5fe8bdc132cfc66e14e5149 to your computer and use it in GitHub Desktop.
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 Hugging Face Clone Button | |
// @namespace http://tampermonkey.net/ | |
// @version 1.0 | |
// @description Adds a button to Hugging Face repository pages to easily copy the git clone command to the clipboard. | |
// @author a1678991 | |
// @match https://huggingface.co/* | |
// @grant GM_setClipboard | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
// This function extracts the author and repository name from the current URL | |
function extractRepoDetails() { | |
const path = window.location.pathname.split('/').filter(part => part.length > 0); | |
if (path.length >= 2) { | |
console.log({ author: path[0], repoName: path[1] }); | |
return { author: path[0], repoName: path[1] }; | |
} | |
return null; | |
} | |
// This function generates the git clone command based on the author and repository name | |
function generateCloneCommand(author, repoName) { | |
return `git clone git@hf.co:${author}/${repoName} ${author}/${repoName}`; | |
} | |
// This function creates and styles the clone button | |
function createCloneButton() { | |
const headerElement = document.querySelector("h1"); | |
if (!headerElement) return; // Exit if the h1 element isn't found | |
const cloneButton = document.createElement("button"); | |
cloneButton.textContent = "Copy Clone Command"; | |
cloneButton.style = "margin-left: 10px; border: solid 1px rgb(20, 28, 46); border-radius: 6px; border-color: rgb(20 28 46 / var(--tw-border-opacity)); font-size: 14px"; | |
cloneButton.title = "Copy git clone command to clipboard"; | |
headerElement.appendChild(cloneButton); | |
return cloneButton; | |
} | |
// This function sets up the clone button on the page | |
function setupCloneButton() { | |
const cloneButton = createCloneButton(); | |
if (!cloneButton) return; // Exit if button wasn't created | |
cloneButton.addEventListener('click', function() { | |
const details = extractRepoDetails(); | |
if (details) { | |
const cloneCommand = generateCloneCommand(details.author, details.repoName); | |
GM_setClipboard(cloneCommand); | |
} | |
}); | |
} | |
// Ensure the script runs after the page content is fully loaded | |
window.addEventListener('load', setupCloneButton); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment