fr4

Open a GitHub releases page from a repo

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        fr4
// @namespace   Violentmonkey Scripts
// @match       *://github.com/*
// @grant       GM_registerMenuCommand
// @grant       GM_notification
// @grant       GM_xmlhttpRequest
// @grant       GM_openInTab
// @grant       unsafeWindow
// @version     1.0
// @license MIT
// @author      -
// @description Open a GitHub releases page from a repo
// ==/UserScript==

const get_repo = /(https?:\/\/[^.]+\.[^.\/]+\/[^\/]+\/[^\/]+)/i;
const get_repo_handle = /https?:\/\/[^.]+\.[^.\/]+\/([^\/]+\/[^\/]+)/i;

function give_up() {
    GM_notification({ title: "Error", text: "I can't find anything. Maybe try compiling it yourself?" });
}

function try_packages() {
    give_up();
}

GM_registerMenuCommand('Find latest release files', () => {
    // Get the root repository url, if possible
    let match_result = get_repo.exec(window.location.href);
    if (match_result != null) {
        let repo_url = match_result[0];
        if (!repo_url.endsWith("/")) {
            repo_url += "/";
        }
        // Try latest release
        GM_xmlhttpRequest({
            url: repo_url + "releases/latest",
            onload: () => {
                GM_openInTab(repo_url + "releases/latest");
            },
            onerror: () => {
                // Try tags
                let tag_api_url = "https://api.github.com/repos/" + get_repo_handle.exec(repo_url)[0] + "/tags";

                GM_xmlhttpRequest({
                    url: tag_api_url,
                    onload: (r) => {
                        let doc = JSON.parse(r.response);
                        if (Array.isArray(doc)) {
                            let tag_url = repo_url + "releases/tag/" + doc[0].name;
                            GM_xmlhttpRequest({
                                url: tag_url,
                                onload: () => {
                                    GM_openInTab(tag_url);
                                },
                                onerror: () => {
                                    try_packages();
                                }
                            });
                        } else {
                            try_packages();
                        }
                    },
                    onerror: () => {
                        try_packages();
                    }
                });
            }
        });
    } else {
        GM_notification({ title: "Error", text: "You must be on the web page of a GitHub repository" });
    }
});