GitHub Watcher

A userscript that can check a repo, folder, file or branch for updates

  1. // ==UserScript==
  2. // @name GitHub Watcher
  3. // @version 0.2.3
  4. // @description A userscript that can check a repo, folder, file or branch for updates
  5. // @license MIT
  6. // @author Rob Garrison
  7. // @namespace https://github.com/Mottie
  8. // @match https://github.com/*
  9. // @match https://gist.github.com/*
  10. // @run-at document-idle
  11. // @grant GM_addStyle
  12. // @grant GM_getValue
  13. // @grant GM_setValue
  14. // @require https://greatest.deepsurf.us/scripts/28721-mutations/code/mutations.js?version=1108163
  15. // @require https://greatest.deepsurf.us/scripts/398877-utils-js/code/utilsjs.js?version=1079637
  16. // @icon https://github.githubassets.com/pinned-octocat.svg
  17. // @supportURL https://github.com/Mottie/GitHub-userscripts/issues
  18. // ==/UserScript==
  19.  
  20. /* global $ $$ on make */
  21. (() => {
  22. "use strict";
  23.  
  24. GM_addStyle(`
  25. .ghwr-list-wrap { max-height:30em; overflow-y:auto; }
  26. .ghwr-item-entry .error, .ghwr-item-status {
  27. width:14px; height:14px; color: var(--color-text-white, #111);
  28. background-image: linear-gradient(#54a3ff, #006eed);
  29. background-clip: padding-box; border: 2px solid #181818;
  30. border-radius: 50%; display:inline-block; }
  31. .ghwr-item-status { position:absolute; top:1px; left:6px; z-index:2;
  32. display:none; }
  33. .ghwr-item-entry .btn-panel { top:-10em; opacity:0; }
  34. .ghwr-item-entry .error, .ghwr-item-status.error {
  35. background-image: linear-gradient(#e00, #703); }
  36. .ghwr-item-status.unread { display:inline-block; }
  37. .ghwr-item-entry:hover, .ghwr-item-entry:focus-within,
  38. .ghwr-item-entry:hover .btn-panel, .ghwr-item-entry:focus-within .btn-panel {
  39. background-color: var(--color-state-hover-primary-bg);
  40. color:var(--color-text); }
  41. .ghwr-item-entry:hover .octicon, .ghwr-item-entry:focus-within .octicon {
  42. color:var(--color-text); }
  43. .ghwr-item-entry:hover .btn-panel, .ghwr-item-entry:focus-within .btn-panel {
  44. top:auto; opacity:1; }
  45. .ghwr-item-entry .btn-panel, .ghwr-last-checked .ghwr-right-panel {
  46. position:absolute; right:0; }
  47. .ghwr-last-checked { color:var(--color-text-primary); padding:4px 8px; }
  48. .ghwr-item-entry { position:relative; }
  49. .ghwr-item-entry:hover a, .ghwr-item-entry:focus-within a,
  50. .ghwr-item-entry:hover button, .ghwr-item-entry:focus-within button {
  51. color:var(--color-state-hover-primary-text); }
  52. .ghwr-item-entry > button:hover, .ghwr-item-entry > button:focus {
  53. color:var(--color-btn-text); }
  54. .ghwr-item-entry label { width:75%; }
  55. .ghwr-item-entry input[type="text"] { width:100%; }
  56. .ghwr-item-entry .editing { justify-content:space-between; }
  57. .ghwr-items button { border:1px solid var(--color-btn-border); padding:0;
  58. border-radius:4px; }
  59. .ghwr-items button:not(:hover):not(:focus),
  60. .ghwr-item-entry:not(.editing) button:not([aria-checked="true"]) {
  61. background:#0000; border-color:#0000; }
  62. .ghwr-items button:hover, .ghwr-items button:focus, .ghwr-last-checked a:hover,
  63. .ghwr-last-checked a:focus { border-color:var(--color-btn-focus-border);
  64. outline:none; box-shadow:var(--color-btn-focus-shadow); }
  65. .ghwr-items .btn-panel button:hover, .ghwr-items .btn-panel button:focus {
  66. border-color:#eee; box-shadow:#eee; }
  67. .ghwr-last-checked button:not([aria-checked="true"]) { border-color:#0000;
  68. background:#0000; }
  69. .ghwr-items button[data-type="check"]:not([aria-checked="true"]) {
  70. cursor:auto; }
  71. .btn-panel button { color:var(--color-state-hover-primary-text);
  72. filter:brightness(90%); }
  73. .btn-panel button:hover, .btn-panel button:focus { filter:brightness(110%); }
  74. .ghwr-items li svg { pointer-events:none; fill:currentColor; }
  75. .ghwr-items li .btn[data-type="check"][aria-checked="false"] svg {
  76. visibility:hidden; }
  77. .ghwr-settings { padding:4px 8px 4px 16px; }
  78. .ghwr-settings input[type="number"] { width:5em; }
  79. .ghwr-items { --color-tooltip-bg:#343434; }
  80. .ghwr-items { width:50vw; min-width:500px; }
  81. @media (max-width:768px) { .ghwr-items { width:100vw; min-width:unset; }
  82. .hide-small { display:none; } }
  83. @media (max-width:1260px) { .ghwr-items { width:75vw; } }`
  84. );
  85.  
  86. let token = GM_getValue("github-token");
  87. let timer;
  88. let timer2;
  89. let focus;
  90. let options;
  91. let wrap = null;
  92. let showSettings = !token;
  93.  
  94. const encodedChars = /[&"'<>\n]/g;
  95. const sanitize = text => text.replace(
  96. encodedChars,
  97. r => `&#${r.charCodeAt(0)};`
  98. );
  99.  
  100. const timeElement = time =>
  101. `<relative-time datetime="${time}" class="no-wrap"></relative-time>`;
  102.  
  103. const watcherIcon = `
  104. <svg class="octicon octicon-spy" width="20" height="20" viewBox="0 0 24 24">
  105. <path d="M14.8 4c-1.3-.2-2.5 1.2-3.8.5-1-.6-2.9-.8-3.7.4C6.2 6.3 6.2 8.3 5.6 10c-.5 1.1-2 .3-2.8 1-1 .6-.5 2 .3 2.5.6.6 1.7.5 2 1.2.3.4 1.1 1.4.4 1.6l-4.1 2 3.1 5.3 1-.6-2.6-4.3 3.3-1.5c.9 2.1 2 4.2 4 5.5 1.6 1 3.7.2 4.8-1.1a12 12 0 002.5-4.2l3.7 1.6-2.7 4.3.9.6 3.4-5.4-5-2c.5-.6.3-1.8 1-2 1.2-.3 2.6-1 2.8-2.3 0-1.5-1.7-1.3-2.7-1.5-.5-1-.6-2-1-3-1-2.3-1.7-3.6-3-3.7zm.5 1c1.3 1 1.5 2.5 1.9 3.9.3 1 1 2.4-.6 2.4-2 .7-4.2.9-6.3.7-1.3-.2-2.6-.7-3.8-1.3.7-1.8.6-3.9 1.9-5.4 1-.7 2.1.5 3.2.5 1.4.3 2.4-1 3.7-.7zm-9.4 6.5c1.8 1.3 4 1.8 6.3 1.7 1.2 0 2.5-.3 3.7-.5 1.1-.3 2.2-1 3.4-.8.8-.3 1.8.4.7 1-1.3.7-3 1-4.4 1.3-4 .7-8.1.1-11.8-1.4-.8-.4-.4-1 .4-1.1.5 0 1.1 0 1.7-.2zM7 14.8c1.1.3 3 .4 4.1.7-.9 1.1-3.3.7-4-.7zm9.6.1c-1 .8-2.1 2-3.8.6 1.2 0 2.7-.3 3.8-.6zm-10 .8c1 .8 2.1 2.2 5.3.4 1.7 1.5 3.1.8 4.9 0-.8 2.2-1.8 4.7-4.1 5.8-1.3.6-2.7-.3-3.4-1.3-1.2-1.5-2-3.2-2.7-5z"/>
  106. </svg>`;
  107.  
  108. const buttonCheck = (label, index) => `
  109. <button type="button" data-type="check" data-index="${index}" class="btn btn-sm tooltipped-e" aria-label="${label}">
  110. <svg class="octicon octicon-check" width="16" height="16" viewBox="0 0 16 16" aria-hidden="true">
  111. <path fill-rule="evenodd" d="M13.78 4.22a.75.75 0 010 1.06l-7.25 7.25a.75.75 0 01-1.06 0L2.22 9.28a.75.75 0 011.06-1.06L6 10.94l6.72-6.72a.75.75 0 011.06 0z"></path>
  112. </svg>
  113. </button>`;
  114.  
  115. const buttonPanel = index => `
  116. <span class="btn-panel px-2">
  117. <button type="button" data-type="edit" data-index="${index}" class="ml-1" aria-label="Edit">
  118. <svg class="octicon octicon-pencil" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true">
  119. <path fill-rule="evenodd" d="M11.013 1.427a1.75 1.75 0 012.474 0l1.086 1.086a1.75 1.75 0 010 2.474l-8.61 8.61c-.21.21-.47.364-.756.445l-3.251.93a.75.75 0 01-.927-.928l.929-3.25a1.75 1.75 0 01.445-.758l8.61-8.61zm1.414 1.06a.25.25 0 00-.354 0L10.811 3.75l1.439 1.44 1.263-1.263a.25.25 0 000-.354l-1.086-1.086zM11.189 6.25L9.75 4.81l-6.286 6.287a.25.25 0 00-.064.108l-.558 1.953 1.953-.558a.249.249 0 00.108-.064l6.286-6.286z"></path>
  120. </svg>
  121. </button>
  122. <button type="button" data-type="delete" data-index="${index}" class="ml-1 color-text-danger" aria-label="Delete">
  123. <svg class="octicon octicon-trashcan" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true">
  124. <path fill-rule="evenodd" d="M6.5 1.75a.25.25 0 01.25-.25h2.5a.25.25 0 01.25.25V3h-3V1.75zm4.5 0V3h2.25a.75.75 0 010 1.5H2.75a.75.75 0 010-1.5H5V1.75C5 .784 5.784 0 6.75 0h2.5C10.216 0 11 .784 11 1.75zM4.496 6.675a.75.75 0 10-1.492.15l.66 6.6A1.75 1.75 0 005.405 15h5.19c.9 0 1.652-.681 1.741-1.576l.66-6.6a.75.75 0 00-1.492-.149l-.66 6.6a.25.25 0 01-.249.225h-5.19a.25.25 0 01-.249-.225l-.66-6.6z"></path>
  125. </svg>
  126. </button>
  127. </span>`;
  128.  
  129. const editRow = (item, index) => `
  130. <label class="d-flex ml-3 pl-1 pr-2">
  131. URL:&nbsp;
  132. <input type="text" class="form-control input-sm" value="${item.url || ""}" autofocus />
  133. </label>
  134. <button type="button" data-type="save" data-index="${index}" class="btn px-2">
  135. Save
  136. </button>
  137. <button type="button" data-type="cancel" data-index="${index}" class="btn px-2 ml-2">
  138. Cancel
  139. </button>`;
  140.  
  141. const errorRow = (item, index) => `
  142. <span role="alert" class="d-flex flex-items-center">
  143. <span class="error mr-1" aria-hidden="true"></span>
  144. Error: ${item.error || "Please enter a GitHub URL"}
  145. ${buttonPanel(index)}
  146. </span>`;
  147.  
  148. const itemRow = (item, index) => {
  149. const { org, repo, branch = "master", path } = getItemDataFromUrl(item);
  150. const message = sanitize(item.message?.trim() || "");
  151. return `
  152. ${buttonCheck("Clear this watched update", index)}
  153. <span title="${org}/${repo}...${path || ""}\n\n${message}">
  154. <span class="hide-small">Updated </span>
  155. ${timeElement(item.date)}
  156. &ndash;
  157. <a href="${item.url}">
  158. ${path?.split("/").slice(-1) ||
  159. `${org}/${repo}${branch === "master" ? "" : ` (${branch})`}`}
  160. </a>
  161. &ndash; by ${item.name} &ndash;
  162. ${message}
  163. </span>
  164. ${buttonPanel(index)}`;
  165. };
  166.  
  167. // Easier to maintain 3 separate than one big one
  168. const regexes = [
  169. /github.com\/(?<org>.+?)\/(?<repo>.+?)\/(.+?)\/(?<branch>.+?)\/(?<path>.+$)/,
  170. /github.com\/(?<org>.+?)\/(?<repo>.+?)\/(.+?)\/(?<branch>.+?)$/,
  171. /github.com\/(?<org>.+?)\/(?<repo>.+?)$/
  172. ];
  173. const getItemDataFromUrl = ({ url = "" }) => {
  174. const match = regexes.reduce((match, regex) => {
  175. const test = url.match(regex);
  176. if (!match.done && test) {
  177. return { done: true, ...test };
  178. }
  179. return match;
  180. }, { done: false });
  181. return match?.groups || {};
  182. };
  183.  
  184. const buildQuery = () => options.items.map((item, index) => {
  185. const { org, repo, branch, path } = getItemDataFromUrl(item);
  186. const pathQuery = (path || "") !== "" ? `, path: "${path}"` : "";
  187. return org && repo
  188. ? `item${index}: repository(name: "${repo}", owner: "${org}") {
  189. ref(qualifiedName: "${branch || "master"}") {
  190. target {
  191. ...on Commit{
  192. history(first:1${pathQuery}) {
  193. nodes {
  194. author {
  195. name
  196. date
  197. }
  198. message
  199. }
  200. }
  201. }
  202. }
  203. }
  204. }` : null;
  205. });
  206.  
  207. const calculatedInterval = (minutes = options.interval) => minutes * 60 * 1000;
  208.  
  209. const setLastCheckedTime = () => {
  210. options.lastChecked =
  211. token && options.items.filter(item => item.url).length > 0
  212. ? Date.now()
  213. : null;
  214. };
  215.  
  216. const setOptions = () => {
  217. GM_setValue("options", options);
  218. };
  219.  
  220. const getOptions = () => {
  221. options = Object.assign({}, {
  222. interval: 60, // check status every x minutes
  223. lastChecked: null,
  224. items: []
  225. }, GM_getValue("options") || {});
  226. };
  227.  
  228. const updateOptions = ({ data, errors }) => {
  229. $(".ghwr-details", wrap).classList.toggle("error", errors?.length);
  230. if (data || errors) {
  231. const emptyUrl = "Please enter a GitHub URL";
  232. // Update stored data
  233. options.items = options.items
  234. .map((item, index) => {
  235. const { author: { name, date } = {}, message } =
  236. data[`item${index}`]?.ref?.target?.history?.nodes[0] || {};
  237. const hasError = !name || !date
  238. ? { message: `${item.url ? `${item.url} is not a valid URL` : emptyUrl}` }
  239. : errors?.find(err => err.path[0] === `item${index}`);
  240.  
  241. return {
  242. ...item,
  243. name,
  244. message,
  245. date,
  246. unread: item.unread || date !== item.date,
  247. error: hasError ? hasError.message : "",
  248. editing: false
  249. };
  250. })
  251. // Sort by date to make the rendered list easy to read
  252. .sort((a, b) => new Date(b.date) - new Date(a.date));
  253. setLastCheckedTime();
  254. setOptions();
  255. showStatusIndicator();
  256. }
  257. };
  258.  
  259. // item list is resorted after every update; so find row by url
  260. const findRow = (index, selector) => {
  261. const url = options.items[index]?.url
  262. return url
  263. ? `.dropdown-item[data-url="${url}"] ${selector}`
  264. : null;
  265. }
  266.  
  267. const trailingSlashRegex = /\/$/g;
  268. const removeTrailingSlash = url => url.replace(trailingSlashRegex, "");
  269.  
  270. const handleClick = async ({ type, index } = {}) => {
  271. let value;
  272. if (type) {
  273. switch (type) {
  274. case "check":
  275. if (index === "all") {
  276. options.items = options.items.map(item => (
  277. { ...item, unread: false }
  278. ));
  279. focus = "button[data-type='refresh']";
  280. } else {
  281. options.items[index].unread = false;
  282. focus = findRow(index, "a");
  283. }
  284. break;
  285. case "add":
  286. options.items.push({ editing: true });
  287. break;
  288. case "delete":
  289. options.items.splice(index, 1);
  290. focus = findRow(index, "a");
  291. break;
  292. case "cancel":
  293. options.items[index].editing = false;
  294. focus = findRow(index, "button[data-type='edit']");
  295. break;
  296. case "edit":
  297. options.items[index].editing = true;
  298. focus = findRow(index, "input[type='text']");
  299. break;
  300. case "save":
  301. value = $("input", $$(".ghwr-item-entry")[index])?.value;
  302. if (value && value !== options.items[index].url) {
  303. options.items[index] = { url: removeTrailingSlash(value) };
  304. options.lastChecked = null;
  305. }
  306. options.items[index].editing = false;
  307. focus = "button[data-type='add']";
  308. break;
  309. case "refresh":
  310. options.lastChecked = null;
  311. focus = "button[data-type='refresh']";
  312. break;
  313. case "settings":
  314. showSettings = !showSettings;
  315. focus = "input[type='password']";
  316. break;
  317. case "save-settings":
  318. options.error = "";
  319. value = parseInt($("input[type='number']", wrap).value, 10);
  320. if (!isNaN(value) && value > 1 && value !== options.interval) {
  321. options.interval = value;
  322. options.lastChecked = null;
  323. }
  324. value = $("input[type='password']", wrap).value;
  325. if (value && value !== token) {
  326. token = value;
  327. GM_setValue("github-token", value);
  328. options.lastChecked = null;
  329. }
  330. showSettings = false;
  331. focus = "button[data-type='settings']";
  332. break;
  333. case "cancel-settings":
  334. showSettings = false;
  335. focus = "button[data-type='settings']";
  336. break;
  337. }
  338. setOptions();
  339. renderStatus();
  340. }
  341. }
  342.  
  343. const setFocus = () => {
  344. setTimeout(() => {
  345. if (focus) {
  346. $(focus, wrap)?.focus();
  347. focus = "";
  348. }
  349. });
  350. };
  351.  
  352. const getStatus = () => options.items
  353. .filter(item => item.unread)
  354. .length > 0;
  355.  
  356. const setStatus = () => {
  357. $$(".ghwr-item-entry button[data-type='check']", wrap)?.forEach(btn => {
  358. const indx = btn.dataset.index;
  359. const isChecked = options.items[indx]?.unread;
  360. btn.setAttribute("aria-checked", isChecked);
  361. btn.disabled = !isChecked;
  362. // toggle tooltips
  363. btn.classList.toggle("tooltipped", isChecked);
  364. });
  365. const isChecked = getStatus();
  366. const lastChkBtn = $(".ghwr-last-checked button[data-type='check']", wrap);
  367. lastChkBtn.setAttribute("aria-checked", isChecked);
  368. lastChkBtn.classList.toggle("tooltipped", isChecked);
  369. lastChkBtn.disabled = !isChecked;
  370. showStatusIndicator();
  371. };
  372.  
  373. const renderStatus = async () => {
  374. if (!$(".ghwr-details")?.open) {
  375. return;
  376. }
  377.  
  378. await fetchStatus();
  379. const list = options.items
  380. .map((item, index) => {
  381. let content = `<span class="ml-4">Unknown</span>${buttonPanel(index)}`;
  382. if (item.editing) {
  383. content = editRow(item, index);
  384. } else if (item.error || !item.url) {
  385. content = errorRow(item, index);
  386. } else if (item.url) {
  387. content = itemRow(item, index);
  388. }
  389. const rowClass = [
  390. "d-flex flex-items-center dropdown-item ghwr-item-entry mr-2 px-2",
  391. item.editing ? " editing" : "",
  392. ].join("")
  393. return `<li class="${rowClass}" data-url="${item.url}">${content}</li>`;
  394. })
  395. .join("");
  396.  
  397. const dateTime = token && options.lastChecked
  398. ? timeElement(new Date(options.lastChecked).toISOString())
  399. : "Unknown";
  400.  
  401. let error;
  402. if (token) {
  403. error = options.items.length
  404. ? options.error
  405. : "Please add some items to watch";
  406. } else {
  407. error = `
  408. <span class='text-gray-light'>
  409. Please
  410. <a href='https://github.com/settings/tokens/new'>
  411. add a GitHub personal access token
  412. </a>
  413. </span>`;
  414. }
  415.  
  416. $(".ghwr-items", wrap).innerHTML = `
  417. <ul>
  418. <li class="ghwr-header ml-2 text-gray-light">${watcherIcon} GitHub Watcher</li>
  419. <li class="dropdown-divider" role="separator"></li>
  420. ${list.length ? `
  421. <li class="ghwr-list-wrap"><ul>${list}</ul></li>
  422. <li class='dropdown-divider' role='separator'></li>` : ""}
  423. ${!token || showSettings ? `
  424. <li class="ghwr-settings text-gray-light d-flex flex-items-center border-bottom pr-4">
  425. <label class="ml-3 pl-1 pr-2">
  426. GitHub Token: <input type="password" class="form-control input-sm" value="${token || ""}" />
  427. </label>
  428. <label class="pl-4 pr-2" aria-label="Check interval in minutes">
  429. Interval (min):
  430. <input type="number" class="form-control input-sm" value="${options.interval}" min="5" max="9999" step="1" />
  431. </label>
  432. <button type="button" data-type="save-settings" class="btn px-2 ml-2">Save</button>
  433. <button type="button" data-type="cancel-settings" class="btn px-2 ml-2">Cancel</button>
  434. </li>` : ""}
  435. <li class="ghwr-last-checked text-gray-light d-flex flex-items-center flex-justify-between">
  436. <span class="d-flex flex-items-center flex-start">
  437. ${buttonCheck("Clear all watched updates", "all")}
  438. Last checked:&nbsp;${dateTime}
  439. <button type="button" data-type="refresh" class="btn px-2 ml-1 tooltipped tooltipped-e" aria-label="Check again now">
  440. <svg class="octicon octicon-sync" viewBox="0 0 24 24" width="16" height="16" aria-hidden="true">
  441. <path d="M3.38 8A9.502 9.502 0 0112 2.5a9.502 9.502 0 019.215 7.182.75.75 0 101.456-.364C21.473 4.539 17.15 1 12 1a10.995 10.995 0 00-9.5 5.452V4.75a.75.75 0 00-1.5 0V8.5a1 1 0 001 1h3.75a.75.75 0 000-1.5H3.38zm-.595 6.318a.75.75 0 00-1.455.364C2.527 19.461 6.85 23 12 23c4.052 0 7.592-2.191 9.5-5.451v1.701a.75.75 0 001.5 0V15.5a1 1 0 00-1-1h-3.75a.75.75 0 000 1.5h2.37A9.502 9.502 0 0112 21.5c-4.446 0-8.181-3.055-9.215-7.182z"/>
  442. </svg>
  443. </button>
  444. <button type="button" data-type="settings" class="btn px-2 ml-1 tooltipped tooltipped-e" aria-label="Settings">
  445. <svg class="octicon octicon-gear" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true">
  446. <path fill-rule="evenodd" d="M7.429 1.525a6.593 6.593 0 011.142 0c.036.003.108.036.137.146l.289 1.105c.147.56.55.967.997 1.189.174.086.341.183.501.29.417.278.97.423 1.53.27l1.102-.303c.11-.03.175.016.195.046.219.31.41.641.573.989.014.031.022.11-.059.19l-.815.806c-.411.406-.562.957-.53 1.456a4.588 4.588 0 010 .582c-.032.499.119 1.05.53 1.456l.815.806c.08.08.073.159.059.19a6.494 6.494 0 01-.573.99c-.02.029-.086.074-.195.045l-1.103-.303c-.559-.153-1.112-.008-1.529.27-.16.107-.327.204-.5.29-.449.222-.851.628-.998 1.189l-.289 1.105c-.029.11-.101.143-.137.146a6.613 6.613 0 01-1.142 0c-.036-.003-.108-.037-.137-.146l-.289-1.105c-.147-.56-.55-.967-.997-1.189a4.502 4.502 0 01-.501-.29c-.417-.278-.97-.423-1.53-.27l-1.102.303c-.11.03-.175-.016-.195-.046a6.492 6.492 0 01-.573-.989c-.014-.031-.022-.11.059-.19l.815-.806c.411-.406.562-.957.53-1.456a4.587 4.587 0 010-.582c.032-.499-.119-1.05-.53-1.456l-.815-.806c-.08-.08-.073-.159-.059-.19a6.44 6.44 0 01.573-.99c.02-.029.086-.075.195-.045l1.103.303c.559.153 1.112.008 1.529-.27.16-.107.327-.204.5-.29.449-.222.851-.628.998-1.189l.289-1.105c.029-.11.101-.143.137-.146zM8 0c-.236 0-.47.01-.701.03-.743.065-1.29.615-1.458 1.261l-.29 1.106c-.017.066-.078.158-.211.224a5.994 5.994 0 00-.668.386c-.123.082-.233.09-.3.071L3.27 2.776c-.644-.177-1.392.02-1.82.63a7.977 7.977 0 00-.704 1.217c-.315.675-.111 1.422.363 1.891l.815.806c.05.048.098.147.088.294a6.084 6.084 0 000 .772c.01.147-.038.246-.088.294l-.815.806c-.474.469-.678 1.216-.363 1.891.2.428.436.835.704 1.218.428.609 1.176.806 1.82.63l1.103-.303c.066-.019.176-.011.299.071.213.143.436.272.668.386.133.066.194.158.212.224l.289 1.106c.169.646.715 1.196 1.458 1.26a8.094 8.094 0 001.402 0c.743-.064 1.29-.614 1.458-1.26l.29-1.106c.017-.066.078-.158.211-.224a5.98 5.98 0 00.668-.386c.123-.082.233-.09.3-.071l1.102.302c.644.177 1.392-.02 1.82-.63.268-.382.505-.789.704-1.217.315-.675.111-1.422-.364-1.891l-.814-.806c-.05-.048-.098-.147-.088-.294a6.1 6.1 0 000-.772c-.01-.147.039-.246.088-.294l.814-.806c.475-.469.679-1.216.364-1.891a7.992 7.992 0 00-.704-1.218c-.428-.609-1.176-.806-1.82-.63l-1.103.303c-.066.019-.176.011-.299-.071a5.991 5.991 0 00-.668-.386c-.133-.066-.194-.158-.212-.224L10.16 1.29C9.99.645 9.444.095 8.701.031A8.094 8.094 0 008 0zm1.5 8a1.5 1.5 0 11-3 0 1.5 1.5 0 013 0zM11 8a3 3 0 11-6 0 3 3 0 016 0z"></path>
  447. </svg>
  448. </button>
  449. ${error && `<span class="ghwr-api-error ml-2 color-text-danger" role="alert">${error}</span>`}
  450. </span>
  451. <span class="d-flex flex-items-center flex-end">
  452. <a href="https://github.com/Mottie/GitHub-userscripts/wiki/GitHub-watcher" class="px-2 border-0 tooltipped tooltipped-w" aria-label="Click to learn how to use GitHub watcher">
  453. <svg class="octicon" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="14" height="16" viewBox="0 0 14 16">
  454. <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"></path>
  455. </svg>
  456. </a>
  457. <button type="button" data-type="add" class="btn px-2 ml-1 tooltipped tooltipped-w" aria-label="Add new watched item">
  458. <svg class="octicon octicon-plus" viewBox="0 0 16 16" width="16" height="16" aria-hidden="true">
  459. <path fill-rule="evenodd" d="M7.75 2a.75.75 0 01.75.75V7h4.25a.75.75 0 110 1.5H8.5v4.25a.75.75 0 11-1.5 0V8.5H2.75a.75.75 0 010-1.5H7V2.75A.75.75 0 017.75 2z"></path>
  460. </svg>
  461. </button>
  462. </span>
  463. </li>
  464. </ul>`;
  465.  
  466. setStatus();
  467. setFocus();
  468. }
  469.  
  470. const showError = error => {
  471. setLastCheckedTime();
  472. options.error = error.errorMessage || error.message;
  473. setOptions();
  474. }
  475.  
  476. const showStatusIndicator = () => {
  477. // show unread indicator
  478. const indicator = $(".ghwr-item-status", wrap);
  479. const hasError = options.items.some(item => item.error);
  480. indicator.classList.toggle("unread", hasError || getStatus());
  481. indicator.classList.toggle("error", hasError);
  482. };
  483.  
  484. const shouldFetch = () => {
  485. getOptions();
  486. const hasItems = options.items.filter(item => item.url).length > 0;
  487. const needsCheck = !options.lastChecked ||
  488. options.lastChecked + calculatedInterval() < Date.now();
  489. return !!token && hasItems && needsCheck;
  490. };
  491.  
  492. const fetchStatus = () => {
  493. getOptions();
  494. if (shouldFetch()) {
  495. return fetch(
  496. "https://api.github.com/graphql",
  497. {
  498. method: "POST",
  499. headers: {
  500. "Content-Type": "application/json",
  501. "Authorization": `bearer ${token}`
  502. },
  503. body: JSON.stringify({ query: `{${buildQuery()}}` }),
  504. })
  505. .then(res => res.json())
  506. .then(res => {
  507. if (res.message) {
  508. throw new Error(res.message);
  509. }
  510. options.error = "";
  511. return updateOptions(res);
  512. })
  513. .catch(err => showError(err));
  514. }
  515. return Promise.resolve();
  516. };
  517.  
  518. const init = async () => {
  519. getOptions();
  520. const header = $(".Header .notification-indicator");
  521. if (header && !$(".ghwr-header-notification")) {
  522. wrap = make({
  523. el: "div",
  524. className: "Header-item position-relative d-md-flex ghwr-header-notification",
  525. html: `
  526. <details class="details-overlay details-reset ghwr-details">
  527. <summary class="Header-link" aria-label="see watched items" aria-haspopup="menu" role="button">
  528. <span class="ghwr-item-status"></span>
  529. ${watcherIcon}
  530. <span class="dropdown-caret"></span>
  531. </summary>
  532. <details-menu class="dropdown-menu dropdown-menu-sw ghwr-items" role="menu">
  533. <div class="d-flex flex-justify-center py-md-2">
  534. <img src="https://github.githubassets.com/images/spinners/octocat-spinner-32.gif" width="32" alt="">
  535. </div>
  536. </details-menu>
  537. </details>`
  538. });
  539. header.closest(".Header-item")?.before(wrap);
  540. on($(".ghwr-details", wrap), "toggle", () => {
  541. renderStatus();
  542. });
  543. on($(".ghwr-items", wrap), "click", event => {
  544. const { target } = event;
  545. if (target.type === "button") {
  546. event.preventDefault();
  547. handleClick(target.dataset);
  548. } else if (target.type === "text") {
  549. event.preventDefault();
  550. }
  551. });
  552. await fetchStatus();
  553. showStatusIndicator();
  554. }
  555. }
  556.  
  557. function setTimer() {
  558. clearTimeout(timer);
  559. clearInterval(timer2);
  560. timer = setTimeout(async () => {
  561. if (shouldFetch()) {
  562. setLastCheckedTime();
  563. await fetchStatus();
  564. }
  565. renderStatus();
  566. setTimer();
  567. }, calculatedInterval());
  568. timer2 = setInterval(() => {
  569. // Update indicator every 5 minutes because of multiple tabs
  570. getOptions();
  571. showStatusIndicator();
  572. }, calculatedInterval(5));
  573. }
  574.  
  575. getOptions();
  576. init();
  577. setTimer();
  578.  
  579. on(document, "ghmo:container", init);
  580.  
  581. })();