GitHub Toggle Issue Comments

A userscript that toggles issues/pull request comments & messages

目前為 2017-01-12 提交的版本,檢視 最新版本

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