Hide github fork button

Hide github fork button for some reason.

  1. // ==UserScript==
  2. // @name Hide github fork button
  3. // @namespace https://github.com/mozillazg/hide-github-fork-button.user.js
  4. // @description Hide github fork button for some reason.
  5. // @version 0.3.0
  6. // @author mozillazg
  7. // @include https://github.com/test/*
  8. // @run-at document-end
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function () {
  13. "use strict";
  14.  
  15. function isContainFork() {
  16. return (document.querySelectorAll("#fork-destination-box").length !== 0);
  17. }
  18.  
  19. function getSubmitTypeButtons() {
  20. var navigations = document.querySelectorAll("#js-repo-pjax-container .file-navigation button[type=submit]");
  21. var headers = document.querySelectorAll("#js-repo-pjax-container .file-header button[type=submit]");
  22. return [navigations, headers]; // edit, delete buttons
  23. }
  24.  
  25. function getRepoName() {
  26. var href = document.querySelectorAll('#js-repo-pjax-container h1 strong[itemprop="name"] a')[0].attributes.href;
  27. var name = href.value;
  28. return name; // "/<account>/<repo>"
  29. }
  30.  
  31. function getUploadFilesButtons(repoName) {
  32. var urlSubContent = repoName + '/upload/';
  33. var selector = '#js-repo-pjax-container .file-navigation a[href^="' + urlSubContent + '"]';
  34. console.debug(selector);
  35. var buttons = document.querySelectorAll(selector);
  36. return buttons;
  37. }
  38.  
  39. function hideFork() {
  40. var buttons = document.querySelectorAll(".experiment-repo-nav .pagehead-actions li");
  41. if (buttons.length > 2) {
  42. var forkButton = buttons[2];
  43. forkButton.remove();
  44. }
  45. }
  46.  
  47. function hideSubmitTypeButtons() {
  48. var submitTypeButtons = getSubmitTypeButtons();
  49. submitTypeButtons.map(function(buttons) {
  50. buttons.forEach(function(button) {
  51. button.remove();
  52. });
  53. });
  54. }
  55.  
  56. function hideUploadFileButtons() {
  57. var repoName = getRepoName();
  58. var buttons = getUploadFilesButtons(repoName);
  59. buttons.forEach(function(button) {
  60. button.remove();
  61. });
  62. }
  63.  
  64. // run
  65. function run() {
  66. if (isContainFork()) {
  67. hideFork();
  68. hideSubmitTypeButtons();
  69. hideUploadFileButtons();
  70. }
  71. }
  72.  
  73. // DOM targets - to detect GitHub dynamic ajax page loading
  74. var targets = document.querySelectorAll([
  75. "#js-repo-pjax-container",
  76. "#js-pjax-container"
  77. ].join(","));
  78.  
  79. Array.prototype.forEach.call(targets, function(target) {
  80. // detect DOM change
  81. new MutationObserver(function(mutations) {
  82. mutations.forEach(function(mutation) {
  83. run();
  84. });
  85. }).observe(target, {
  86. childList: true,
  87. subtree: true
  88. });
  89. });
  90.  
  91. run();
  92. })();