Skip to content

Instantly share code, notes, and snippets.

@foka
Last active January 14, 2022 12:31
Show Gist options
  • Save foka/881a73313348927d547420101259abda to your computer and use it in GitHub Desktop.
Save foka/881a73313348927d547420101259abda to your computer and use it in GitHub Desktop.
Add GitLab's-like [History] button on GitHub pages via Tampermonkey
// ==UserScript==
// @name Add GitLab's-like [History] button on GitHub pages
// @namespace http://tampermonkey.net/
// @version 1.1
// @author foka
// @match *://github.com/*
// @icon https://www.google.com/s2/favicons?domain=github.com
// @grant none
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js
// ==/UserScript==
(function() {
'use strict';
const goToFileSelector = 'a[data-hotkey="t"]:not(.dropdown-item)';
const historyIconSelector = 'svg.octicon-history';
waitForKeyElements(goToFileSelector, goToFile => addHistoryBeforeGoToFile(goToFile[0], document.querySelector(historyIconSelector)));
waitForKeyElements(historyIconSelector, historyIcon => addHistoryBeforeGoToFile(document.querySelector(goToFileSelector), historyIcon[0]));
})();
function addHistoryBeforeGoToFile(goToFile, historyIcon) {
if (!goToFile || !historyIcon || document.getElementById('gitlab-history-button')) {
return;
}
const historyUrl = historyIcon.closest('a').getAttribute('href');
const addRightMargin = ! hasLeftMargin(goToFile);
goToFile.before(createHistoryElement(historyUrl, addRightMargin));
}
function createHistoryElement(historyUrl, addRightMargin) {
const history = document.createElement('a');
history.id = 'gitlab-history-button';
history.setAttribute('href', historyUrl);
history.innerText = 'History';
history.classList.add('btn');
if (addRightMargin) {
history.classList.add('mr-2');
}
return history;
}
function hasLeftMargin(element) {
return !!element.classList.contains('ml-2');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment