GitHub Toggle Issue Comments

A userscript that toggles issues/pull request comments & messages

当前为 2018-10-06 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name GitHub Toggle Issue Comments
  3. // @version 1.3.4
  4. // @description A userscript that toggles issues/pull request comments & messages
  5. // @license MIT
  6. // @author Rob Garrison
  7. // @namespace https://github.com/Mottie
  8. // @include https://github.com/*
  9. // @run-at document-idle
  10. // @grant GM_addStyle
  11. // @grant GM_getValue
  12. // @grant GM_setValue
  13. // @require https://greasyfork.org/scripts/28721-mutations/code/mutations.js?version=634242
  14. // @icon https://assets-cdn.github.com/pinned-octocat.svg
  15. // ==/UserScript==
  16. (() => {
  17. "use strict";
  18.  
  19. GM_addStyle(`
  20. .ghic-button { float:right; }
  21. .ghic-button .btn:hover div.select-menu-modal-holder { display:block; top:auto; bottom:25px; right:0; }
  22. .ghic-right { position:absolute; right:10px; top:9px; }
  23. .ghic-button .select-menu-header, .ghic-participants { cursor:default; display:block; }
  24. .ghic-participants { border-top:1px solid #484848; padding:15px; }
  25. .ghic-avatar { display:inline-block; float:left; margin: 0 2px 2px 0; cursor:pointer; position:relative; }
  26. .ghic-avatar:last-child { margin-bottom:5px; }
  27. .ghic-avatar.comments-hidden svg { display:block; position:absolute; top:-2px; left:-2px; z-index:1; }
  28. .ghic-avatar.comments-hidden img { opacity:0.5; }
  29. .ghic-button .dropdown-item { font-weight:normal; position:relative; }
  30. .ghic-button .dropdown-item span { font-weight:normal; opacity:.5; }
  31. .ghic-button .dropdown-item.ghic-has-content span { opacity:1; }
  32. .ghic-button .dropdown-item.ghic-checked span { font-weight:bold; }
  33. .ghic-button .dropdown-item.ghic-checked svg,
  34. .ghic-button .dropdown-item:not(.ghic-checked) .ghic-count { display:inline-block; }
  35. .ghic-button .dropdown-item:not(.ghic-checked) { text-decoration:line-through; }
  36. .ghic-button .ghic-count { margin-left:5px; }
  37. .ghic-button .select-menu-modal { margin:0; }
  38. .ghic-button .ghic-participants { margin-bottom:20px; }
  39. /* for testing: ".ghic-hidden { opacity: 0.3; } */
  40. .ghic-hidden, .ghic-hidden-participant, .ghic-avatar svg, .ghic-button .ghic-count,
  41. .ghic-hideReactions .comment-reactions,
  42. .select-menu-header.ghic-active + .select-menu-list .dropdown-item:not(.ghic-has-content) { display:none; }
  43. .ghic-menu-wrapper input[type=checkbox] { height:0; width:0; visibility:hidden; position:absolute; }
  44. .ghic-menu-wrapper .ghic-toggle { cursor:pointer; text-indent:-9999px; width:20px; height:10px;
  45. background:grey; display:block; border-radius:10px; position:relative; }
  46. .ghic-menu-wrapper .ghic-toggle:after { content:''; position:absolute; top:0; left:1px; width:9px;
  47. height:9px; background:#fff; border-radius:9px; transition:.3s; }
  48. .ghic-menu-wrapper input:checked + .ghic-toggle { background:#070; }
  49. .ghic-menu-wrapper input:checked + .ghic-toggle:after { top:0; left:calc(100% - 1px);
  50. transform:translateX(-100%); }
  51. .ghic-menu-wrapper .ghic-toggle:active:after { width:13px; }
  52. .timeline-comment-wrapper.ghic-highlight .comment { border-color:#800 !important; }
  53. `);
  54.  
  55. const regex = /(svg|path)/i,
  56. // ZenHub addon active (include ZenHub Enterprise)
  57. hasZenHub = $(".zhio, .zhe") ? true : false,
  58.  
  59. exceptions = [
  60. "ghsr-sort-block" // sort reactions block (github-sort-reactions.user.js)
  61. ],
  62.  
  63. settings = {
  64. // example: https://github.com/Mottie/Keyboard/issues/448
  65. title: {
  66. isHidden: false,
  67. name: "ghic-title",
  68. selector: ".discussion-item-renamed",
  69. label: "Title Changes"
  70. },
  71. labels: {
  72. isHidden: false,
  73. name: "ghic-labels",
  74. selector: ".discussion-item-labeled, .discussion-item-unlabeled",
  75. label: "Label Changes"
  76. },
  77. state: {
  78. isHidden: false,
  79. name: "ghic-state",
  80. selector: ".discussion-item-reopened, .discussion-item-closed",
  81. label: "State Changes (close/reopen)"
  82. },
  83.  
  84. // example: https://github.com/jquery/jquery/issues/2986
  85. milestone: {
  86. isHidden: false,
  87. name: "ghic-milestone",
  88. selector: ".discussion-item-milestoned",
  89. label: "Milestone Changes"
  90. },
  91. refs: {
  92. isHidden: false,
  93. name: "ghic-refs",
  94. selector: ".discussion-item",
  95. contains: ".discussion-item-ref-title",
  96. label: "References"
  97. },
  98. assigned: {
  99. isHidden: false,
  100. name: "ghic-assigned",
  101. selector: ".discussion-item-assigned",
  102. label: "Assignment Changes"
  103. },
  104.  
  105. // Pull Requests
  106. commits: {
  107. isHidden: false,
  108. name: "ghic-commits",
  109. selector: ".discussion-commits",
  110. label: "Commits"
  111. },
  112. reviews: {
  113. isHidden: false,
  114. name: "ghic-reviews",
  115. selector: ".discussion-item-review, .discussion-item-review_requested",
  116. label: "Reviews (All)"
  117. },
  118. outdated: {
  119. isHidden: false,
  120. name: "ghic-outdated",
  121. selector: ".discussion-item-review",
  122. contains: ".outdated-comment-label",
  123. label: "Reviews (Outdated)"
  124. },
  125. // example: https://github.com/jquery/jquery/pull/3014
  126. diffOld: {
  127. isHidden: false,
  128. name: "ghic-diffOld",
  129. selector: ".outdated-diff-comment-container",
  130. label: "Diff (outdated) Comments"
  131. },
  132. diffNew: {
  133. isHidden: false,
  134. name: "ghic-diffNew",
  135. selector: "[id^=diff-for-comment-]:not(.outdated-diff-comment-container)",
  136. label: "Diff (current) Comments"
  137. },
  138. // example: https://github.com/jquery/jquery/pull/2949
  139. merged: {
  140. isHidden: false,
  141. name: "ghic-merged",
  142. selector: ".discussion-item-merged",
  143. label: "Merged"
  144. },
  145. integrate: {
  146. isHidden: false,
  147. name: "ghic-integrate",
  148. selector: ".discussion-item-integrations-callout",
  149. label: "Integrations"
  150. },
  151.  
  152. // similar comments
  153. similar: {
  154. isHidden: false,
  155. name: "ghic-similar",
  156. selector: `.js-discussion > .Details-element.details-reset,
  157. #js-progressive-timeline-item-container > .Details-element.details-reset`,
  158. label: "Similar comments"
  159. },
  160.  
  161. // extras (special treatment - no selector)
  162. plus1: {
  163. isHidden: false,
  164. name: "ghic-plus1",
  165. label: "+1 Comments"
  166. },
  167. reactions: {
  168. isHidden: false,
  169. name: "ghic-reactions",
  170. label: "Reactions"
  171. },
  172. // page with lots of users to hide:
  173. // https://github.com/isaacs/github/issues/215
  174.  
  175. // ZenHub pipeline change
  176. pipeline: {
  177. isHidden: false,
  178. name: "ghic-pipeline",
  179. selector: ".discussion-item.zh-discussion-item",
  180. label: "ZenHub Pipeline Changes"
  181. }
  182. };
  183.  
  184. const iconHidden = `<svg class="octicon" xmlns="http://www.w3.org/2000/svg" width="30" height="30" viewBox="0 0 9 9"><path fill="#777" d="M7.07 4.5c0-.47-.12-.9-.35-1.3L3.2 6.7c.4.25.84.37 1.3.37.35 0 .68-.07 1-.2.32-.14.6-.32.82-.55.23-.23.4-.5.55-.82.13-.32.2-.65.2-1zM2.3 5.8l3.5-3.52c-.4-.23-.83-.35-1.3-.35-.35 0-.68.07-1 .2-.3.14-.6.32-.82.55-.23.23-.4.5-.55.82-.13.32-.2.65-.2 1 0 .47.12.9.36 1.3zm6.06-1.3c0 .7-.17 1.34-.52 1.94-.34.6-.8 1.05-1.4 1.4-.6.34-1.24.52-1.94.52s-1.34-.18-1.94-.52c-.6-.35-1.05-.8-1.4-1.4C.82 5.84.64 5.2.64 4.5s.18-1.35.52-1.94.8-1.06 1.4-1.4S3.8.64 4.5.64s1.35.17 1.94.52 1.06.8 1.4 1.4c.35.6.52 1.24.52 1.94z"/></svg>`,
  185. plus1Icon = `<img src="https://assets-cdn.github.com/images/icons/emoji/unicode/1f44d.png" class="emoji" title=":+1:" alt=":+1:" height="20" width="20" align="absmiddle">`;
  186.  
  187. function addMenu() {
  188. if ($("#discussion_bucket") && !$(".ghic-button")) {
  189. // update "isHidden" values
  190. getSettings();
  191. let name, isHidden, isChecked,
  192. list = "",
  193. keys = Object.keys(settings),
  194. onlyActive = GM_getValue("onlyActive", false),
  195. header = $(".discussion-sidebar-item:last-child"),
  196. menu = document.createElement("div");
  197.  
  198. for (name of keys) {
  199. if (!(name === "pipeline" && !hasZenHub)) {
  200. isHidden = settings[name].isHidden;
  201. isChecked = isHidden ? "" : "ghic-checked";
  202. list += `<label class="dropdown-item ${isChecked} ${settings[name].name}" data-ghic="${name}">
  203. <span>${settings[name].label} <span class="ghic-count"> </span></span>
  204. <span class="ghic-right">
  205. <input type="checkbox"${isHidden ? "" : " checked"}>
  206. <span class="ghic-toggle"></span>
  207. </span>
  208. </label>`;
  209. }
  210. }
  211.  
  212. menu.className = "ghic-button";
  213. menu.innerHTML = `
  214. <span class="btn btn-sm" role="button" tabindex="0" aria-haspopup="true">
  215. <span class="tooltipped tooltipped-w" aria-label="Toggle issue comments">
  216. <svg class="octicon octicon-comment-discussion" height="16" width="16" role="img" viewBox="0 0 16 16">
  217. <path d="M15 2H6c-0.55 0-1 0.45-1 1v2H1c-0.55 0-1 0.45-1 1v6c0 0.55 0.45 1 1 1h1v3l3-3h4c0.55 0 1-0.45 1-1V10h1l3 3V10h1c0.55 0 1-0.45 1-1V3c0-0.55-0.45-1-1-1zM9 12H4.5l-1.5 1.5v-1.5H1V6h4v3c0 0.55 0.45 1 1 1h3v2z m6-3H13v1.5l-1.5-1.5H6V3h9v6z"></path>
  218. </svg>
  219. </span>
  220. <div class="select-menu-modal-holder ghic-menu-wrapper">
  221. <div class="select-menu-modal" aria-hidden="true">
  222. <div class="select-menu-header ${onlyActive ? "ghic-active" : ""}" tabindex="-1">
  223. <span class="select-menu-title">Toggle items</span>
  224. <label class="ghic-right tooltipped tooltipped-w" aria-label="Only show active items">
  225. <input id="ghic-only-active" type="checkbox" ${onlyActive ? "checked" : ""}>
  226. <span class="ghic-toggle"></span>
  227. </label>
  228. </div>
  229. <div class="select-menu-list ghic-menu" role="menu">
  230. ${list}
  231. <div class="ghic-participants">
  232. <p><strong>Hide Comments from</strong></p>
  233. <div class="ghic-list"></div>
  234. </div>
  235. </div>
  236. </div>
  237. </div>
  238. </span>
  239. `;
  240. if (hasZenHub) {
  241. header.insertBefore(menu, header.childNodes[0]);
  242. } else {
  243. header.appendChild(menu);
  244. }
  245. addAvatars();
  246. }
  247. update();
  248. }
  249.  
  250. function addAvatars() {
  251. let indx = 0,
  252. str = "",
  253. list = $(".ghic-list"),
  254. unique = $$("span.ghic-avatar", list).map(el => el.getAttribute("aria-label")),
  255. // get all avatars
  256. avatars = $$(".timeline-comment-avatar img"),
  257. len = avatars.length - 1, // last avatar is the new comment with the current user
  258. updateAvatars = () => {
  259. list.innerHTML += str;
  260. str = "";
  261. },
  262.  
  263. loop = () => {
  264. let el, name,
  265. max = 0;
  266. while (max < 50 && indx < len) {
  267. if (indx >= len) {
  268. return updateAvatars();
  269. }
  270. el = avatars[indx];
  271. name = (el.getAttribute("alt") || "").replace("@", "");
  272. if (!unique.includes(name)) {
  273. str += `<span class="ghic-avatar tooltipped tooltipped-n" aria-label="${name}">
  274. ${iconHidden}
  275. <img class="ghic-avatar avatar" width="24" height="24" src="${el.src}"/>
  276. </span>`;
  277. unique[unique.length] = name;
  278. max++;
  279. }
  280. indx++;
  281. }
  282. updateAvatars();
  283. if (indx < len) {
  284. setTimeout(() => {
  285. loop();
  286. }, 200);
  287. }
  288. };
  289. loop();
  290. }
  291.  
  292. function getSettings() {
  293. let name,
  294. keys = Object.keys(settings);
  295. for (name of keys) {
  296. settings[name].isHidden = GM_getValue(settings[name].name, false);
  297. }
  298. }
  299.  
  300. function saveSettings() {
  301. let name,
  302. keys = Object.keys(settings);
  303. for (name of keys) {
  304. GM_setValue(settings[name].name, settings[name].isHidden);
  305. }
  306. }
  307.  
  308. function getInputValues() {
  309. let name, item,
  310. keys = Object.keys(settings),
  311. menu = $(".ghic-menu");
  312. for (name of keys) {
  313. if (!(name === "pipeline" && !hasZenHub)) {
  314. item = closest(".dropdown-item", $("." + settings[name].name, menu));
  315. if (item) {
  316. settings[name].isHidden = !$("input", item).checked;
  317. toggleClass(item, "ghic-checked", !settings[name].isHidden);
  318. }
  319. }
  320. }
  321. }
  322.  
  323. function hideStuff(name, init) {
  324. const obj = settings[name],
  325. isHidden = obj.isHidden;
  326. let count, results,
  327. item = $(".ghic-menu .dropdown-item." + obj.name);
  328. if (name === "plus1") {
  329. hidePlus1(init, item);
  330. } else if (item && name === "reactions") {
  331. toggleClass($("body"), "ghic-hideReactions", isHidden);
  332. toggleClass(item, "ghic-has-content", $$(".has-reactions").length - 1 > 0);
  333. // make first comment reactions visible
  334. item = $(".has-reactions", $(".timeline-comment-wrapper"));
  335. if (item) {
  336. item.style.display = "block";
  337. }
  338. } else if (item && obj.selector) {
  339. results = $$(obj.selector);
  340. if (obj.contains) {
  341. results = results.filter(el => {
  342. return !!$(obj.contains, el);
  343. });
  344. }
  345. toggleClass(item, "ghic-checked", !isHidden);
  346. if (isHidden) {
  347. count = addClass(results, "ghic-hidden");
  348. $(".ghic-count", item).textContent = count ? "(" + count + " hidden)" : " ";
  349. } else if (!init) {
  350. // no need to remove classes on initialization
  351. removeClass(results, "ghic-hidden");
  352. }
  353. toggleClass(item, "ghic-has-content", results.length);
  354. }
  355. }
  356.  
  357. function hidePlus1(init, item) {
  358. let max,
  359. indx = 0,
  360. count = 0,
  361. total = 0,
  362. // keep a list of post authors to prevent duplicate +1 counts
  363. authors = [],
  364. // used https://github.com/isaacs/github/issues/215 for matches here...
  365. // matches "+1!!!!", "++1", "+!", "+99!!!", "-1", "+ 100", "thumbs up"; ":+1:^21425235"
  366. // ignoring -1's... add unicode for thumbs up; it gets replaced with an image in Windows
  367. regexPlus = /([?!*,.:^[\]()\'\"+-\d]|bump|thumbs|up|\ud83d\udc4d)/gi,
  368. // other comments to hide - they are still counted towards the +1 counter (for now?)
  369. // seen "^^^" to bump posts; "bump plleeaaassee"; "eta?"; "pretty please"
  370. // "need this"; "right now"; "still nothing?"; "super helpful"; "for gods sake"
  371. regexHide = new RegExp("(" + [
  372. "@\\w+",
  373. "\\b(it|is|a|so|the|and|no|on|oh|do|this|any|very|much|here|just|my|me|too|want|yet|image)\\b",
  374. "pretty",
  375. "pl+e+a+s+e+",
  376. "plz",
  377. "totally",
  378. "y+e+s+",
  379. "eta",
  380. "fix",
  381. "right",
  382. "now",
  383. "hope(ful)?",
  384. "still",
  385. "wait(ed|ing)?",
  386. "nothing",
  387. "really",
  388. "add(ed|ing)?",
  389. "need(ed|ing)?",
  390. "updat(es|ed|ing)?",
  391. "(months|years)\\slater",
  392. "back",
  393. "features?",
  394. "infinity", // +Infinity
  395. "useful",
  396. "super",
  397. "helpful",
  398. "thanks",
  399. "for\\sgod'?s\\ssake",
  400. "c['emon]+" // c'mon, com'on, comeon
  401. ].join("|") + ")", "gi"),
  402. // image title ":{anything}:", etc.
  403. regexEmoji = /(:.*:)|[\u{1f300}-\u{1f64f}\u{1f680}-\u{1f6ff}\u{1f900}-\u{1f9ff}]/gu,
  404. regexWhitespace = /\s+/g,
  405.  
  406. comments = $$(".js-discussion .timeline-comment-wrapper")
  407. .filter(comment => {
  408. const classes = comment.className.split(" ");
  409. return !exceptions.some(ex => classes.includes(ex));
  410. }),
  411. len = comments.length,
  412.  
  413. loop = () => {
  414. let wrapper, el, tmp, txt, img, hasLink, dupe;
  415. max = 0;
  416. while (max < 20 && indx < len) {
  417. if (indx >= len) {
  418. if (init) {
  419. item.classList.toggle("ghic-has-content", count > 0);
  420. }
  421. return;
  422. }
  423. wrapper = comments[indx];
  424. // save author list to prevent repeat +1s
  425. el = $(".timeline-comment-header .author", wrapper);
  426. txt = (el ? el.textContent || "" : "").toLowerCase();
  427. dupe = true;
  428. if (txt && authors.indexOf(txt) < 0) {
  429. authors[authors.length] = txt;
  430. dupe = false;
  431. }
  432. el = $(".comment-body", wrapper);
  433. // ignore quoted messages, but get all fragments
  434. tmp = $$(".email-fragment", el);
  435. // some posts only contain a link to related issues; these should not be counted as a +1
  436. // see https://github.com/isaacs/github/issues/618#issuecomment-200869630
  437. hasLink = $$(tmp.length ? ".email-fragment .issue-link" : ".issue-link", el).length;
  438. if (tmp.length) {
  439. // ignore quoted messages
  440. txt = getAllText(tmp);
  441. } else {
  442. txt = (el ? el.textContent || "" : "").trim();
  443. }
  444. if (!txt) {
  445. img = $("img", el);
  446. if (img) {
  447. txt = img.getAttribute("title") || img.getAttribute("alt");
  448. }
  449. }
  450. // remove fluff
  451. txt = (txt || "")
  452. .replace(regexEmoji, "")
  453. .replace(regexHide, "")
  454. .replace(regexPlus, "")
  455. .replace(regexWhitespace, " ")
  456. .trim();
  457. if (txt === "" || (txt.length <= 4 && !hasLink)) {
  458. if (init && !settings.plus1.isHidden) {
  459. // +1 Comments has-content
  460. item.classList.toggle("ghic-has-content", true);
  461. return;
  462. }
  463. if (settings.plus1.isHidden) {
  464. wrapper.classList.add("ghic-hidden", "ghic-highlight");
  465. total++;
  466. // one +1 per author
  467. if (!dupe) {
  468. count++;
  469. }
  470. } else if (!init) {
  471. wrapper.classList.remove("ghic-hidden");
  472. }
  473. max++;
  474. }
  475. indx++;
  476. }
  477. if (indx < len) {
  478. setTimeout(() => {
  479. window.requestAnimationFrame(loop);
  480. }, 200);
  481. } else {
  482. if (init) {
  483. item.classList.toggle("ghic-has-content", count > 0);
  484. }
  485. $(".ghic-menu .ghic-plus1 .ghic-count").textContent = total ? "(" + total + " hidden)" : " ";
  486. addCountToReaction(count);
  487. }
  488. };
  489. loop();
  490. }
  491.  
  492. function getAllText(el) {
  493. let txt = "",
  494. indx = el.length;
  495. // text order doesn't matter
  496. while (indx--) {
  497. txt += el[indx].textContent.trim();
  498. }
  499. return txt;
  500. }
  501.  
  502. function addCountToReaction(count) {
  503. if (!count) {
  504. count = ($(".ghic-menu .ghic-plus1 .ghic-count").textContent || "")
  505. .replace(/[()]/g, "")
  506. .trim();
  507. }
  508. let comment = $(".timeline-comment"),
  509. tmp = $(
  510. ".has-reactions button[value='+1 react'], .has-reactions button[value='+1 unreact']",
  511. comment
  512. ),
  513. el = $(".ghic-count", comment);
  514. if (el) {
  515. // the count may have been appended to the comment & now
  516. // there is a reaction, so remove any "ghic-count" elements
  517. el.parentNode.removeChild(el);
  518. }
  519. if (count) {
  520. if (tmp) {
  521. el = document.createElement("span");
  522. el.className = "ghic-count";
  523. el.textContent = count ? " + " + count + " (from hidden comments)" : "";
  524. tmp.appendChild(el);
  525. } else {
  526. el = document.createElement("p");
  527. el.className = "ghic-count";
  528. el.innerHTML = "<hr>" + plus1Icon + " " + count + " (from hidden comments)";
  529. $(".comment-body", comment).appendChild(el);
  530. }
  531. }
  532. }
  533.  
  534. function hideParticipant(el) {
  535. if (el) {
  536. el.classList.toggle("comments-hidden");
  537. let name = el.getAttribute("aria-label"),
  538. results = $$(
  539. ".timeline-comment-wrapper, .commit-comment, .discussion-item"
  540. ).filter(el => {
  541. const author = $(".js-discussion .author", el);
  542. return author ? name === author.textContent.trim() : false;
  543. });
  544. // use a different participant class name to hide timeline events
  545. // or unselecting all users will show everything
  546. if (el.classList.contains("comments-hidden")) {
  547. addClass(results, "ghic-hidden-participant");
  548. } else {
  549. removeClass(results, "ghic-hidden-participant");
  550. }
  551. results = [];
  552. }
  553. }
  554.  
  555. function update() {
  556. if ($("#discussion_bucket") && $(".ghic-button")) {
  557. let keys = Object.keys(settings),
  558. indx = keys.length;
  559. while (indx--) {
  560. // true flag for init - no need to remove classes
  561. hideStuff(keys[indx], true);
  562. }
  563. addAvatars();
  564. }
  565. }
  566.  
  567. function checkItem(event) {
  568. if (document.getElementById("discussion_bucket")) {
  569. let name,
  570. target = event.target,
  571. wrap = target && target.closest(".dropdown-item, .ghic-participants");
  572. if (target && wrap) {
  573. if (target.nodeName === "INPUT") {
  574. getInputValues();
  575. saveSettings();
  576. name = wrap.dataset.ghic;
  577. if (name) {
  578. hideStuff(name);
  579. }
  580. } else if (target.classList.contains("ghic-avatar")) {
  581. // make sure we're targeting the span wrapping the image
  582. hideParticipant(target.nodeName === "IMG" ? target.parentNode : target);
  583. } else if (regex.test(target.nodeName)) {
  584. // clicking on the SVG may target the svg or path inside
  585. hideParticipant(closest(".ghic-avatar", target));
  586. }
  587. } else if (target.id === "ghic-only-active") {
  588. closest(".select-menu-header", target).classList.toggle("ghic-active", target.checked);
  589. GM_setValue("onlyActive", target.checked);
  590. }
  591. // Make button show if it is active
  592. target = $(".ghic-button .btn");
  593. if (target) {
  594. const active = $$(".ghic-hidden, .ghic-hidden-participant").length > 0;
  595. target.classList.toggle("btn-outline", active);
  596. }
  597. }
  598. }
  599.  
  600. function $(selector, el) {
  601. return (el || document).querySelector(selector);
  602. }
  603.  
  604. function $$(selector, el) {
  605. return Array.from((el || document).querySelectorAll(selector));
  606. }
  607.  
  608. function closest(selector, el) {
  609. while (el && el.nodeType === 1) {
  610. if (el.matches(selector)) {
  611. return el;
  612. }
  613. el = el.parentNode;
  614. }
  615. return null;
  616. }
  617.  
  618. function addClass(els, name) {
  619. let indx,
  620. len = els.length;
  621. for (indx = 0; indx < len; indx++) {
  622. els[indx].classList.add(name);
  623. }
  624. return len;
  625. }
  626.  
  627. function removeClass(els, name) {
  628. let indx,
  629. len = els.length;
  630. for (indx = 0; indx < len; indx++) {
  631. els[indx].classList.remove(name);
  632. }
  633. }
  634.  
  635. function toggleClass(els, name, flag) {
  636. els = Array.isArray(els) ? els : [els];
  637. let el,
  638. indx = els.length;
  639. while (indx--) {
  640. el = els[indx];
  641. if (el) {
  642. if (typeof flag === "undefined") {
  643. flag = !el.classList.contains(name);
  644. }
  645. if (flag) {
  646. el.classList.add(name);
  647. } else {
  648. el.classList.remove(name);
  649. }
  650. }
  651. }
  652. }
  653.  
  654. function init() {
  655. getSettings();
  656. addMenu();
  657. $("body").addEventListener("click", checkItem);
  658. update();
  659. }
  660.  
  661. // update list when content changes
  662. document.addEventListener("ghmo:container", addMenu);
  663. document.addEventListener("ghmo:comments", update);
  664. init();
  665.  
  666. })();