GitHub Toggle Expanders

A userscript that toggles all expanders when one expander is shift-clicked

Versione datata 02/09/2019. Vedi la nuova versione l'ultima versione.

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

You will need to install an extension such as Tampermonkey to install this script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name        GitHub Toggle Expanders
// @version     2.0.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
// ==/UserScript==
(() => {
	"use strict";

	function toggleButton(el, modKey) {
		const stateNode = el.closest(".js-details-container");
		const state = stateNode && (
			// Resolved expander
			stateNode.classList.contains("open") ||
			// compare detail expander
			stateNode.classList.contains("Details--on")
		);
		const parentNode = stateNode && stateNode.closest(modKey
			// shift+ctrl+click = expand all on page
			? ".repository-content"
			// shift+click = expand all in date
			: ".commit-group"
		);
		if (parentNode) {
			const containers = parentNode.querySelectorAll(".js-details-container");
			[...containers].forEach(node => {
				node.classList.toggle("open", state);
				node.classList.toggle("Details--on", state);
			});
		}
	}

	function toggleDetails(el, modKey) {
		const state = el && el.open;
		const parentNode = el && el.closest(modKey
			? "#discussion_bucket" // .js-discussion
			: ".discussion-item-body" // .container?
		);
		if (parentNode) {
			const containers = parentNode.querySelectorAll(
				".outdated-comment, .js-comment-container"
			);
			[...containers].forEach(node => {
				node.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")) {
					toggleButton(target, mod);
				} else if (
					target.matches(".Details-content--closed, .Details-content--open")
				) {
					toggleDetails(target.closest("details"), mod);
				}
			}, 100);
		}
	});

})();