GitHub Custom Navigation

A userscript that allows you to customize GitHub's main navigation bar

当前为 2019-04-06 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name GitHub Custom Navigation
  3. // @version 1.1.7
  4. // @description A userscript that allows you to customize GitHub's main navigation bar
  5. // @license MIT
  6. // @author Rob Garrison
  7. // @namespace https://github.com/Mottie
  8. // @include https://github.com/*
  9. // @include https://gist.github.com/*
  10. // @run-at document-end
  11. // @grant GM_addStyle
  12. // @grant GM_getValue
  13. // @grant GM_setValue
  14. // @icon https://github.githubassets.com/pinned-octocat.svg
  15. // @require https://cdnjs.cloudflare.com/ajax/libs/dragula/3.7.2/dragula.js
  16. // ==/UserScript==
  17. (() => {
  18. "use strict";
  19.  
  20. // open menu via hash
  21. const panelHash = "#github-custom-nav-settings",
  22.  
  23. // get user name; or empty string if not logged in
  24. user = $("meta[name='user-login']") &&
  25. $("meta[name='user-login']").getAttribute("content") || "",
  26.  
  27. defaults = {
  28. github: [
  29. "pr", "issues", "gist", "separator", "stars", "watching", "separator",
  30. "profile", "blog", "marketplace", "explore", "menu"
  31. ],
  32. gists: [
  33. "gistall", "giststars", "github", "separator", "pr", "issues", "stars",
  34. "watching", "separator", "profile", "blog", "marketplace", "explore", "menu"
  35. ],
  36.  
  37. currentLink: "pr",
  38. // using full length url so the links work from any subdomain (e.g. gist pages)
  39. items: {
  40. "advsearch": {
  41. url: "https://github.com/search/advanced",
  42. tooltip: "Advanced Search",
  43. hotkey: "",
  44. content: "<svg class='octicon' xmlns='http://www.w3.org/2000/svg' aria-hidden='true' height='16' width='16' viewBox='0 0 16 16'><path d='M15.7 14.3L11.89 10.47c0.7-0.98 1.11-2.17 1.11-3.47 0-3.31-2.69-6-6-6S1 3.69 1 7s2.69 6 6 6c1.3 0 2.48-0.41 3.47-1.11l3.83 3.81c0.19 0.2 0.45 0.3 0.7 0.3s0.52-0.09 0.7-0.3c0.39-0.39 0.39-1.02 0-1.41zM7 11.7c-2.59 0-4.7-2.11-4.7-4.7s2.11-4.7 4.7-4.7 4.7 2.11 4.7 4.7-2.11 4.7-4.7 4.7z'/></svg>"
  45. },
  46. "blog": {
  47. url: "https://github.blog",
  48. tooltip: "Blog",
  49. hotkey: "",
  50. content: "<svg class='octicon' xmlns='http://www.w3.org/2000/svg' aria-hidden='true' height='16' viewBox='0 0 16 16' width='16'><path d='M9 9H8c.55 0 1-.45 1-1V7c0-.55-.45-1-1-1H7c-.55 0-1 .45-1 1v1c0 .55.45 1 1 1H6c-.55 0-1 .45-1 1v2h1v3c0 .55.45 1 1 1h1c.55 0 1-.45 1-1v-3h1v-2c0-.55-.45-1-1-1zM7 7h1v1H7V7zm2 4H8v4H7v-4H6v-1h3v1zm2.09-3.5c0-1.98-1.61-3.59-3.59-3.59A3.593 3.593 0 0 0 4 8.31v1.98c-.61-.77-1-1.73-1-2.8 0-2.48 2.02-4.5 4.5-4.5S12 5.01 12 7.49c0 1.06-.39 2.03-1 2.8V8.31c.06-.27.09-.53.09-.81zm3.91 0c0 2.88-1.63 5.38-4 6.63v-1.05a6.553 6.553 0 0 0 3.09-5.58A6.59 6.59 0 0 0 7.5.91 6.59 6.59 0 0 0 .91 7.5c0 2.36 1.23 4.42 3.09 5.58v1.05A7.497 7.497 0 0 1 7.5 0C11.64 0 15 3.36 15 7.5z'/></svg>"
  51. },
  52. "explore": {
  53. url: "https://github.com/explore",
  54. tooltip: "Explore",
  55. hotkey: "",
  56. content: "<svg class='octicon' xmlns='http://www.w3.org/2000/svg' aria-hidden='true' height='16' viewBox='0 0 16 16' width='16'><path d='M8 0a8 8 0 1 0 0 16A8 8 0 0 0 8 0zm.6 14.5v-1H7.2v1a6.5 6.5 0 0 1-5.7-6h1V7.4h-1a6.5 6.5 0 0 1 5.7-5.8v1h1.4v-1a6.6 6.6 0 0 1 6 5.8h-1v1.3h1a6.5 6.5 0 0 1-6 6zm2.7-10.8l-4.5 3-2.2 4.8 4.7-3z'/></svg>"
  57. },
  58. "gist": {
  59. url: "https://gist.github.com/",
  60. tooltip: "Gist",
  61. hotkey: "",
  62. content: "<svg class='octicon' xmlns='http://www.w3.org/2000/svg' aria-hidden='true' height='16' viewBox='0 0 12 16' width='12'><path d='M7.5 5L10 7.5 7.5 10l-.75-.75L8.5 7.5 6.75 5.75 7.5 5zm-3 0L2 7.5 4.5 10l.75-.75L3.5 7.5l1.75-1.75L4.5 5zM0 13V2c0-.55.45-1 1-1h10c.55 0 1 .45 1 1v11c0 .55-.45 1-1 1H1c-.55 0-1-.45-1-1zm1 0h10V2H1v11z'></path></svg>"
  63. },
  64. "gistall": {
  65. url: "https://gist.github.com/discover",
  66. tooltip: "Discover Gists",
  67. hotkey: "",
  68. content: "<svg class='octicon' xmlns='http://www.w3.org/2000/svg' aria-hidden='true' height='16' viewBox='0 0 12 16' width='12'><path d='M7.5 5L10 7.5 7.5 10l-.75-.75L8.5 7.5 6.75 5.75 7.5 5zm-3 0L2 7.5 4.5 10l.75-.75L3.5 7.5l1.75-1.75L4.5 5zM0 13V2c0-.55.45-1 1-1h10c.55 0 1 .45 1 1v11c0 .55-.45 1-1 1H1c-.55 0-1-.45-1-1zm1 0h10V2H1v11z'></path></svg>"
  69. },
  70. "giststars": {
  71. url: "https://gist.github.com/${me}/starred",
  72. tooltip: "Starred Gists",
  73. hotkey: "",
  74. content: "<svg class='octicon' xmlns='http://www.w3.org/2000/svg' aria-hidden='true' height='16' viewBox='0 0 14 16' width='14'><path d='M14 6l-4.9-.64L7 1 4.9 5.36 0 6l3.6 3.26L2.67 14 7 11.67 11.33 14l-.93-4.74z'></path></svg>"
  75. },
  76. "github": {
  77. url: "https://github.com",
  78. tooltip: "GitHub",
  79. hotkey: "",
  80. content: "<svg class='octicon' xmlns='http://www.w3.org/2000/svg' aria-hidden='true' height='16' viewBox='0 0 16 16' width='16'><path d='M14.7 5.34c.13-.32.55-1.59-.13-3.31 0 0-1.05-.33-3.44 1.3-1-.28-2.07-.32-3.13-.32s-2.13.04-3.13.32c-2.39-1.64-3.44-1.3-3.44-1.3-.68 1.72-.26 2.99-.13 3.31C.49 6.21 0 7.33 0 8.69 0 13.84 3.33 15 7.98 15S16 13.84 16 8.69c0-1.36-.49-2.48-1.3-3.35zM8 14.02c-3.3 0-5.98-.15-5.98-3.35 0-.76.38-1.48 1.02-2.07 1.07-.98 2.9-.46 4.96-.46 2.07 0 3.88-.52 4.96.46.65.59 1.02 1.3 1.02 2.07 0 3.19-2.68 3.35-5.98 3.35zM5.49 9.01c-.66 0-1.2.8-1.2 1.78s.54 1.79 1.2 1.79c.66 0 1.2-.8 1.2-1.79s-.54-1.78-1.2-1.78zm5.02 0c-.66 0-1.2.79-1.2 1.78s.54 1.79 1.2 1.79c.66 0 1.2-.8 1.2-1.79s-.53-1.78-1.2-1.78z'/></svg>"
  81. },
  82. "issues": {
  83. url: "https://github.com/issues",
  84. tooltip: "Issues",
  85. hotkey: "g i",
  86. content: "<svg class='octicon' xmlns='http://www.w3.org/2000/svg' aria-hidden='true' height='16' viewBox='0 0 14 16' width='14'><path d='M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z'></path></svg>"
  87. },
  88. "marketplace": {
  89. url: "https://github.com/marketplace",
  90. tooltip: "Marketplace",
  91. hotkey: "",
  92. content: "<svg class='octicon' xmlns='http://www.w3.org/2000/svg' aria-hidden='true' height='16' viewBox='0 0 14 16' width='16'><path d='M0 0v16h14v-2h-1v1H1V1h12v.5h1V0H0zm3 3v1h8.8l2.5 2.5-.7.6.8.8 1.3-1.4L12.2 3H3zm1.4 2.1l-.8.8 2 2 .8-.8-2-2zm4.5 0L8 6l2 2 1-1-2.1-1.9z'/><path d='M6.5 15h-1v-4H4v4H3v-5h3.5zM12 13H8v-3h4v3zm-3-1h2v-1H9v1z'/></svg>"
  93. },
  94. "menu": {
  95. url: panelHash,
  96. tooltip: "Open Custom Navigation Settings",
  97. hotkey: "",
  98. content: "<svg class='octicon' xmlns='http://www.w3.org/2000/svg' aria-hidden='true' height='16' viewBox='0 0 14 16' width='14'><path d='M8.79 15H6.553l-.7-1.91-.608-.247-1.835.905-1.585-1.556.892-1.83-.25-.595L.5 9.127V6.933l1.944-.676.25-.597-.922-1.802L3.358 2.3l1.865.876.624-.248.638-1.93H8.73l.697 1.91.61.246 1.838-.905 1.58 1.555-1.114 2.317-2.714.65-.203-.24c-.444-.524-1.098-.824-1.794-.824C6.34 5.708 5.294 6.736 5.294 8c0 1.264 1.047 2.292 2.334 2.292.6 0 1.17-.224 1.604-.63l.18-.165 2.93.4 1.156 2.24-1.58 1.564-1.868-.88-.625.25L8.79 15zm-1.52-1h.78l.556-1.68 1.48-.592 1.62.765.553-.547-.583-1.13-1.93-.264c-.597.48-1.34.74-2.118.74-1.85 0-3.354-1.477-3.354-3.292 0-1.815 1.503-3.292 3.353-3.292.89 0 1.73.342 2.356.95l1.643-.394.6-1.25-.555-.546-1.598.786-1.455-.592L8.014 2h-.79L6.67 3.68l-1.48.59-1.622-.762-.556.546.802 1.566-.603 1.432-1.692.59v.763l1.71.558.603 1.43-.775 1.593.556.546 1.596-.788 1.456.593L7.27 14z'/></svg>"
  99. },
  100. "pr": {
  101. url: "https://github.com/pulls",
  102. tooltip: "Pull Requests",
  103. hotkey: "g p",
  104. content: "<svg class='octicon' xmlns='http://www.w3.org/2000/svg' aria-hidden='true' height='16' viewBox='0 0 12 16' width='12'><path d='M11 11.28V5c-.03-.78-.34-1.47-.94-2.06C9.46 2.35 8.78 2.03 8 2H7V0L4 3l3 3V4h1c.27.02.48.11.69.31.21.2.3.42.31.69v6.28A1.993 1.993 0 0 0 10 15a1.993 1.993 0 0 0 1-3.72zm-1 2.92c-.66 0-1.2-.55-1.2-1.2 0-.65.55-1.2 1.2-1.2.65 0 1.2.55 1.2 1.2 0 .65-.55 1.2-1.2 1.2zM4 3c0-1.11-.89-2-2-2a1.993 1.993 0 0 0-1 3.72v6.56A1.993 1.993 0 0 0 2 15a1.993 1.993 0 0 0 1-3.72V4.72c.59-.34 1-.98 1-1.72zm-.8 10c0 .66-.55 1.2-1.2 1.2-.65 0-1.2-.55-1.2-1.2 0-.65.55-1.2 1.2-1.2.65 0 1.2.55 1.2 1.2zM2 4.2C1.34 4.2.8 3.65.8 3c0-.65.55-1.2 1.2-1.2.65 0 1.2.55 1.2 1.2 0 .65-.55 1.2-1.2 1.2z'></path></svg>"
  105. },
  106. "profile": {
  107. url: "https://github.com/${me}",
  108. tooltip: "Profile",
  109. hotkey: "",
  110. content: "<svg class='octicon' xmlns='http://www.w3.org/2000/svg' aria-hidden='true' height='16' viewBox='0 0 8 16' width='8'><path d='M7 6H1c-.55 0-1 .45-1 1v5h2v3c0 .55.45 1 1 1h2c.55 0 1-.45 1-1v-3h2V7c0-.55-.45-1-1-1zm0 5H6V9H5v6H3V9H2v2H1V7h6v4zm0-8c0-1.66-1.34-3-3-3S1 1.34 1 3s1.34 3 3 3 3-1.34 3-3zM4 5c-1.11 0-2-.89-2-2 0-1.11.89-2 2-2 1.11 0 2 .89 2 2 0 1.11-.89 2-2 2z' fill-rule='evenodd'/></svg>"
  111. },
  112. "settings": {
  113. url: "https://github.com/settings/profile",
  114. tooltip: "Settings",
  115. hotkey: "",
  116. content: "<svg class='octicon' xmlns='http://www.w3.org/2000/svg' aria-hidden='true' height='16' viewBox='0 0 14 16' width='14'><path d='M14 8.77v-1.6l-1.94-.64-.45-1.09.88-1.84-1.13-1.13-1.81.91-1.09-.45-.69-1.92h-1.6l-.63 1.94-1.11.45-1.84-.88-1.13 1.13.91 1.81-.45 1.09L0 7.23v1.59l1.94.64.45 1.09-.88 1.84 1.13 1.13 1.81-.91 1.09.45.69 1.92h1.59l.63-1.94 1.11-.45 1.84.88 1.13-1.13-.92-1.81.47-1.09L14 8.75v.02zM7 11c-1.66 0-3-1.34-3-3s1.34-3 3-3 3 1.34 3 3-1.34 3-3 3z'></path></svg>"
  117. },
  118. "stars": {
  119. url: "https://github.com/stars",
  120. tooltip: "Stars",
  121. hotkey: "",
  122. content: "<svg class='octicon' xmlns='http://www.w3.org/2000/svg' aria-hidden='true' height='16' viewBox='0 0 14 16' width='14'><path d='M14 6l-4.9-.64L7 1 4.9 5.36 0 6l3.6 3.26L2.67 14 7 11.67 11.33 14l-.93-4.74z'></path></svg>"
  123. },
  124. "watching": {
  125. url: "https://github.com/watching",
  126. tooltip: "Watching",
  127. hotkey: "",
  128. content: "<svg class='octicon' xmlns='http://www.w3.org/2000/svg' aria-hidden='true' height='16' viewBox='0 0 16 16' width='16'><path d='M8.06 2C3 2 0 8 0 8s3 6 8.06 6C13 14 16 8 16 8s-3-6-7.94-6zM8 12c-2.2 0-4-1.78-4-4 0-2.2 1.8-4 4-4 2.22 0 4 1.8 4 4 0 2.22-1.78 4-4 4zm2-4c0 1.11-.89 2-2 2-1.11 0-2-.89-2-2 0-1.11.89-2 2-2 1.11 0 2 .89 2 2z'></path></svg>"
  129. },
  130. "zenhub": {
  131. url: "#todo",
  132. tooltip: "ZenHub ToDo",
  133. hotkey: "",
  134. content: "<svg class='octicon' xmlns='http://www.w3.org/2000/svg' aria-hidden='true' height='16' viewBox='0 0 50 50' width='16'><path d='M29.17 45.988L13.82 21.218h10.56l-1.1-17.206 13.498 24.77h-9.514'/></svg>"
  135. }
  136. }
  137. },
  138. icons = {
  139. add: "<svg class='octicon' xmlns='http://www.w3.org/2000/svg' aria-hidden='true' height='14' viewBox='0 0 12 16' width='12'><path d='M12 9H7v5H5V9H0V7h5V2h2v5h5'/></svg>",
  140. close: "<svg class='octicon' xmlns='http://www.w3.org/2000/svg' width='9' height='9' viewBox='0 0 9 9'><path d='M9 1L5.4 4.4 9 8 8 9 4.6 5.4 1 9 0 8l3.6-3.5L0 1l1-1 3.5 3.6L8 0l1 1z'></path></svg>",
  141. info: "<svg class='octicon' xmlns='http://www.w3.org/2000/svg' height='16' width='14' viewBox='0 0 16 14'><path d='M6 10h2v2H6V10z m4-3.5c0 2.14-2 2.5-2 2.5H6c0-0.55 0.45-1 1-1h0.5c0.28 0 0.5-0.22 0.5-0.5v-1c0-0.28-0.22-0.5-0.5-0.5h-1c-0.28 0-0.5 0.22-0.5 0.5v0.5H4c0-1.5 1.5-3 3-3s3 1 3 2.5zM7 2.3c3.14 0 5.7 2.56 5.7 5.7S10.14 13.7 7 13.7 1.3 11.14 1.3 8s2.56-5.7 5.7-5.7m0-1.3C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7S10.86 1 7 1z'/></svg>",
  142. separator: "<svg class='octicon' xmlns='http://www.w3.org/2000/svg' aria-hidden='true' height='16' viewBox='0 0 12 16' width='12'><path d='M7 16H5V0h2'/></svg>"
  143. };
  144.  
  145. let drake,
  146. editMode = false,
  147. panelHashTriggered = false,
  148. // remember scrollTop when settings panel opens (if using sticky nav header
  149. // style)
  150. scrollTop = 0,
  151. settings = GM_getValue("custom-links", defaults);
  152.  
  153. function addPanel() {
  154. GM_addStyle(`
  155. /* Use border right when a vertical bar is added */
  156. .HeaderNavlink.ghcn-separator { border-right:#777 1px solid;
  157. padding:4px 0; }
  158. /* settings panel */
  159. #ghcn-overlay { position:fixed; top:50px; left:0; right:0; bottom:0;
  160. z-index:45; background:rgba(0,0,0,.5); display:none; }
  161. #ghcn-menu { cursor:pointer; }
  162. .ghcn-close, .ghcn-code { float:right; cursor:pointer; font-size:.8em;
  163. margin-left:3px; padding:0 6px 2px 6px; }
  164. .ghcn-close .octicon { vertical-align:middle; fill:currentColor; }
  165. #ghcn-settings-inner { position:fixed; left:50%; top:60px; z-index:50;
  166. width:30rem; transform:translate(-50%,0); box-shadow:0 .5rem 1rem #111;
  167. color:#c0c0c0; display:none; }
  168. #ghcn-settings-inner input { width:85%; float:right; border-style:solid;
  169. border-width:1px; max-height:35px; }
  170. .ghcn-settings-wrapper div { line-height:38px; }
  171. #ghcn-nav-items { min-height: 38px; }
  172. #ghcn-nav-items .HeaderNavitem { margin-bottom:4px; }
  173. .ghcn-settings-wrapper hr { margin: 10px 0; }
  174. .ghcn-footer { margin-top:4px; border-top:#555 solid 1px; }
  175. li[data-ghcn] a { min-width:25px; text-align: center; }
  176. .HeaderNavlink { height:28px; padding:2px 5px; }
  177. .HeaderNav .HeaderNavlink svg,
  178. .HeaderNav .HeaderNavlink img,
  179. #ghcn-nav-items .HeaderNavlink svg,
  180. #ghcn-nav-items .HeaderNavlink img, .gu-mirror svg, .gu-mirror img {
  181. max-height:16px; fill:currentColor; vertical-align:middle;
  182. overflow:visible; }
  183. /* override white text when settings panel is open*/
  184. body.ghcn-settings-open #ghcn-nav-items .text-emphasized {
  185. color: #24292e; }
  186. /* panel open */
  187. body.ghcn-settings-open {
  188. overflow:hidden !important; /* !important overrides wiki style */ }
  189. /* hide other header elements while settings is open (overflow issues) */
  190. body.ghcn-settings-open .header-search,
  191. body.ghcn-settings-open #user-links.d-flex,
  192. body.ghcn-settings-open .header-logo-invertocat,
  193. body.ghcn-settings-open .header-logo-wordmark,
  194. .gist-header .octicon-logo-github, /* hide GitHub logo on Gist page */
  195. .zh-todo-link { display:none !important; }
  196. body.ghcn-settings-open .HeaderNav { width:100%; }
  197. body.ghcn-settings-open .HeaderNavlink > * { pointer-events:none; }
  198. body.ghcn-settings-open #ghcn-overlay,
  199. body.ghcn-settings-open #ghcn-settings-inner,
  200. #ghcn-nav-items { display:block; }
  201. body.ghcn-settings-open .HeaderNav .HeaderNavitem,
  202. .ghcn-settings-wrapper .HeaderNavitem { cursor:move;
  203. border:#555 1px solid; border-radius:4px; margin-left: 2px;
  204. display:inline-block; }
  205. body.ghcn-settings-open .HeaderNavlink,
  206. .ghcn-settings-wrapper .HeaderNavlink { min-height:auto;
  207. min-width:16px; padding-top:1px; }
  208. body.ghcn-settings-open .js-header-wrapper .HeaderNavlink.form-control,
  209. body.ghcn-settings-open .Header .HeaderNavlink.form-control {
  210. background-color: transparent; border: 1px solid #444; }
  211. /* JSON code block */
  212. .ghcn-json-code { display:none; font-family:Menlo, Inconsolata,
  213. "Droid Mono", monospace; font-size:1em; }
  214. .ghcn-visible { display:block; position:absolute; top:38px; bottom:0;
  215. left:2px; right:2px; z-index:1;
  216. width:476px; max-width:476px; }
  217. /* Dragula.min.css v3.7.2 (Microsoft definitions removed) */
  218. .gu-mirror { position:fixed !important; margin:0 !important;
  219. z-index:9999 !important; opacity:.8; list-style:none; }
  220. .gu-hide { display:none !important; }
  221. .gu-unselectable { -webkit-user-select:none !important;
  222. -moz-user-select:none !important; user-select:none !important; }
  223. .gu-transit { opacity:.2; }
  224. `);
  225.  
  226. make({
  227. el: "div",
  228. appendTo: "body",
  229. attr: {
  230. id: "ghcn-settings"
  231. },
  232. html: `
  233. <div id="ghcn-overlay"></div>
  234. <div id="ghcn-settings-inner" class="boxed-group">
  235. <h3>GitHub Custom Navigation Settings
  236. <button type="button" class="ghcn-close btn btn-sm">${icons.close}</button>
  237. <button type="button" class="ghcn-code btn btn-sm tooltipped tooltipped-w" aria-label="Toggle JSON data view">{ }</button>
  238. </h3>
  239. <div class="ghcn-settings-wrapper boxed-group-inner">
  240. <ul id="ghcn-nav-items" class="BtnGroup HeaderNav"></ul>
  241. <hr>
  242. <form>
  243. <p>Click an link above to edit its properties
  244. <a href="https://github.com/Mottie/GitHub-userscripts/wiki/GitHub-custom-navigation" class="tooltipped tooltipped-e" aria-label="Click to learn about the properties below">${icons.info}</a>
  245. </p>
  246. <div>URL
  247. <span class="tooltipped tooltipped-e" aria-label="Enter a full URL, or hash">${icons.info}</span>
  248. <input class="form-control ghcn-url" type="text"/>
  249. </div>
  250. <div>Tooltip<input class="form-control ghcn-tooltip" type="text"/></div>
  251. <div>Hotkey
  252. <a href="https://github.com/Mottie/GitHub-userscripts/wiki/GitHub-custom-navigation#hotkey" class="tooltipped tooltipped-e ghcn-hotkey-link" aria-label="Click to learn about hotkeys">${icons.info}</a>
  253. <input class="form-control ghcn-hotkey" type="text"/>
  254. </div>
  255. <div>Content
  256. <span class="tooltipped tooltipped-e" aria-label="Include text and/or HTML (&lt;svg&gt; or &lt;img&gt;)">${icons.info}</span>
  257. <input class="form-control ghcn-content" type="text"/>
  258. </div>
  259. </form>
  260. <textarea class="ghcn-json-code"></textarea>
  261. <div class="ghcn-footer">
  262. <span class="btn btn-sm ghcn-add">${icons.add} New Link</span>
  263. <span class="btn btn-sm ghcn-destroy btn-danger tooltipped tooltipped-n" aria-label="Completely remove selected link">Destroy</span>
  264. <span class="btn btn-sm ghcn-reset btn-danger tooltipped tooltipped-n tooltipped-multiline" aria-label="Reset to default
  265. (Removes all custom entries!)">Reset</span>
  266. <span class="btn btn-sm ghcn-restore tooltipped tooltipped-n" aria-label="Restore missing default entries">Restore</span>
  267. </div>
  268. </div>
  269. </div>`
  270. });
  271. }
  272.  
  273. function updatePanel() {
  274. let indx, item, inNav, inSettings,
  275. panelStr = "#ghcn-nav-items",
  276. panel = $(panelStr),
  277. setItems = settings[getLocation()],
  278. keys = Object.keys(settings.items),
  279. len = keys.length;
  280. for (indx = 0; indx < len; indx++) {
  281. item = keys[indx];
  282. inNav = setItems.indexOf(item) > -1;
  283. inSettings = $(panelStr + ` .HeaderNavitem[data-ghcn="${item}"]`);
  284. // customize adds stuff to main nav
  285. if (inNav && inSettings) {
  286. panel.removeChild(inSettings);
  287. } else if (!inNav && !inSettings) {
  288. addToMenu(item, panelStr);
  289. }
  290. }
  291. if (!$(panelStr + " .HeaderNavitem[data-ghcn='separator']")) {
  292. addToMenu("separator", panelStr);
  293. }
  294. selectItem();
  295. }
  296.  
  297. function openPanel() {
  298. scrollTop = document.documentElement.scrollTop;
  299. window.scrollTo(0, 0);
  300. $("body").classList.add("ghcn-settings-open");
  301. editMode = true;
  302. customize();
  303. $(".modal-backdrop").click();
  304. $(".ghcn-json-code").classList.remove("ghcn-visible");
  305. }
  306.  
  307. function openPanelOnHash() {
  308. if (!panelHashTriggered && window.location.hash === panelHash) {
  309. panelHashTriggered = true;
  310. openPanel();
  311. // immediately remove the hash because I noticed issues where the "#" was
  312. // removed; and upon reload, a 404 page is shown because
  313. // "https://github.com/github-custom-navigation-settings" does not exist
  314. history.pushState("", document.title, window.location.pathname);
  315. panelHashTriggered = false;
  316. }
  317. }
  318.  
  319. function closePanel() {
  320. if (editMode) {
  321. window.scrollTo(0, scrollTop);
  322. $("body").classList.remove("ghcn-settings-open");
  323. editMode = false;
  324. customize();
  325. $(".ghcn-json-code").classList.remove("ghcn-visible");
  326. }
  327. }
  328.  
  329. function getLocation() {
  330. // used by "settings" object
  331. return window.location.hostname === "gist.github.com" ? "gists" : "github";
  332. }
  333.  
  334. // continually destroying & reapplying Dragula sometimes ignores elements;
  335. // so just leave it always applied
  336. function addDragula() {
  337. let topNav = $(".HeaderNav");
  338. drake = dragula($$(".HeaderNav, #ghcn-nav-items"), {
  339. invalid: () => {
  340. return !editMode;
  341. }
  342. });
  343. drake.on("drop", () => {
  344. let indx, link,
  345. temp = [],
  346. list = topNav.childNodes,
  347. len = list.length;
  348. for (indx = 0; indx < len; indx++) {
  349. link = list[indx].getAttribute("data-ghcn");
  350. if (link) {
  351. temp[temp.length] = link;
  352. }
  353. }
  354. settings[getLocation()] = temp;
  355. GM_setValue("custom-links", settings);
  356. updatePanel();
  357.  
  358. });
  359. }
  360.  
  361. // Clicked item; show selection
  362. function selectItem() {
  363. // highlight current link
  364. let temp = $$(".HeaderNavlink.focus");
  365. removeClass(temp, "focus");
  366. temp = $$(".HeaderNavitem[data-ghcn='" + (settings.currentLink || "") +
  367. "'] .HeaderNavlink");
  368. if (temp[0]) {
  369. addClass(temp, "focus");
  370. updateLink(temp[0].parentNode);
  371. }
  372. }
  373.  
  374. // New Link button pressed
  375. function createLink() {
  376. let name = findUniqueId("custom");
  377. settings.items[name] = {
  378. url: "",
  379. tooltip: "",
  380. hotkey: "",
  381. content: "*"
  382. };
  383. addToMenu(name, "#ghcn-nav-items");
  384. settings.currentLink = name;
  385. selectItem();
  386. }
  387.  
  388. // append named list item to menu
  389. function addToMenu(name, target) {
  390. let html,
  391. item = settings.items[name] || {},
  392. url = (item.url || "").replace(/\$\{me\}/g, user),
  393. linkClass = "text-emphasized HeaderNavlink " +
  394. (editMode ? "" : "js-selected-navigation-item");
  395. // only show tooltip if defined
  396. if (item.tooltip) {
  397. linkClass += " tooltipped tooltipped-s";
  398. if (/(&#10;|&#xA;)/g.test(item.tooltip)) {
  399. linkClass += " tooltipped-multiline";
  400. }
  401. }
  402. if (name === "separator") {
  403. html = editMode
  404. // *** Separator (icon in editMode; zero-width-space when not)
  405. ? `<span class="${linkClass} tooltipped tooltipped-s" aria-label="Menu separator">${icons.separator}</span>`
  406. : `<span class="HeaderNavlink ghcn-separator linkable-line-number">&#8203;</span>`;
  407. } else {
  408. html = editMode ?
  409. `<span class="${linkClass}" aria-label="${item.tooltip}">${item.content}</span>` :
  410. // GitHub might get upset, but we're not going to bother with analytics;
  411. // not including "data-ga-click" nor "data-selected-links" attributes
  412. `<a href="${url}" class="${linkClass}" aria-label="${item.tooltip}" data-hotkey="${item.hotkey}">
  413. ${item.content}
  414. </a>`;
  415. }
  416. make({
  417. el: "span",
  418. appendTo: target,
  419. attr: {
  420. "data-ghcn": name
  421. },
  422. cl4ss: "HeaderNavitem",
  423. html: html
  424. });
  425. }
  426.  
  427. // Destroy button pressed
  428. function destroyLink(item) {
  429. if (item) {
  430. delete settings.items[item];
  431. GM_setValue("custom-links", settings);
  432. let el,
  433. indx = settings.github.indexOf(item);
  434. if (indx >= 0) {
  435. settings.github.splice(indx, 1);
  436. }
  437. indx = settings.gists.indexOf(item);
  438. if (indx >= 0) {
  439. settings.gists.splice(indx, 1);
  440. }
  441. el = $(`.HeaderNavitem[data-ghcn="${item}"]`);
  442. if (el) {
  443. el.parentNode.removeChild(el);
  444. }
  445. if ((settings.currentLink || "") === item) {
  446. settings.currentLink = "";
  447. }
  448. updateLink();
  449. }
  450. }
  451.  
  452. // Reset button pressed or new JSON added
  453. function resetLinks(newSettings) {
  454. if (newSettings) {
  455. settings = newSettings;
  456. } else {
  457. // quick n'dirty deep merge
  458. let str = JSON.stringify(defaults);
  459. settings = JSON.parse(str);
  460. }
  461. GM_setValue("custom-links", settings);
  462. // remove extra items individually; dragula doesn't seem to like it when we
  463. // use innerHTML = ""
  464. let item,
  465. els = $$(".HeaderNavitem"),
  466. indx = els.length;
  467. while (indx--) {
  468. item = els[indx].getAttribute("data-ghcn");
  469. if (item !== "separator" && !settings.items.hasOwnProperty(item)) {
  470. destroyLink(item);
  471. }
  472. }
  473. customize();
  474. }
  475.  
  476. function restoreLinks() {
  477. Object.assign(settings.items, defaults.items);
  478. GM_setValue("custom-links", settings);
  479. updatePanel();
  480. }
  481.  
  482. // Clicked item; update input values
  483. function updateLink(el) {
  484. let item = el && el.getAttribute("data-ghcn") || "",
  485. link = settings.items[item] || {};
  486. settings.currentLink = item;
  487. $(".ghcn-url").value = link.url || "";
  488. $(".ghcn-tooltip").value = link.tooltip || "";
  489. $(".ghcn-hotkey").value = link.hotkey || "";
  490. $(".ghcn-content").value = link.content || "";
  491.  
  492. // "separator" shouldn't show options
  493. $(".ghcn-settings-wrapper form").style.visibility = item === "separator" ?
  494. "hidden" :
  495. "visible";
  496. }
  497.  
  498. // save changes on-the-fly
  499. function saveLink() {
  500. let name = settings.currentLink || "",
  501. item = $(`.HeaderNavitem[data-ghcn="${name}"] .HeaderNavlink`);
  502. if (name) {
  503. settings.items[name] = {
  504. url: $(".ghcn-url").value,
  505. tooltip: $(".ghcn-tooltip").value,
  506. hotkey: $(".ghcn-hotkey").value,
  507. content: $(".ghcn-content").value
  508. };
  509. GM_setValue("custom-links", settings);
  510. // update item (should be unique)
  511. if (item) {
  512. // "\n" is the only thing that works as a carriage return for
  513. // javascript's setAttribute; see
  514. // http://wowmotty.blogspot.com/2014/04/methods-to-add-multi-line-css-content.html
  515. item.setAttribute(
  516. "aria-label",
  517. settings.items[name].tooltip.replace(/(&#10;|&#xA;)/g, "\n")
  518. );
  519. item.innerHTML = settings.items[name].content;
  520. }
  521. }
  522. }
  523.  
  524. function addJSON() {
  525. $(".ghcn-json-code").value = JSON.stringify(settings, null, 2);
  526. }
  527.  
  528. function processJSON() {
  529. let val,
  530. txt = $(".ghcn-json-code").value;
  531. try {
  532. val = JSON.parse(txt);
  533. } catch (err) {
  534. console.error("GitHub Custom Navigation: Invalid JSON!");
  535. }
  536. return val;
  537. }
  538.  
  539. function addBindings() {
  540. // Create a menu entry
  541. let el,
  542. menu = make({
  543. el: "a",
  544. cl4ss: "dropdown-item",
  545. html: "Custom Nav Settings",
  546. attr: {
  547. id: "ghcn-menu"
  548. }
  549. });
  550.  
  551. el = $$(`
  552. .Header .dropdown-item[href='/settings/profile'],
  553. .Header .dropdown-item[data-ga-click*='go to profile'],
  554. .js-header-wrapper .dropdown-item[href='/settings/profile'],
  555. .js-header-wrapper .dropdown-item[data-ga-click*='go to profile']`
  556. );
  557. // get last found item - gists only have the "go to profile" item; GitHub
  558. // has both
  559. el = el[el.length - 1];
  560. if (el) {
  561. // insert after
  562. el.parentNode.insertBefore(menu, el.nextSibling);
  563. on($("#ghcn-menu"), "click", openPanel);
  564. }
  565.  
  566. on(window, "hashchange", openPanelOnHash);
  567. on($("#ghcn-overlay"), "click", event => {
  568. // ignore bubbled up events
  569. if (event.target.id === "ghcn-overlay") {
  570. closePanel();
  571. }
  572. });
  573. on($("body"), "keyup", event => {
  574. // using F2 key for testing
  575. if (editMode && event.keyCode === 27) {
  576. closePanel();
  577. }
  578. });
  579. on($("body"), "click", event => {
  580. const target = event.target;
  581. if (editMode && target.classList.contains("HeaderNavlink")) {
  582. // HeaderNavlink is a child of HeaderNavitem, but is the same size
  583. settings.currentLink = target.parentNode.getAttribute("data-ghcn");
  584. selectItem();
  585. }
  586. });
  587. on($$(".ghcn-settings-wrapper input"), "input change", saveLink);
  588. on($(".ghcn-add"), "click", createLink);
  589. on($(".ghcn-destroy"), "click", () => destroyLink(settings.currentLink));
  590. on($(".ghcn-reset"), "click", resetLinks);
  591. on($(".ghcn-restore"), "click", restoreLinks);
  592. // close panel when hotkey link is clicked or the page scrolls on the
  593. // documentation wiki
  594. on($$(".ghcn-close, .ghcn-hotkey-link"), "click", closePanel);
  595.  
  596. // Code
  597. on($(".ghcn-code"), "click", () => {
  598. // open JSON code textarea
  599. $(".ghcn-json-code").classList.toggle("ghcn-visible");
  600. addJSON();
  601. });
  602. // close JSON code textarea
  603. on($(".ghcn-json-code"), "focus", function() {
  604. this.select();
  605. });
  606. on($(".ghcn-json-code"), "paste", () => {
  607. setTimeout(() => {
  608. checkJSON(processJSON());
  609. }, 200);
  610. });
  611.  
  612. }
  613.  
  614. function checkJSON(val, init) {
  615. let hasGitHub = false,
  616. hasGists = false,
  617. hasItems = false;
  618. if (val) {
  619. hasGitHub = val.hasOwnProperty("github");
  620. hasGists = val.hasOwnProperty("gists");
  621. hasItems = val.hasOwnProperty("items");
  622. // simple validation
  623. if (hasGitHub && hasGists && hasItems) {
  624. if (!init) {
  625. resetLinks(val);
  626. $(".ghcn-json-code").classList.remove("ghcn-visible");
  627. selectItem();
  628. }
  629. return true;
  630. }
  631. }
  632. let msg = [];
  633. if (!hasGitHub) {
  634. msg.push(`"github"`);
  635. }
  636. if (!hasGists) {
  637. msg.push(`"gists"`);
  638. }
  639. if (!hasItems) {
  640. msg.push(`"items"`);
  641. }
  642. msg = msg.length ? "JSON is missing " + msg.join(" & ") : "Invalid JSON";
  643. console.error("GitHub Custom Navigation: " + msg, val);
  644. return false;
  645. }
  646.  
  647. // add new link; needs a unique ID
  648. function findUniqueId(prefix) {
  649. let indx = 0,
  650. id = prefix + indx;
  651. if (settings.items[id]) {
  652. while (settings.items[id]) {
  653. id = prefix + indx++;
  654. }
  655. }
  656. return id;
  657. }
  658.  
  659. // Main process - adds links to header navigation
  660. function customize() {
  661. let nav = $(".Header nav");
  662. if (nav) {
  663. nav.classList.add("HeaderNav");
  664. let indx, els,
  665. navStr = ".HeaderNav",
  666. setItems = settings[getLocation()],
  667. len = setItems.length;
  668. if (!len) {
  669. return;
  670. }
  671.  
  672. els = nav.childNodes;
  673. indx = els.length;
  674. while (indx--) {
  675. nav.removeChild(els[indx]);
  676. }
  677.  
  678. for (indx = 0; indx < len; indx++) {
  679. addToMenu(setItems[indx], navStr);
  680. }
  681. // make sure all svg's have an "octicon" class name
  682. addClass($$(navStr + " svg"), "octicon");
  683.  
  684. if (editMode) {
  685. updatePanel();
  686. }
  687. }
  688. }
  689.  
  690. function $(selector, el) {
  691. return (el || document).querySelector(selector);
  692. }
  693.  
  694. function $$(selector, el) {
  695. return [...(el || document).querySelectorAll(selector)];
  696. }
  697.  
  698. function addClass(els, name) {
  699. let indx = els.length;
  700. while (indx--) {
  701. els[indx].classList.add(name);
  702. }
  703. }
  704.  
  705. function removeClass(els, name) {
  706. let indx = els.length;
  707. while (indx--) {
  708. els[indx].classList.remove(name);
  709. }
  710. }
  711.  
  712. function on(els, name, callback) {
  713. els = Array.isArray(els) ? els : [els];
  714. let events = name.split(/\s+/);
  715. els.forEach(el => {
  716. events.forEach(ev => {
  717. el.addEventListener(ev, callback);
  718. });
  719. });
  720. }
  721.  
  722. function make(obj) {
  723. let key,
  724. el = document.createElement(obj.el);
  725. if (obj.cl4ss) {
  726. el.className = obj.cl4ss;
  727. }
  728. if (obj.html) {
  729. el.innerHTML = obj.html;
  730. }
  731. if (obj.attr) {
  732. for (key in obj.attr) {
  733. if (obj.attr.hasOwnProperty(key)) {
  734. el.setAttribute(key, obj.attr[key]);
  735. }
  736. }
  737. }
  738. if (obj.appendTo) {
  739. $(obj.appendTo).appendChild(el);
  740. }
  741. return el;
  742. }
  743.  
  744. let isValid = checkJSON(settings, "init");
  745. if (!isValid) {
  746. resetLinks();
  747. }
  748. customize();
  749. addPanel();
  750. addBindings();
  751. addDragula();
  752. openPanelOnHash();
  753.  
  754. })();