Find the initial Github commit

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

Versione datata 20/02/2025. Vedi la nuova versione l'ultima versione.

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==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);
    }
})();