GitHub Toggle Issue Comments

A userscript that toggles issues/pull request comments & messages

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

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