GitHub Octotree single file view button

Adds a button to GitHub pull requests that activates a single file view for Octotree.

目前为 2019-01-12 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name GitHub Octotree single file view button
  3. // @namespace https://puac.de/
  4. // @version 0.1.0
  5. // @description Adds a button to GitHub pull requests that activates a single file view for Octotree.
  6. // @author Hans Puac
  7. // @match https://github.com/*/pull/*/files
  8. // @run-at document-end
  9. // ==/UserScript==
  10.  
  11. (function () {
  12. 'use strict';
  13.  
  14. var activateSingleFileView = function () {
  15. document.querySelector('.jstree-container-ul').addEventListener('click', function (e) {
  16. if (e.target.matches('.jstree-anchor')) {
  17. var id = e.target.href.split('#')[1];
  18. document.querySelectorAll('.js-file').forEach(function (item) {
  19. if (item.id === id) {
  20. item.classList.add('open', 'Details--on');
  21. } else {
  22. item.classList.remove('open', 'Details--on');
  23. }
  24. });
  25. }
  26. });
  27. };
  28.  
  29. var activateButton = document.createElement('button');
  30. activateButton.innerText = 'Octotree single file view';
  31. activateButton.classList.add('btn', 'btn-sm');
  32. activateButton.style.float = 'left';
  33. activateButton.style.marginRight = '20px';
  34. activateButton.addEventListener(
  35. 'click',
  36. function () {
  37. activateButton.remove();
  38. activateSingleFileView();
  39. },
  40. false
  41. );
  42.  
  43. var buttonWrapper = document.querySelector('.pr-review-tools');
  44. if (buttonWrapper === null) {
  45. buttonWrapper = document.querySelector('.js-details-container .BtnGroup');
  46. }
  47. buttonWrapper.prepend(activateButton);
  48. })();