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.

As of 08. 12. 2023. See the latest version.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

  1. // ==UserScript==
  2. // @name Add repo/branch links to GitHub's "Comparing Changes" page
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.2
  5. // @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.
  6. // @author DanKaplanSES
  7. // @match https://github.com/*/*/compare/*...*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=github.com
  9. // @require https://code.jquery.com/jquery-3.7.1.min.js
  10. // @grant none
  11. // @license MIT
  12. // ==/UserScript==
  13.  
  14. jQuery.noConflict(true)(function($) {
  15. function modifyPage() {
  16. const isPageAlreadyModified = $(`#browse-link-0`).length > 0;
  17. if (isPageAlreadyModified) {
  18. return;
  19. }
  20. const browseRepoHtml = `<div class="browse-repo"><a href="#" target="_blank">Browse Repo</a></div>`;
  21. const browseBranchHtml = `<div class="browse-branch"><a href="#" target="_blank">Browse Branch</a></div>`;
  22.  
  23. $(`.range-cross-repo-pair details`).wrap(`<div class="details-container"></div>`);
  24. $(`.details-container`).each(function (index) {
  25. const isIndexEven = index % 2 === 0;
  26. const html = isIndexEven ? browseRepoHtml : browseBranchHtml;
  27. const href = buildHref(isIndexEven ? "repo" : "branch", index);
  28. $(this).css({"display": `inline-block`})
  29. .append(html).css({"text-align": `center`})
  30. .find(`a`).attr({id: `browse-link-${index}`, href});
  31. });
  32. $(`.range-editor .pre-mergability, .range-editor .d-inline-block`).css({"vertical-align": `top`});
  33. }
  34.  
  35. function buildHref(hrefType, index) {
  36. switch (hrefType) {
  37. case "repo":
  38. return `/` + $(`.css-truncate-target`)[index].textContent;
  39. case "branch":
  40. return `/` + $(`.css-truncate-target`)[index - 1].textContent + `/tree/` + $(`.css-truncate-target`)[index].textContent;
  41. default:
  42. throw new Error(`Unexpected hrefType: ${hrefType}`);
  43. }
  44. }
  45.  
  46. const isPageLoaded = $(`.range-cross-repo-pair details`).length > 0;
  47. if (isPageLoaded) {
  48. modifyPage();
  49. }
  50.  
  51. setInterval(modifyPage, 1500);
  52. });