Add repo/branch links to GitHub's "Comparing Changes" page

This adds a link to the fork's branch on the GitHub "Comparing changes" page (AKA the "Create a Pull Request" page). See https://stackoverflow.com/questions/77623282/how-do-i-get-my-remote-branch-url-from-the-github-create-pull-request-page for more details.

Per 08-12-2023. Zie de nieuwste versie.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey, Greasemonkey of Violentmonkey.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey of Violentmonkey.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey of Violentmonkey.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey of Userscripts.

Voor het installeren van scripts heb je een extensie nodig, zoals {tampermonkey_link:Tampermonkey}.

Voor het installeren van scripts heb je een gebruikersscriptbeheerder nodig.

(Ik heb al een user script manager, laat me het downloaden!)

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

(Ik heb al een beheerder - laat me doorgaan met de installatie!)

// ==UserScript==
// @name         Add repo/branch links to GitHub's "Comparing Changes" page
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  This adds a link to the fork's branch on the GitHub "Comparing changes" page (AKA the "Create a Pull Request" page). See https://stackoverflow.com/questions/77623282/how-do-i-get-my-remote-branch-url-from-the-github-create-pull-request-page for more details.
// @author       DanKaplanSES
// @match        https://github.com/*/*/compare/*...*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=github.com
// @require      https://code.jquery.com/jquery-3.7.1.min.js
// @grant        none
// @license MIT
// ==/UserScript==

jQuery.noConflict(true)(function($) {
    function modifyPage() {
        const isPageAlreadyModified = $(`#browse-link-0`).length > 0;
        if (isPageAlreadyModified) {
            return;
        }
        const browseRepoHtml = `<div class="browse-repo"><a href="#" target="_blank">Browse Repo</a></div>`;
        const browseBranchHtml = `<div class="browse-branch"><a href="#" target="_blank">Browse Branch</a></div>`;

        $(`.range-cross-repo-pair details`).wrap(`<div class="details-container"></div>`);
        $(`.details-container`).each(function (index) {
            const isIndexEven = index % 2 === 0;
            const html = isIndexEven ? browseRepoHtml : browseBranchHtml;
            const href = buildHref(isIndexEven ? "repo" : "branch", index);
            $(this).css({"display": `inline-block`})
                .append(html).css({"text-align": `center`})
                    .find(`a`).attr({id: `browse-link-${index}`, href});
        });
        $(`.range-editor .pre-mergability, .range-editor .d-inline-block`).css({"vertical-align": `top`});
    }

    function buildHref(hrefType, index) {
        switch (hrefType) {
            case "repo":
                return `/` + $(`.css-truncate-target`)[index].textContent;
            case "branch":
                return `/` + $(`.css-truncate-target`)[index - 1].textContent + `/tree/` + $(`.css-truncate-target`)[index].textContent;
            default:
                throw new Error(`Unexpected hrefType: ${hrefType}`);
        }
    }

    const isPageLoaded = $(`.range-cross-repo-pair details`).length > 0;
    if (isPageLoaded) {
        modifyPage();
    }

    setInterval(modifyPage, 1500);
});