Skip to content

Instantly share code, notes, and snippets.

@hayleyxyz
Last active April 16, 2022 13:21
Show Gist options
  • Save hayleyxyz/3c76973052b85b5f2e0f44a0c2881290 to your computer and use it in GitHub Desktop.
Save hayleyxyz/3c76973052b85b5f2e0f44a0c2881290 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name GitHub "Earliest Commit"
// @version 1
// @grant none
// @match https://github.com/*
// ==/UserScript==
const exec = () => {
const exists = (...sel) => {
return sel.every((s) => document.querySelectorAll(s).length > 0);
};
if (!exists('.js-details-container strong', '.js-permalink-shortcut', '.js-permalink-shortcut', '#branch-select-menu')) {
console.log('Exiting GitHub "Earliest Commit"');
return;
}
const getEarliestCommitLogUrl = () => {
const commits = parseInt(document.querySelector('.js-details-container strong').textContent.match(/[\d,]+/).pop().replace(/,/, ''))
const hash = document.querySelector('.js-permalink-shortcut').href.match(/[a-f0-9]{40}$/).pop();
const url = window.location.pathname.match(/^\/[\w-\.]+\/[\w-\.]+/).pop() + `/commits/master?after=${hash}+${commits - 36}`;
return url;
};
const createButton = (event) => {
const btn = document.createElement('div');
btn.classList.add('btn', 'earliest-commit-log-btn');
btn.textContent = 'Earliest Commit';
btn.addEventListener('click', event);
return btn;
};
const removeExistingButtons = () => {
document.querySelectorAll('.earliest-commit-log-btn').forEach((e) => e.remove());
};
removeExistingButtons();
const btn = createButton((event) => {
window.location = getEarliestCommitLogUrl();
});
el = document.querySelector('.file-navigation > .flex-auto');
if (!el) {
el = document.querySelector('div.flex-self-center');
}
el.after(btn);
console.log('exec');
};
exec();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment