GitHub Remove Diff Signs

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

当前为 2016-04-05 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name GitHub Remove Diff Signs
  3. // @version 1.0.1
  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 */
  13. (function() {
  14. "use strict";
  15.  
  16. var busy = false,
  17.  
  18. processDiff = function() {
  19. busy = true;
  20. if (document.querySelector(".highlight")) {
  21. var indx = 0,
  22.  
  23. els = document.querySelectorAll(".blob-code-deletion .blob-code-inner, .blob-code-addition .blob-code-inner"),
  24. len = els.length,
  25.  
  26. // target "+" and "-" at start
  27. regex = /^[+-]/,
  28.  
  29. // loop with delay to allow user interaction
  30. loop = function() {
  31. var el, txt,
  32. // max number of DOM insertions per loop
  33. max = 0;
  34. while ( max < 20 && indx < len ) {
  35. if (indx >= len) { return; }
  36. el = els[indx];
  37. txt = el.childNodes[0].textContent;
  38. el.childNodes[0].textContent = txt.replace(regex, " ");
  39. max++;
  40. indx++;
  41. }
  42. if (indx < len) {
  43. setTimeout(function(){
  44. loop();
  45. }, 200);
  46. }
  47. };
  48. loop();
  49. }
  50. busy = false;
  51. },
  52.  
  53. init = function() {
  54. if (document.querySelector("#files.diff-view")) {
  55. processDiff();
  56. }
  57. },
  58.  
  59. // DOM targets - to detect GitHub dynamic ajax page loading
  60. targets = document.querySelectorAll([
  61. "#js-repo-pjax-container",
  62. "#js-pjax-container"
  63. ].join(","));
  64.  
  65. // update TOC when content changes
  66. Array.prototype.forEach.call(targets, function(target) {
  67. new MutationObserver(function(mutations) {
  68. mutations.forEach(function(mutation) {
  69. // preform checks before adding code wrap to minimize function calls
  70. if (!busy && mutation.target === target) {
  71. init();
  72. }
  73. });
  74. }).observe(target, {
  75. childList: true,
  76. subtree: true
  77. });
  78. });
  79.  
  80. init();
  81.  
  82. })();