Last active
August 1, 2017 10:45
-
-
Save mdziekon/a71c46091b716d57136791fe22672f7e to your computer and use it in GitHub Desktop.
Simple & hackish "Expand all diffs" button for Github PRs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==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