Github Load All Comments

Automatically load all Github comments on page load

  1. // ==UserScript==
  2. // @name Github Load All Comments
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description Automatically load all Github comments on page load
  6. // @author Aaron1011
  7. // @match https://github.com/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. // Retrieve the form associated with the "X Hidden Items\nLoad More" button
  15. const form = document.querySelector('form.ajax-pagination-form');
  16. if (form) {
  17. // The first button, which has the text "X hidden items".
  18. const button = form.querySelector('button');
  19. if (button) {
  20. // Extract the number of hidden items from the button text
  21. const numberString = button.textContent.trim().split(' ')[0];
  22. if (numberString) {
  23. // Construct the new URL. This is undocumented, and may break at any time.
  24. // As of 12/08/2019, URLS look like this:
  25. // "/_render_node/MDExOlB1bGxSZXF1ZXN0MzMyNTc0Mjgz/timeline/more_items?variables%5Bafter%5D=Y3Vyc29yOnYyOpPPAAABbhuxRigCqjEwMTM5OTEwNjI%3D&variables%5Bbefore%5D=Y3Vyc29yOnYyOpPPAAABbzDoAogAqTU2ODM0NTgzNA%3D%3D&variables%5Bfirst%5D=60&variables%5BhasFocusedReviewComment%5D=false&variables%5BhasFocusedReviewThread%5D=false"
  26. // The key "variables[first]" in the URL query parameters appears to control
  27. // how many additional comments are fetched.
  28. // Github appears to always set this to 60, but it can be increased up to the
  29. // total number of hidden items.
  30. // By setting "variables[first]" to total number of hidden items (extracted from the button text),
  31. // we can fetch all hidden comments at once
  32. const url = new URL('https://github.com' + (form.getAttribute('action').toString() || ''));
  33. url.searchParams.set('variables[first]', numberString);
  34. form.setAttribute('action', url.toString());
  35.  
  36. // Trigger a button click, causing the page to fetch and display
  37. // the hidden comments.
  38. // For some reason, trying to do this immediately (without setTimeout)
  39. // causes the browser to navigate to the actual URL (e.g. https://github.com//_render_node/...)
  40. // instead of triggering the proper Github event handler.
  41. // It seems likely that the event handler isn't yet registered when this function runs.
  42. // Delaying the button clock with setTimeout() appears to cause the event
  43. // handler to be consistently triggered, resulting in the desired behavior
  44. setTimeout(() => button.click(), 0);
  45. }
  46. }
  47. }
  48. })();