Skip to content

Instantly share code, notes, and snippets.

@mdziekon
Last active August 1, 2017 10:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdziekon/a71c46091b716d57136791fe22672f7e to your computer and use it in GitHub Desktop.
Save mdziekon/a71c46091b716d57136791fe22672f7e to your computer and use it in GitHub Desktop.
Simple & hackish "Expand all diffs" button for Github PRs
// ==UserScript==
// @name Github Expand All Button
// @namespace mdziekon_github_expandall
// @include /http(s)*://(.*?)github.com/(.*?)/pull/(.*?)/
// @version 1
// @grant none
// ==/UserScript==
let evtListener;
function onExpandAllClicked() {
const event = new MouseEvent(
"click",
{
view: window,
bubbles: true,
cancelable: true
}
);
const $expandAllButton = document.querySelector("#expand-all-diffs-container button");
$expandAllButton.textContent = "(All diffs expanded)";
$expandAllButton.removeEventListener("click", evtListener);
const $expandButtons = document.querySelectorAll(".load-diff-button");
$expandButtons.forEach((node) => node.dispatchEvent(event));
}
function injectButton() {
const $codeStats = document.querySelector(".diffbar-item.diffstat");
if ($codeStats === null) {
return;
}
const $expandAllContainer = document.createElement("div");
$expandAllContainer.className = "diffbar-item";
$expandAllContainer.id = "expand-all-diffs-container";
const $expandAllButton = document.createElement("button");
const $buttonText = document.createElement("strong");
$buttonText.appendChild(document.createTextNode("Expand all diffs"));
$expandAllButton.appendChild($buttonText);
$expandAllButton.className = "btn-link muted-link";
evtListener = $expandAllButton.addEventListener("click", onExpandAllClicked);
$expandAllContainer.appendChild($expandAllButton);
$codeStats.parentNode.insertBefore($expandAllContainer, $codeStats.nextSibling);
}
function hasExpandAllButton() {
if (document.querySelector("#expand-all-diffs-container") !== null) {
return;
}
injectButton();
}
setInterval(hasExpandAllButton, 2000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment