Find the initial Github commit

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

当前为 2025-02-20 提交的版本,查看 最新版本

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

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

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

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

您需要先安装一款用户脚本管理器扩展,例如 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);
    }
})();