Find the initial Github commit

This will help users get a button to click to end of the github commits page.

目前為 2025-02-20 提交的版本,檢視 最新版本

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Find the initial Github commit
// @namespace    http://tampermonkey.net/
// @version      0.2.1
// @description  This will help users get a button to click to end of the github commits page.
// @author       Mutu,aspen138
// @match        https://github.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=github.com
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';
    let reg = new RegExp("\/commits\/")
    var isInsert = false
    var timer = setInterval(() => {
        if (isInsert) {
            if (reg.test(window.location.pathname)) {
            } else {
                getCommits()
                isInsert = false
            }
        } else {
            if (reg.test(window.location.pathname)) {
                insertBtn()
                isInsert = true
            } else {
                getCommits()
            }
        }
    }, 1000);

    function getCommits() {
        // Attempt to find the span that displays the commit count
        let commitSpan = document.querySelector("span.fgColor-default.custom-highlight");
        if (commitSpan) {
            // Example innerText might be "90 Commits", so remove "Commits" and trim extra space
            let commitText = commitSpan.innerText.replace("Commits", "").trim();
            //let commitNumber = parseInt(commitText, 10);

            // Store just the numeric portion in sessionStorage
            sessionStorage.setItem("commits", commitText);
        }
    }

    function insertBtn() {
        // Get the total commit count from sessionStorage
        let commitsStr = sessionStorage.getItem("commits");
        let commitsNum = parseInt(commitsStr.replace(/,/, ""), 10);
        console.log("commitsNum=",commitsNum);
        // Select the container that holds the pagination buttons
        let btnGroup = document.querySelector(".Box-sc-g0xbh4-0.prc-ButtonGroup-ButtonGroup-vcMeG");
        if (!btnGroup) return; // Guard clause if the container isn't found

        // Select the "Next" pagination link
        let btnToNext = document.querySelector("[data-testid='pagination-next-button']");
        if (!btnToNext) return; // Guard clause if there's no "Next" button

        // Create the wrapper div
        let newDiv = document.createElement("div");

        // Create the new "Click To End" anchor
        let btnToEnd = document.createElement("a");
        btnToEnd.type = "button";
        btnToEnd.tabIndex = 0;
        btnToEnd.setAttribute("data-testid", "pagination-last-button");
        btnToEnd.className = "prc-Button-ButtonBase-c50BI fgColor-accent text-normal";
        btnToEnd.setAttribute("data-loading", "false");
        btnToEnd.setAttribute("data-size", "medium");
        btnToEnd.setAttribute("data-variant", "invisible");
        btnToEnd.href = btnToNext.href.replace(/\+\d+/g, `+${commitsNum-4}`);

        // Construct the inner span structure to match the new button style
        let spanContent = document.createElement("span");
        spanContent.setAttribute("data-component", "buttonContent");
        spanContent.className = "prc-Button-ButtonContent-HKbr-";

        let spanText = document.createElement("span");
        spanText.setAttribute("data-component", "text");
        spanText.className = "prc-Button-Label-pTQ3x";
        spanText.innerText = "Click To End";

        // Assemble all parts
        spanContent.appendChild(spanText);
        btnToEnd.appendChild(spanContent);
        newDiv.appendChild(btnToEnd);
        console.log("newDiv=",newDiv);
        btnGroup.appendChild(newDiv);
        console.log("btnGroup=", btnGroup);
    }
})();