GitHub Remove Diff Signs

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

当前为 2016-12-29 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name GitHub Remove Diff Signs
  3. // @version 1.0.2
  4. // @description A userscript that removes the "+" and "-" from code diffs
  5. // @license https://creativecommons.org/licenses/by-sa/4.0/
  6. // @namespace http://github.com/Mottie
  7. // @include https://github.com/*
  8. // @grant none
  9. // @run-at document-idle
  10. // @author Rob Garrison
  11. // ==/UserScript==
  12. /* jshint unused:true, esnext:true */
  13. (function() {
  14. "use strict";
  15.  
  16. let targets,
  17. busy = false;
  18.  
  19. function processDiff() {
  20. busy = true;
  21. if (document.querySelector(".highlight")) {
  22. let indx = 0,
  23.  
  24. els = document.querySelectorAll(".blob-code-deletion .blob-code-inner, .blob-code-addition .blob-code-inner"),
  25. len = els.length,
  26.  
  27. // target "+" and "-" at start
  28. regex = /^[+-]/,
  29.  
  30. // loop with delay to allow user interaction
  31. loop = function() {
  32. let el, txt,
  33. // max number of DOM insertions per loop
  34. max = 0;
  35. while ( max < 20 && indx < len ) {
  36. if (indx >= len) {
  37. return;
  38. }
  39. el = els[indx];
  40. txt = el.childNodes[0].textContent;
  41. el.childNodes[0].textContent = txt.replace(regex, " ");
  42. max++;
  43. indx++;
  44. }
  45. if (indx < len) {
  46. setTimeout(function(){
  47. loop();
  48. }, 200);
  49. }
  50. };
  51. loop();
  52. }
  53. busy = false;
  54. }
  55.  
  56. function init() {
  57. if (document.querySelector("#files.diff-view")) {
  58. processDiff();
  59. }
  60. }
  61.  
  62. // DOM targets - to detect GitHub dynamic ajax page loading
  63. targets = document.querySelectorAll([
  64. "#js-repo-pjax-container",
  65. "#js-pjax-container"
  66. ].join(","));
  67.  
  68. // update TOC when content changes
  69. Array.prototype.forEach.call(targets, function(target) {
  70. new MutationObserver(function(mutations) {
  71. mutations.forEach(function(mutation) {
  72. // preform checks before adding code wrap to minimize function calls
  73. if (!busy && mutation.target === target) {
  74. init();
  75. }
  76. });
  77. }).observe(target, {
  78. childList: true,
  79. subtree: true
  80. });
  81. });
  82.  
  83. init();
  84.  
  85. })();