Greasy Fork is available in English.

GitHub: inline patch view

Clicking on a commit ID shows the patch inline. Second click opens the link.

2016-02-12 يوللانغان نەشرى. ئەڭ يېڭى نەشرىنى كۆرۈش.

  1. // ==UserScript==
  2. // @name GitHub: inline patch view
  3. // @namespace https://akinori.org/
  4. // @description Clicking on a commit ID shows the patch inline. Second click opens the link.
  5. // @license 2-clause BSDL
  6. // @author Akinori MUSHA
  7. // @include https://github.com/*/*/commits
  8. // @include https://github.com/*/*/commits/*
  9. // @include https://github.com/*/*/pull/*
  10. // @version 1.0.0
  11. // @homepage https://github.com/knu/userjs-github_inline_patch_view
  12. // @homepage https://greatest.deepsurf.us/scripts/17016-github-inline-patch-view
  13. // @grant none
  14. // ==/UserScript==
  15. "use strict";
  16. (function () {
  17. let cloneNode = function (node) {
  18. let clone = document.createElement(node.tagName);
  19. Array.prototype.forEach.
  20. call(node.attributes,
  21. function (attr) {
  22. clone.setAttribute(attr.name, attr.value);
  23. });
  24. return clone;
  25. };
  26. let insertInlinePatch = function (commit, sha) {
  27. if (commit.classList.contains("inline-patch")) {
  28. return false;
  29. }
  30. let url = sha.href;
  31. let xhr = new XMLHttpRequest();
  32. xhr.onload = function () {
  33. let patch = this.responseXML.getElementById("files");
  34. switch (commit.tagName) {
  35. case "TR":
  36. // Split the table to avoid CSS rule conflicts
  37. let container = null;
  38. for (let node = commit.parentNode;
  39. node !== null;
  40. node = node.parentNode) {
  41. let clone = cloneNode(node);
  42. if (container === null) {
  43. // Move following siblings to a new container
  44. let siblings = [];
  45. for (let sibling = commit.nextSibling;
  46. sibling !== null;
  47. sibling = sibling.nextSibling) {
  48. siblings.push(sibling);
  49. }
  50. siblings.forEach(function (sibling) {
  51. clone.appendChild(sibling);
  52. });
  53. } else {
  54. clone.appendChild(container);
  55. }
  56. container = clone;
  57. if (node.tagName === "TABLE") {
  58. node.parentNode.insertBefore(container, node.nextSibling);
  59. patch.style.marginLeft = commit.querySelector(".commit-message").offsetLeft.toString() + "px";
  60. node.parentNode.insertBefore(patch, container);
  61. break;
  62. }
  63. }
  64. break;
  65. default:
  66. let meta = commit.querySelector(".commit-meta");
  67. if (meta) {
  68. meta.parentNode.appendChild(patch);
  69. }
  70. }
  71. commit.classList.add("inline-patch");
  72. };
  73. xhr.onerror = function () {
  74. console.log("Failed to load the patch from " + url);
  75. };
  76. xhr.open("GET", url);
  77. xhr.responseType = "document";
  78. xhr.send();
  79. return true;
  80. };
  81. let activateInlinePatch = function (commit) {
  82. let sha = commit.querySelector("a.sha[href*='/commit/'], a.commit-id[href*='/commit/']");
  83. if (!sha) {
  84. return;
  85. }
  86. sha.addEventListener("click",
  87. function (e) {
  88. if (insertInlinePatch(commit, sha)) {
  89. e.preventDefault();
  90. }
  91. },
  92. false);
  93. let expander = commit.querySelector(".hidden-text-expander a[href]");
  94. if (expander) {
  95. expander.addEventListener("click",
  96. function (e) {
  97. insertInlinePatch(commit, sha);
  98. },
  99. false);
  100. }
  101. };
  102. Array.prototype.forEach.
  103. call(document.querySelectorAll("li.commit, tr.commit"),
  104. activateInlinePatch);
  105. })();