Last active
April 16, 2022 13:21
-
-
Save hayleyxyz/3c76973052b85b5f2e0f44a0c2881290 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==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