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