Last active
March 31, 2023 04:17
-
-
Save FlatMapIO/aa086305fcc9155f6b71f6b793966e80 to your computer and use it in GitHub Desktop.
Add a Button for the Github page to open in https://blocks.githubnext.com
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 Gitbub Blocks | |
// @namespace https://github.com/ | |
// @version 1.0 | |
// @description Add a button to replace github.com with block.githubnext.com in the URL bar on GitHub repositories page | |
// @author https://twitter.com/stackobserve | |
// @match https://github.com/*/* | |
// @grant window.onurlchange | |
// ==/UserScript== | |
if (window.onurlchange === null) { | |
// feature is supported | |
window.addEventListener("urlchange", inject); | |
} | |
function inject({ url }) { | |
// console.log("inject", url); | |
const url_segments = url.split("/"); | |
if (url_segments.length < 5) return; | |
const container = document.querySelector("ul.pagehead-actions"); | |
if (container === null) { | |
return console.warn( | |
"[GithubBlocks] query is invalid: ul.pagehead-actions is not found" | |
); | |
} | |
const [user, repo, ...paths] = url_segments.slice(3); | |
if (paths[0] === "tree") paths[0] = "blob"; | |
const path = paths.join("/"); | |
const next_url = `https://blocks.githubnext.com/${user}/${repo}/${path}`; | |
render(container, next_url); | |
} | |
function render(parent, url) { | |
const newInnerHTML = `<a id='__gm_blocks' class="btn-sm btn BtnGroup-item" href="${url}">Blocks</a>`; | |
let target = document.getElementById("__gm_blocks"); | |
if (target === null) { | |
const li = document.createElement("li"); | |
li.innerHTML = newInnerHTML; | |
parent.insertBefore(li, parent.firstChild); | |
target = li.children.item(0); | |
} else { | |
target.parentElement.innerHTML = newInnerHTML; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment