// ==UserScript==
// @name GitHub Toggle Issue Comments
// @version 1.0.13
// @description A userscript that toggles issues/pull request comments & messages
// @license https://creativecommons.org/licenses/by-sa/4.0/
// @namespace http://github.com/Mottie
// @include https://github.com/*
// @run-at document-idle
// @grant GM_addStyle
// @grant GM_getValue
// @grant GM_setValue
// @author Rob Garrison
// @downloadURL none
// ==/UserScript==
/* global GM_addStyle, GM_getValue, GM_setValue */
/*jshint unused:true, esnext:true */
(function() {
"use strict";
GM_addStyle(`
.ghic-button { float:right; }
.ghic-button .btn:hover div.select-menu-modal-holder { display:block; top:auto; bottom:25px; right:0; }
.ghic-right { float:right; }
/* pre-wrap set for Firefox; see https://greasyfork.org/en/forum/discussion/9166/x */
.ghic-menu label { display:block; padding:5px 15px; white-space:pre-wrap; }
.ghic-button .select-menu-header, .ghic-participants { cursor:default; }
.ghic-participants { border-top:1px solid #484848; padding:15px; }
.ghic-avatar { display:inline-block; float:left; margin: 0 2px 2px 0; cursor:pointer; position:relative; }
.ghic-avatar:last-child { margin-bottom:5px; }
.ghic-avatar.comments-hidden svg { display:block; position:absolute; top:-2px; left:-2px; z-index:1; }
.ghic-avatar.comments-hidden img { opacity:0.5; }
.ghic-button .dropdown-item span { font-weight:normal; opacity:.5; }
.ghic-button .dropdown-item.ghic-has-content span { opacity:1; }
.ghic-button .dropdown-item.ghic-checked span { font-weight:bold; }
.ghic-button .dropdown-item.ghic-checked svg,
.ghic-button .dropdown-item.ghic-checked .ghic-count { display:inline-block; }
.ghic-button .ghic-count { float:left; margin-right:5px; }
.ghic-button .select-menu-modal { margin:0; }
.ghic-button .ghic-participants { margin-bottom:20px; }
/* for testing: ".ghic-hidden { opacity: 0.3; } */
.ghic-hidden, .ghic-hidden-participant, .ghic-avatar svg, .ghic-button .ghic-right > *,
.ghic-hideReactions .comment-reactions { display:none; }
`);
let targets,
busy = false,
// ZenHub addon active (include ZenHub Enterprise)
hasZenHub = $(".zhio, .zhe") ? true : false;
const regex = /(svg|path)/i,
settings = {
// example: https://github.com/Mottie/Keyboard/issues/448
title: {
isHidden: false,
name: "ghic-title",
selector: ".discussion-item-renamed",
label: "Title Changes"
},
labels: {
isHidden: false,
name: "ghic-labels",
selector: ".discussion-item-labeled, .discussion-item-unlabeled",
label: "Label Changes"
},
state: {
isHidden: false,
name: "ghic-state",
selector: ".discussion-item-reopened, .discussion-item-closed",
label: "State Changes (close/reopen)"
},
// example: https://github.com/jquery/jquery/issues/2986
milestone: {
isHidden: false,
name: "ghic-milestone",
selector: ".discussion-item-milestoned",
label: "Milestone Changes"
},
refs: {
isHidden: false,
name: "ghic-refs",
selector: ".discussion-item-ref, .discussion-item-head_ref_deleted",
label: "References"
},
assigned: {
isHidden: false,
name: "ghic-assigned",
selector: ".discussion-item-assigned",
label: "Assignment Changes"
},
// Pull Requests
commits: {
isHidden: false,
name: "ghic-commits",
selector: ".discussion-commits",
label: "Commits"
},
// example: https://github.com/jquery/jquery/pull/3014
diffOld: {
isHidden: false,
name: "ghic-diffOld",
selector: ".outdated-diff-comment-container",
label: "Diff (outdated) Comments"
},
diffNew: {
isHidden: false,
name: "ghic-diffNew",
selector: "[id^=diff-for-comment-]:not(.outdated-diff-comment-container)",
label: "Diff (current) Comments"
},
// example: https://github.com/jquery/jquery/pull/2949
merged: {
isHidden: false,
name: "ghic-merged",
selector: ".discussion-item-merged",
label: "Merged"
},
integrate: {
isHidden: false,
name: "ghic-integrate",
selector: ".discussion-item-integrations-callout",
label: "Integrations"
},
// extras (special treatment - no selector)
plus1: {
isHidden: false,
name: "ghic-plus1",
label: "Hide +1s"
},
reactions: {
isHidden: false,
name: "ghic-reactions",
label: "Reactions"
},
// page with lots of users to hide:
// https://github.com/isaacs/github/issues/215
// ZenHub pipeline change
pipeline: {
isHidden: false,
name: "ghic-pipeline",
selector: ".discussion-item.zh-discussion-item",
label: "ZenHub Pipeline Changes"
}
};
const iconHidden = ``,
iconCheck = ``,
plus1Icon = ``;
function $(selector, el) {
return (el || document).querySelector(selector);
}
function $$(selector, el) {
return Array.from((el || document).querySelectorAll(selector));
}
function closest(el, selector) {
while (el && !el.matches(selector)) {
el = el.parentNode;
}
return el && el.matches(selector) ? el : null;
}
function addClass(els, name) {
let indx,
len = els.length;
for (indx = 0; indx < len; indx++) {
els[indx].classList.add(name);
}
return len;
}
function removeClass(els, name) {
let indx,
len = els.length;
for (indx = 0; indx < len; indx++) {
els[indx].classList.remove(name);
}
}
function toggleClass(els, name, flag) {
els = Array.isArray(els) ? els : [els];
let el,
indx = els.length;
while (indx--) {
el = els[indx];
if (el) {
if (typeof flag === "undefined") {
flag = !el.classList.contains(name);
}
if (flag) {
el.classList.add(name);
} else {
el.classList.remove(name);
}
}
}
}
function addMenu() {
busy = true;
if ($("#discussion_bucket") && !$(".ghic-button")) {
// update "isHidden" values
getSettings();
let name, bright, isHidden, isChecked,
list = "",
keys = Object.keys(settings),
header = $(".discussion-sidebar-item:last-child"),
menu = document.createElement("div");
for (name of keys) {
if (!(name === "pipeline" && !hasZenHub)) {
// make plus1 and reactions list items always bright
bright = name === "plus1" ? " ghic-has-content" : "";
isHidden = settings[name].isHidden;
isChecked = isHidden ? " ghic-checked": "";
// not using multi-line backticks because it adds lots of white-space to the label
list += ``;
}
}
menu.className = "ghic-button";
menu.innerHTML = `
`;
if (hasZenHub) {
header.insertBefore(menu, header.childNodes[0]);
} else {
header.appendChild(menu);
}
addAvatars();
}
update();
busy = false;
}
function addAvatars() {
let indx = 0,
str = "