virzen / Toggle all difs

// ==UserScript==
// @name         Toggle all difs
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       virzen
// @match        https://github.com/*/pull/*/files*
// @grant        none
// @license MIT
// ==/UserScript==

// ==OpenUserJS==
// @author virzen
// @copyright 2019, virzen (https://openuserjs.org/users/virzen)
// ==/OpenUserJS==

const getAllOpenDetails = () => Array.from(document.querySelectorAll('.Details--on'))
const getAllClosedDetails = () => Array.from(document.querySelectorAll('.Details:not(.Details--on)'))

const toggleDetails = (details) => {
    const toggle = details.querySelector('[aria-label="Toggle diff contents"]');
    if (toggle) toggle.click();
}

const closeAllOpenDiffs = () => {
  getAllOpenDetails().forEach(toggleDetails)
}

const openAllClosedDiffs = () => {
  getAllClosedDetails().forEach(toggleDetails)
}

const createButton = ({ text, onClick }) => {
    const button = document.createElement('button')

    button.onclick = onClick;
    button.textContent = text;

    button.classList.add('btn', 'btn-sm', 'diffbar-item');

    return button;
}

const createCloseButton = () => {
    return createButton({ text: 'Close all open diffs', onClick: closeAllOpenDiffs });
}

const createOpenButton = () => {
    return createButton({ text: 'Open all closed diffs', onClick: openAllClosedDiffs });
}

const insertIntoDocument = (button) => {
    const parent = document.querySelector('.pr-review-tools');
    const child = parent.querySelector('.js-reviews-container');
    parent.insertBefore(button, child);
}

const init = () => {
    const closeButton = createCloseButton();
    const openButton = createOpenButton();

    insertIntoDocument(closeButton);
    insertIntoDocument(openButton);
}


(function() {
    'use strict';

    init();

    // Your code here...
})();