Skip to content

Instantly share code, notes, and snippets.

@bradcupit
Last active June 19, 2023 18:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bradcupit/3880c34245a465a0fe82e869a168e61b to your computer and use it in GitHub Desktop.
Save bradcupit/3880c34245a465a0fe82e869a168e61b to your computer and use it in GitHub Desktop.
Bookmarklet to expand all "Show resolved" and "Show hidden" and "Load More" links in a GitHub PR conversation
javascript:(() => {
const DEFAULT_NUM_TRIES_AFTER_DONE = 5;
const MILLIS_TO_SLEEP = 500;
const MAX_ITERATIONS = 50;
const MAX_PARENTS_TO_CHECK = 7;
/* Originally taken from https://github.com/broadinstitute/gatk/wiki/Expand-outdated-Github-Comments */
function loadAll(numIterations, triesRemainingAfterDone) {
let showButtonArray = Array.from(document.getElementsByClassName('Details-content--closed')).filter(element => !parentIsOpen(element));
let loadMoreButtonArray = Array.from(document.getElementsByClassName('ajax-pagination-btn'));
let foundSome = showButtonArray.length > 0 || loadMoreButtonArray.length > 0;
showButtonArray.forEach(showButton => showButton.click());
loadMoreButtonArray.forEach(loadMoreButton => loadMoreButton.click());
scheduledNext(foundSome, numIterations, triesRemainingAfterDone);
}
function scheduledNext(foundSome, numIterations, triesRemainingAfterDone) {
if (numIterations > MAX_ITERATIONS) {
console.log(`stopped loading github comments to prevent infinite loop. numIterations = ${numIterations}`);
return;
}
if (foundSome) {
setTimeout(() => loadAll(numIterations + 1, DEFAULT_NUM_TRIES_AFTER_DONE), MILLIS_TO_SLEEP);
} else if (triesRemainingAfterDone > 0) {
setTimeout(() => loadAll(numIterations + 1, triesRemainingAfterDone - 1), MILLIS_TO_SLEEP);
} else {
done();
}
}
function parentIsOpen(element) {
let currentParent = element.parentNode;
for (let i = 0; i < MAX_PARENTS_TO_CHECK; i++) {
if (currentParent === null || currentParent === undefined) {
return false;
}
if (currentParent.hasAttribute('open')) {
return true;
}
currentParent = currentParent.parentNode;
}
}
/* inspired from this: https://stackoverflow.com/a/15466856/152061 */
function done() {
let div = document.createElement('div');
div.setAttribute('style', 'position:fixed; top:15%; left:1%; background-color:white; color:black display:inline-block; border-style:solid; border-color:black;');
div.innerHTML = 'finished expanding comments';
document.body.appendChild(div);
setTimeout(() => div.parentNode.removeChild(div), 2500);
console.log('done');
}
loadAll(0, DEFAULT_NUM_TRIES_AFTER_DONE);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment