Last active
December 12, 2024 11:43
-
-
Save pajecawav/70ffe72bf4aa0968aa9f97318976138f to your computer and use it in GitHub Desktop.
User script for adding a direct link to GitHub repo's ghloc page: https://github.com/pajecawav/ghloc-web
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 ghloc | |
// @namespace http://tampermonkey.net/ | |
// @version 0.0.7 | |
// @description Add ghloc link to GitHub repos. | |
// @author pajecawav | |
// @source https://gist.github.com/pajecawav/70ffe72bf4aa0968aa9f97318976138f | |
// @updateURL https://gist.githubusercontent.com/pajecawav/70ffe72bf4aa0968aa9f97318976138f/raw | |
// @downloadURL https://gist.githubusercontent.com/pajecawav/70ffe72bf4aa0968aa9f97318976138f/raw | |
// @match *://github.com/* | |
// @icon https://icons.duckduckgo.com/ip3/ghloc.vercel.app.ico | |
// @grant none | |
// ==/UserScript== | |
(function () { | |
const LINK_ID = "_ghloc-link"; | |
function parseCurrentGituhbUrl() { | |
const match = location.pathname.match( | |
/\/(?<repo>[^/]+\/[^/]+)(\/(tree|blob)\/(?<branch>[^/]+))?(?<path>\/[^\$]+)?/, | |
); | |
if (!match || !match.groups) { | |
return null; | |
} | |
const groups = match.groups; | |
if (!groups.branch) { | |
const branchSelect = document.querySelector("[data-hotkey='w']"); | |
if (branchSelect?.textContent) { | |
groups.branch = branchSelect.textContent.trim(); | |
} | |
} | |
return groups; | |
} | |
function attachLink() { | |
if (document.getElementById(LINK_ID)) { | |
return; | |
} | |
let withMargin = false; | |
// containers for old ui | |
const oldContainer = | |
document.querySelector(".file-navigation") || | |
document.getElementById("blob-more-options-details")?.parentElement; | |
// container for new ui (currently in A/B test?) | |
const newContainer = document.querySelector(".react-directory-add-file-icon")?.parentElement | |
?.parentElement; | |
const container = oldContainer ?? newContainer; | |
if (container === oldContainer) { | |
withMargin = true; | |
} | |
if (!container) { | |
return; | |
} | |
const url = parseCurrentGituhbUrl(); | |
if (!url) { | |
return; | |
} | |
const link = document.createElement("a"); | |
link.className = ["btn", withMargin ? "ml-2" : ""].filter(Boolean).join(" "); | |
link.textContent = "Stats"; | |
link.id = LINK_ID; | |
link.target = "_blank"; | |
link.rel = "noopener"; | |
link.dataset.hotkey = "g l"; | |
const params = new URLSearchParams(); | |
let href = `https://ghloc.vercel.app/${url.repo}`; | |
if (url.branch) { | |
params.append("branch", url.branch); | |
} | |
const paramsString = params.toString(); | |
if (paramsString) { | |
href += `?${paramsString}`; | |
} | |
link.href = href; | |
container.appendChild(link); | |
} | |
const observer = new MutationObserver(() => { | |
attachLink(); | |
}); | |
observer.observe(document.body, { subtree: true, childList: true }); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment