GitHub Toggle Issue Comments

A userscript that toggles issues/pull request comments & messages

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

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