Skip to content

Instantly share code, notes, and snippets.

@71
Last active September 23, 2025 13:34
Show Gist options
  • Select an option

  • Save 71/80a51cffe04425a7efe11656dbc53459 to your computer and use it in GitHub Desktop.

Select an option

Save 71/80a51cffe04425a7efe11656dbc53459 to your computer and use it in GitHub Desktop.

Move .h files before .cpp files in GitHub PR reviews (https://github.com/71/repo/pull/1/files).

javascript:(function()%7B(()%20%3D%3E%20%7B%0A%20%20const%20showBefore%20%3D%20%5B%0A%20%20%20%20%5B%2F%5C.(h%7Chh%7Chpp%7Ch%5C%2B%5C%2B)%24%2F%2C%20%2F%5C.(c%7Ccc%7Ccpp%7Cc%5C%2B%5C%2B)%24%2F%5D%2C%0A%20%20%5D%3B%0A%20%20const%20maybeMoveBefore%20%3D%20(li1%2C%20li2%2C%20getPath)%20%3D%3E%20%7B%0A%20%20%20%20let%20path1%2C%20path2%2C%20base1%2C%20base2%3B%0A%20%20%20%20if%20(!(path1%20%3D%20getPath(li1))%20%7C%7C%20!(path2%20%3D%20getPath(li2)))%20return%3B%0A%0A%20%20%20%20for%20(const%20%5Bpattern1%2C%20pattern2%5D%20of%20showBefore)%20%7B%0A%20%20%20%20%20%20if%20((base1%20%3D%20path1.replace(pattern1%2C%20%22%22))%20!%3D%3D%20path1%20%26%26%0A%20%20%20%20%20%20%20%20%20%20(base2%20%3D%20path2.replace(pattern2%2C%20%22%22))%20!%3D%3D%20path2%20%26%26%0A%20%20%20%20%20%20%20%20%20%20base1%20%3D%3D%3D%20base2)%20%7B%0A%20%20%20%20%20%20%20%20return%20li2.before(li1)%3B%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%7D%3B%0A%0A%20%20for%20(const%20el%20of%20document.querySelectorAll(%22%5Bid%5E%3D'diff-'%5D%5Bclass%5E%3D'Diff-module'%5D%22))%20%7B%0A%20%20%20%20const%20li1%20%3D%20el.parentElement%3B%0A%20%20%20%20const%20li2%20%3D%20li1.previousElementSibling%3B%0A%0A%20%20%20%20maybeMoveBefore(li1%2C%20li2%2C%20(li)%20%3D%3E%20li%3F.querySelector(%22a%20%3E%20code%22).textContent.replaceAll(%2F%5E%5Cp%7BCf%7D%7C%5Cp%7BCf%7D%24%2Fug%2C%20%22%22))%3B%0A%20%20%7D%0A%0A%20%20for%20(const%20li1%20of%20document.querySelectorAll(%22%23pr-file-tree%20li%3Anot(%5Baria-expanded%5D)%22))%20%7B%0A%20%20%20%20const%20li2%20%3D%20li1.previousElementSibling%3B%0A%0A%20%20%20%20maybeMoveBefore(li1%2C%20li2%2C%20(li)%20%3D%3E%20li%3F.id)%3B%0A%20%20%7D%0A%7D)()%7D)()%3B

This works on the newer GitHub UI (as of 2025-08-20).

Usage

  1. Copy the javascript: snippet
  2. Right-click on the Bookmarks Bar
  3. Select "Add Page…"
  4. Pick a name, then paste the javascript: snippet into "URL"

Source

(() => {
  const showBefore = [
    [/\.(h|hh|hpp|h\+\+)$/, /\.(c|cc|cpp|c\+\+)$/],
  ];
  const maybeMoveBefore = (li1, li2, getPath) => {
    let path1, path2, base1, base2;
    if (!(path1 = getPath(li1)) || !(path2 = getPath(li2))) return;

    for (const [pattern1, pattern2] of showBefore) {
      if ((base1 = path1.replace(pattern1, "")) !== path1 &&
          (base2 = path2.replace(pattern2, "")) !== path2 &&
          base1 === base2) {
        return li2.before(li1);
      }
    }
  };

  for (const el of document.querySelectorAll("[id^='diff-'][class^='Diff-module']")) {
    const li1 = el.parentElement;
    const li2 = li1.previousElementSibling;

    maybeMoveBefore(li1, li2, (li) => li?.querySelector("a > code").textContent.replaceAll(/^\p{Cf}|\p{Cf}$/ug, ""));
  }

  for (const li1 of document.querySelectorAll("#pr-file-tree li:not([aria-expanded])")) {
    const li2 = li1.previousElementSibling;

    maybeMoveBefore(li1, li2, (li) => li?.id);
  }
})();

Transformed to a bookmarklet with Bookmarklet Maker.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment