GitHub Toggle Issue Comments

A userscript that toggles issues/pull request comments & messages

当前为 2018-09-24 提交的版本,查看 最新版本

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