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.

Versão de: 09/12/2023. Veja: a última versão.

Você precisará instalar uma extensão como Tampermonkey, Greasemonkey ou Violentmonkey para instalar este script.

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

Você precisará instalar uma extensão como Tampermonkey ou Violentmonkey para instalar este script.

Você precisará instalar uma extensão como Tampermonkey ou Userscripts para instalar este script.

Você precisará instalar uma extensão como o Tampermonkey para instalar este script.

Você precisará instalar um gerenciador de scripts de usuário para instalar este script.

(Eu já tenho um gerenciador de scripts de usuário, me deixe instalá-lo!)

Você precisará instalar uma extensão como o Stylus para instalar este estilo.

Você precisará instalar uma extensão como o Stylus para instalar este estilo.

Você precisará instalar uma extensão como o Stylus para instalar este estilo.

Você precisará instalar um gerenciador de estilos de usuário para instalar este estilo.

Você precisará instalar um gerenciador de estilos de usuário para instalar este estilo.

Você precisará instalar um gerenciador de estilos de usuário para instalar este estilo.

(Eu já possuo um gerenciador de estilos de usuário, me deixar fazer a instalação!)

// ==UserScript==
// @name         Add repo/branch links to GitHub's "Comparing Changes" page
// @namespace    http://tampermonkey.net/
// @version      0.4
// @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/*
// @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==

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>`;

jQuery.noConflict(true)(function ($) {
  function modifyPage() {
    if (location.pathname.indexOf(`/compare/`) === -1) {
      return;
    }
    const isPageAlreadyModified = $(`#browse-link-0`).length > 0;
    if (isPageAlreadyModified) {
      return;
    }

    $(`.range-cross-repo-pair details`).wrap(
      `<div class="details-container"></div>`
    );

    $(`.details-container`).each(function (index) {
      const containerType =
        $(this).find(`summary.branch`).length > 0 ? `branch` : `repo`;
      const html = containerType === `repo` ? browseRepoHtml : browseBranchHtml;
      const href = hrefString(containerType, index);
      $(this)
        .css({ 'display': `inline-block` })
        .append(html)
        .css({ 'text-align': `center` })
        .find(`a`)
        .attr({ id: `browse-link-${index}`, href });
      if ($(this).find(`details`).is(`:hidden`)) {
        $(this).hide();
      }
    });

    $(`.range-editor .pre-mergability, .range-editor .d-inline-block`).css({
      'vertical-align': `top`,
    });
  }

  function hrefString(containerType, index) {
    switch (containerType) {
      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 pageReadyForModification =
    $(`.range-cross-repo-pair details`).length > 0;
  if (pageReadyForModification) {
    modifyPage();
  }

  setInterval(modifyPage, 1500);
});