GitHub: inline patch view

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

Pada tanggal 12 Februari 2016. Lihat %(latest_version_link).

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