Skip to content

Instantly share code, notes, and snippets.

@FlatMapIO
Last active March 31, 2023 04:17
Show Gist options
  • Select an option

  • Save FlatMapIO/aa086305fcc9155f6b71f6b793966e80 to your computer and use it in GitHub Desktop.

Select an option

Save FlatMapIO/aa086305fcc9155f6b71f6b793966e80 to your computer and use it in GitHub Desktop.

Revisions

  1. FlatMapIO revised this gist Mar 31, 2023. 1 changed file with 35 additions and 16 deletions.
    51 changes: 35 additions & 16 deletions github-blocks.js
    Original file line number Diff line number Diff line change
    @@ -4,21 +4,40 @@
    // @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 none
    // @match https://github.com/*/*
    // @grant window.onurlchange
    // ==/UserScript==

    (function() {
    'use strict';
    const repo_url = document.location.href;
    const url_segments = repo_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}`
    const blocks_view = `<li> <a class="btn-sm btn BtnGroup-item" href="${next_url}">Blocks</a> </li>`
    container.innerHTML = blocks_view + container.innerHTML
    })();
    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;
    }
    }
  2. FlatMapIO revised this gist Mar 31, 2023. 1 changed file with 6 additions and 4 deletions.
    10 changes: 6 additions & 4 deletions github-blocks.js
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    // @name Gitbub Blocks
    // @namespace https://github.com/
    // @version 1.0
    // @description Add a button to replace github.com with blocks.githubnext.com in the URL bar on GitHub repositories page
    // @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 none
    @@ -12,11 +12,13 @@
    'use strict';
    const repo_url = document.location.href;
    const url_segments = repo_url.split('/');
    if(url_segments.length < 4) return;
    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);
    const next_url = `https://blocks.githubnext.com/${user}/${repo}/blob/${paths.join('/')}`
    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}`
    const blocks_view = `<li> <a class="btn-sm btn BtnGroup-item" href="${next_url}">Blocks</a> </li>`
    container.innerHTML = blocks_view + container.innerHTML
    })();
  3. FlatMapIO revised this gist Mar 31, 2023. No changes.
  4. FlatMapIO revised this gist Mar 31, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion github-blocks.js
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    // @namespace https://github.com/
    // @version 1.0
    // @description Add a button to replace github.com with blocks.githubnext.com in the URL bar on GitHub repositories page
    // @author @stackobserve
    // @author https://twitter.com/stackobserve
    // @match https://github.com/*
    // @grant none
    // ==/UserScript==
  5. FlatMapIO revised this gist Mar 31, 2023. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions github-blocks.js
    Original file line number Diff line number Diff line change
    @@ -2,8 +2,8 @@
    // @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 Your name
    // @description Add a button to replace github.com with blocks.githubnext.com in the URL bar on GitHub repositories page
    // @author @stackobserve
    // @match https://github.com/*
    // @grant none
    // ==/UserScript==
  6. FlatMapIO created this gist Mar 31, 2023.
    22 changes: 22 additions & 0 deletions github-blocks.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    // ==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 Your name
    // @match https://github.com/*
    // @grant none
    // ==/UserScript==

    (function() {
    'use strict';
    const repo_url = document.location.href;
    const url_segments = repo_url.split('/');
    if(url_segments.length < 4) 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);
    const next_url = `https://blocks.githubnext.com/${user}/${repo}/blob/${paths.join('/')}`
    const blocks_view = `<li> <a class="btn-sm btn BtnGroup-item" href="${next_url}">Blocks</a> </li>`
    container.innerHTML = blocks_view + container.innerHTML
    })();