Skip to content

Instantly share code, notes, and snippets.

@Jaace
Last active February 13, 2018 15:56
Show Gist options
  • Save Jaace/9c4cfb812f70d41b44c9e88659c70086 to your computer and use it in GitHub Desktop.
Save Jaace/9c4cfb812f70d41b44c9e88659c70086 to your computer and use it in GitHub Desktop.
Expand/Collapse button on Github PRs
// Add a button to Collapse or Expand comments in a GitHub PR.
//
// Install Tampermonkey and add this as a script
// ==UserScript==
// @name Github PRs: Expand / Collapse Comments.
// @namespace TBD
// @version 0.1
// @description Add a button to expand or collapse comments in a GitHub PR.
// @author Jason Boyle
// @match https://github.com/*
// @grant GM_addStyle
// ==/UserScript==
GM_addStyle(
'.pr-expand-collapse-btn { margin: 0 0 0 6px; position: absolute; right: 10px; top: 4px; } ' +
'.collapsed { display: none; }'
);
window.addEventListener('load', () => {
initializeButtons();
});
function initializeButtons() {
const comments = document.querySelectorAll('.file.js-comment-container');
for (let comment of comments) {
const actions = comment.querySelector('.file-header');
const diffTable = comment.querySelector('.blob-wrapper');
const reviewComments = comment.querySelector('.review-comments');
const expandCollapseBtn = document.createElement('a');
expandCollapseBtn.classList.add('pr-expand-collapse-btn', 'btn', 'btn-sm');
expandCollapseBtn.innerHTML = 'Collapse';
actions.appendChild(expandCollapseBtn);
expandCollapseBtn.onclick = function() {
if ('Collapse' === expandCollapseBtn.innerHTML) {
expandCollapseBtn.innerHTML = 'Expand';
} else {
expandCollapseBtn.innerHTML = 'Collapse';
}
diffTable.classList.toggle('collapsed');
reviewComments.classList.toggle('collapsed');
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment