GitHub Custom Navigation

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

当前为 2020-09-08 提交的版本,查看 最新版本

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