Greasy Fork

添加GitHub、huggingface镜像下载链接

添加GitHub、huggingface镜像下载链接,可直接在Linux服务器粘贴使用

目前为 2024-08-13 提交的版本。查看 最新版本

// ==UserScript==
// @name         添加GitHub、huggingface镜像下载链接
// @namespace    http://tampermonkey.net/
// @version      0.9
// @description  添加GitHub、huggingface镜像下载链接,可直接在Linux服务器粘贴使用
// @author       HuanZhi
// @match        https://github.com/*
// @match        https://huggingface.co/*
// @match        https://hf-mirror.com/*
// @grant        unsafewindow
// @license      MIT
// @run-at       document-end
// ==/UserScript==
(function(){
    // 下载按钮
    var download_btn = document.createElement("button");
    download_btn.setAttribute('style', "position:absolute; z-index:1000; right:15px; top:55px; height:28px; background-color:#3E8CD0; border:none; color:white; font-size:16px; cursor:pointer; border-radius:1em;");
    download_btn.setAttribute('id', "download_btn");
    document.body.appendChild(download_btn);
    download_btn.onmouseover = function() {
        this.style.backgroundColor="#e9686b";
    };
    download_btn.onmouseout = function() {
        this.style.backgroundColor="#3E8CD0";
    };
    
    // 打开镜像网站按钮
    var web_btn = document.createElement("button");
    web_btn.setAttribute('style', "position:absolute; z-index:1000; left:15px; top:55px; height:28px; background-color:#3E8CD0; border:none; color:white; font-size:16px; cursor:pointer; border-radius:1em;");
    web_btn.setAttribute('id', "web_btn");
    document.body.appendChild(web_btn);
    web_btn.onmouseover = function() {
        this.style.backgroundColor="#e9686b";
    };
    web_btn.onmouseout = function() {
        this.style.backgroundColor="#3E8CD0";
    };
        
    //添加按钮显示
    var url = window.location.href;
    if (url.includes("github")) {
        download_btn.innerText = "下载地址";
        download_btn.onclick = GitMirrorLink;
    } else if (url.includes("huggingface")) {
        download_btn.innerText = "下载地址";
        download_btn.onclick = HFMirrorLink;
    } else if (url.includes("hf-mirror")) {
        download_btn.innerText = "下载地址";
        download_btn.onclick = HFMirrorLink;
    } else if (url.includes("huggingface")) {
        web_btn.innerText = "打开镜像";
        web_btn.onclick = OpenHFMirror;
    }
}
)();

function GitMirrorLink(){
    var home_url = `git clone https://mirror.ghproxy.com/${document.querySelector('meta[name="go-import"]').getAttribute('content').split(' ').slice(-1)}`;
    navigator.clipboard.writeText(home_url);
    download_btn.innerText = "已复制";
}

function HFMirrorLink(){
    var home_url = `huggingface-cli download --resume-download ${document.querySelector('a.break-words.font-mono.font-semibold.hover\\:text-blue-600').getAttribute('href').slice(1)} --local-dir ${document.querySelector('a.break-words.font-mono.font-semibold.hover\\:text-blue-600').getAttribute('href').split("/").slice(-1)}`;
    navigator.clipboard.writeText(home_url);
    download_btn.innerText = "已复制";
}

function OpenHFMirror(){
    var home_url = window.location.href.replace('https://huggingface.co', 'https://hf-mirror.com');
    window.open(home_url);
}