GitHub NBViewer Transfer

Add buttons to open the current GitHub notebook in NBViewer and return from NBViewer to GitHub.

スクリプトをインストールするには、Tampermonkey, GreasemonkeyViolentmonkey のような拡張機能のインストールが必要です。

You will need to install an extension such as Tampermonkey to install this script.

スクリプトをインストールするには、TampermonkeyViolentmonkey のような拡張機能のインストールが必要です。

スクリプトをインストールするには、TampermonkeyUserscripts のような拡張機能のインストールが必要です。

このスクリプトをインストールするには、Tampermonkeyなどの拡張機能をインストールする必要があります。

このスクリプトをインストールするには、ユーザースクリプト管理ツールの拡張機能をインストールする必要があります。

(ユーザースクリプト管理ツールは設定済みなのでインストール!)

このスタイルをインストールするには、Stylusなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus などの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus tなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

(ユーザースタイル管理ツールは設定済みなのでインストール!)

このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください
// ==UserScript==
// @name         GitHub NBViewer Transfer
// @namespace    yuhuangmeng
// @version      1.0.0
// @description  Add buttons to open the current GitHub notebook in NBViewer and return from NBViewer to GitHub.
// @author       yuhuangmeng
// @homepageURL  https://greasyfork.org/zh-CN/users/1065289-yuhuangmeng
// @match        https://github.com/*
// @match        https://nbviewer.org/github/*
// @grant        none
// @license      MIT
// ==/UserScript==

/**
 * TODO:
 * 1. 将两个按钮合并为一个按钮“Github/NBViewer Switcher”, 但在 github.com的点击按钮的功能是打开 nbviewer,在 nbviewer 网站按钮的功能是打开 github 仓库
 * 2. debug
 */


(function() {
    'use strict';

    // Function to get the current GitHub notebook URL
    function getGitHubURL() {
        const currentURL = window.location.href;
        if (currentURL.includes("github.com")) {
            return currentURL;
        }
        const parts = currentURL.split("/");
        const username = parts[parts.indexOf("github") + 1];
        const repo = parts[parts.indexOf("github") + 2];
        return `https://github.com/${username}/${repo}`;
    }

    // Function to add a button
    function addButton(text, onClick) {
        const button = document.createElement("a");
        button.innerHTML = text;
        button.className = "btn btn-sm";
        button.href = "#";
        button.style.marginRight = "10px";
        button.onclick = onClick;
        return button;
    }

    // Create a button to open in NBViewer
    const nbviewerButton = addButton("Open in NBViewer", function() {
        const notebookURL = getNotebookURL();
        window.open(notebookURL, "_blank");
    });

    // Create a button to return to GitHub
    const githubButton = addButton("Return to GitHub", function() {
        const githubURL = getGitHubURL();
        window.open(githubURL, "_blank");
    });

    // Create a container div for the buttons
    const buttonContainer = document.createElement("div");
    buttonContainer.appendChild(nbviewerButton);
    buttonContainer.appendChild(githubButton);

    // Add styles for positioning the button container
    buttonContainer.style.position = "fixed";
    buttonContainer.style.bottom = "10px";
    buttonContainer.style.right = "10px";
    buttonContainer.style.zIndex = "9999";

    // Add the button container to the document body
    document.body.appendChild(buttonContainer);

    // Function to get the current GitHub notebook URL
    function getNotebookURL() {
        const currentURL = window.location.href;
        const notebookURL = currentURL.replace("github.com", "nbviewer.org/github");
        return notebookURL;
    }
})();