GitHub Toggle Issue Comments

A userscript that toggles issues/pull request comments & messages

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

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