GitHub Remove Diff Signs

A userscript that removes the "+" and "-" from code diffs

当前为 2017-03-25 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name GitHub Remove Diff Signs
  3. // @version 1.1.1
  4. // @description A userscript that removes the "+" and "-" from code diffs
  5. // @license https://creativecommons.org/licenses/by-sa/4.0/
  6. // @namespace https://github.com/Mottie
  7. // @include https://github.com/*
  8. // @grant none
  9. // @run-at document-idle
  10. // @author Rob Garrison
  11. // ==/UserScript==
  12. (() => {
  13. "use strict";
  14.  
  15. let debounce,
  16. busy = false,
  17. observers = [];
  18.  
  19. function processDiff() {
  20. busy = true;
  21. if (document.querySelector(".highlight")) {
  22. let indx = 0,
  23.  
  24. els = document.querySelectorAll(`
  25. .blob-code-deletion .blob-code-inner,
  26. .blob-code-addition .blob-code-inner`
  27. ),
  28. len = els.length,
  29.  
  30. // target "+" and "-" at start
  31. regex = /^[+-]/,
  32.  
  33. // loop with delay to allow user interaction
  34. loop = () => {
  35. let el, txt,
  36. // max number of DOM insertions per loop
  37. max = 0;
  38. while ( max < 50 && indx < len ) {
  39. if (indx >= len) {
  40. return;
  41. }
  42. el = els[indx];
  43. txt = el.childNodes[0].textContent;
  44. el.childNodes[0].textContent = txt.replace(regex, " ");
  45. max++;
  46. indx++;
  47. }
  48. if (indx < len) {
  49. setTimeout(() => {
  50. loop();
  51. }, 200);
  52. }
  53. };
  54. loop();
  55. }
  56. busy = false;
  57. }
  58.  
  59. // GitHub pjax
  60. document.addEventListener("pjax:end", init);
  61.  
  62. function removeObservers() {
  63. observers.forEach(observer => {
  64. if (observer) {
  65. observer.disconnect();
  66. }
  67. });
  68. observers = [];
  69. }
  70.  
  71. function addObservers() {
  72. // DOM targets - to detect GitHub dynamic ajax page loading
  73. Array.from(
  74. // Observe progressively loaded content
  75. document.querySelectorAll(`
  76. .js-diff-progressive-container,
  77. .js-diff-load-container`
  78. )
  79. ).forEach(target => {
  80. const obsrvr = new MutationObserver(mutations => {
  81. mutations.forEach(mutation => {
  82. // preform checks before adding code wrap to minimize function calls
  83. const tar = mutation.target;
  84. if (!busy && tar && (
  85. tar.classList.contains("js-diff-progressive-container") ||
  86. tar.classList.contains("js-diff-load-container") ||
  87. tar.classList.contains("blob-wrapper")
  88. )
  89. ) {
  90. clearTimeout(debounce);
  91. debounce = setTimeout(() => {
  92. processDiff();
  93. }, 500);
  94. }
  95. });
  96. });
  97. obsrvr.observe(target, {
  98. childList: true,
  99. subtree: true
  100. });
  101. observers.push(obsrvr);
  102. });
  103. }
  104.  
  105. function init() {
  106. removeObservers();
  107. if (document.querySelector("#files.diff-view")) {
  108. addObservers();
  109. processDiff();
  110. }
  111. }
  112.  
  113. init();
  114.  
  115. })();