|
// ==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'); |
|
} |
|
|