Graphite "Go to Github" button

Add a "Go to Github" button on Graphite PRs

当前为 2023-01-16 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Graphite "Go to Github" button
// @namespace    https://app.graphite.dev
// @version      0.1
// @description  Add a "Go to Github" button on Graphite PRs
// @author       Topher Brown
// @match        https://app.graphite.dev/github/pr/*/*/*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    addButton('Github PR')

    function getPrGithubUrl () {
        // Finds and returns the URL for this PR in Github
        // example match: https://app.graphite.dev/github/pr/ORG/REPO/PR_NUMBER/PR_TITLE
        var pathname = window.location.pathname
        var match = pathname.match(/[^/?]*[^/?]/g)
        var org = match[2]
        var repo = match[3]
        var prNumber = match[4]
        return `https://www.github.com/${org}/${repo}/pull/${prNumber}`
    }

    function addButton(text, onclick, cssObj) {
        cssObj = cssObj || {position: 'absolute', top: '11%', right:'4%', 'z-index': 3}
        let button = document.createElement('a'), btnStyle = button.style
        document.body.appendChild(button)
        button.innerHTML = text
        button.setAttribute("href", getPrGithubUrl())

        Object.keys(cssObj).forEach(key => btnStyle[key] = cssObj[key])
        return button
    }
})();