fr4

Open a GitHub releases page from a repo

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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" });
    }
});