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

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址