forked from Mottie/GitHub-userscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
github-toggle-expanders.user.js
56 lines (52 loc) · 1.76 KB
/
github-toggle-expanders.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// ==UserScript==
// @name GitHub Toggle Expanders
// @version 1.2.0
// @description A userscript that toggles all expanders when one expander is shift-clicked
// @license MIT
// @author Rob Garrison
// @namespace https://github.com/Mottie
// @include https://github.com/*
// @run-at document-idle
// @icon https://github.githubassets.com/pinned-octocat.svg
// @updateURL https://raw.githubusercontent.com/Mottie/GitHub-userscripts/master/github-toggle-expanders.user.js
// @downloadURL https://raw.githubusercontent.com/Mottie/GitHub-userscripts/master/github-toggle-expanders.user.js
// ==/UserScript==
(() => {
"use strict";
function toggle(el, modKey) {
const stateNode = el.closest(".js-details-container, details");
const state = stateNode.nodeName === "DETAILS" ?
stateNode.open :
stateNode.classList.contains("open");
const parentNode = stateNode.closest(modKey ?
".container, .js-discussion" :
".commit-group, .js-timeline-item"
);
const containers = parentNode.querySelectorAll(
".js-details-container, .outdated-comment"
);
[...containers].forEach(node => {
if (node.nodeName === "DETAILS") {
node.open = state;
} else {
node.classList.toggle("open", state);
}
});
}
document.body.addEventListener("click", event => {
const target = event.target;
const mod = event.ctrlKey
|| event.metaKey
|| window.location.pathname.includes("/compare/");
if (target && event.getModifierState("Shift")) {
// give GitHub time to update the elements
setTimeout(() => {
if (target.matches(".js-details-target")) {
toggle(target, mod);
} else if (target.matches(".btn-link, .js-toggle-outdated-comments")) {
toggle(target.closest("details"), mod);
}
}, 100);
}
});
})();