Github Pull Requests - Always Hide Whitespace

Always add the "hide whitespace" when viewing Github PR diffs

  1. // ==UserScript==
  2. // @name Github Pull Requests - Always Hide Whitespace
  3. // @namespace github-hide-whitespace
  4. // @match *://*.github.com/*/pull/*/files*
  5. // @run-at document-start
  6. // @grant none
  7. // @version 1.0.0
  8. // @description Always add the "hide whitespace" when viewing Github PR diffs
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. /*
  13. This script will always add the "hide whitespace" url param when viewing Github
  14. pull request diffs, with ?w=1 or &w=1.
  15.  
  16. You can override to show whitespace diffs by manually changing to w=0 in your
  17. query instead of w=1, and script will leave it alone
  18. */
  19.  
  20. var oldUrlSearch = window.location.search;
  21.  
  22. // Test if "&w=" or "?w=" is in the search params
  23. if ( !/[?&]w=/.test(oldUrlSearch) ) {
  24. // if there were already other search params, just add on with '&'
  25. var ampersandOrQuestionMark = !!oldUrlSearch ? '&' : '?';
  26.  
  27. var newURL = window.location.protocol + "//"
  28. + window.location.host
  29. + window.location.pathname
  30. + oldUrlSearch + ampersandOrQuestionMark + "w=1"
  31. + window.location.hash;
  32.  
  33. window.location.replace(newURL);
  34. }