Last active
June 17, 2021 17:59
-
-
Save jallers/2b954c9e672a1f5795f0952e4e9d39de to your computer and use it in GitHub Desktop.
User script to fix 'Go to File' button for Github branch names that break
This file contains 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 Fix 'Go to File' on Github | |
// @namespace Violentmonkey Scripts | |
// @match https://github.com/* | |
// @match https://github.com/* | |
// @require https://cdn.jsdelivr.net/npm/@violentmonkey/dom@1 | |
// @grant none | |
// @version 1.0 | |
// @author Jim Allers | |
// @description 6/10/2021, 2:00:07 PM | |
// ==/UserScript== | |
const locationPathNodes = window.location.pathname.split('/').filter((token) => token.length > 0); | |
function goToFileExists() { | |
const goToFileButton = document.querySelector('a[data-hotkey="t"]'); | |
if (goToFileButton) { | |
return true; | |
} | |
} | |
function bringBackGoToFile() { | |
if (goToFileExists()) return true; | |
// <include-fragment data-test-selector="overview-actions-fragment" src="/versionone/Core/overview_actions/21.1" class="is-error"></include-fragment> | |
const includeOverviewActionsError = document.querySelector('include-fragment.is-error[data-test-selector="overview-actions-fragment"]'); | |
if (includeOverviewActionsError) { | |
let org, repo, branch, restNodes; | |
if (locationPathNodes.includes('tree')) { | |
[org, repo, , branch, ...restNodes] = locationPathNodes; | |
} else { | |
[org, repo, ...restNodes] = locationPathNodes; | |
branch = document.querySelector('summary[title="Switch branches or tags"] span').textContent | |
} | |
const goToAnchor = document.createElement('a'); | |
goToAnchor.textContent = 'Go to file'; | |
goToAnchor.classList.add('btn', 'ml-2', 'd-none', 'd-md-block'); | |
goToAnchor.setAttribute('data-ga-click', "Repository, find file, location:repo overview"); | |
goToAnchor.setAttribute('data-hotkey', "t"); | |
goToAnchor.setAttribute('data-pjax', "true"); | |
goToAnchor.setAttribute('href', `/${org}/${repo}/find/${branch}`); | |
includeOverviewActionsError.parentElement.replaceChild(goToAnchor, includeOverviewActionsError); | |
// disconnect observer | |
return true; | |
} | |
} | |
// only run this script on the repo tree view | |
if (locationPathNodes.length <= 2 || locationPathNodes.includes('tree')) { | |
if (!goToFileExists() && !bringBackGoToFile()) { | |
VM.observe(document.body, () => { | |
return bringBackGoToFile(); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment