Pull request

Add extra copy buttons into pull request pages.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name Pull request
// @namespace GitHub Scripts
// @description Add extra copy buttons into pull request pages.
// @icon https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png
// @run-at document-start
// @match *://github.com/*
// @grant none
// @version 1.1.5
// ==/UserScript==

function appendFetchBranchName() {
    var remoteBranchEl = document.getElementsByClassName('gh-header-meta')[0];
    if (!document.getElementById('pr_reset_fetch') && remoteBranchEl) {
        var childRemoteNodes = remoteBranchEl.querySelectorAll('span:not(.d-inline-block)');
        var lastChildRemoteNode = childRemoteNodes[childRemoteNodes.length - 1];
        var cpSpanEl = lastChildRemoteNode.cloneNode(true);
        var cpResetSpanEl = lastChildRemoteNode.cloneNode(true);
        var cpEl = cpSpanEl.firstElementChild;
        var remoteBranchName = cpEl.getAttribute('value');
        var branchName = remoteBranchName.split(':')[1] ?? remoteBranchName.split(':')[0];
        var pullNumber = document.getElementsByClassName('js-issue-title')[0].nextElementSibling.textContent.substr(1);
        var pullName = 'pull/' + pullNumber + '/head:' + branchName;
        cpEl.setAttribute('value', branchName);

        var branchNameEl = document.createElement('span');
        branchNameEl.innerText = branchName;
        branchNameEl.setAttribute('class', 'commit-ref');

        var remoteRefEl = document.querySelector('[data-pjax="#repo-content-pjax-container"]');
        var remoteUrl = '[email protected]:' + remoteRefEl.href.replace(/^.*\/\/[^\/]+/, '').substr(1); + '.git';
        var gitCommands = 'git stash';
        gitCommands += ' && git fetch ' + remoteUrl + ' +refs/pull/' + pullNumber + '/head';
        gitCommands += ' && git checkout FETCH_HEAD && git log --oneline -n 2';
        cpResetSpanEl.firstElementChild.setAttribute('value', gitCommands);

        var resetFetchEl = document.createElement('span');
        resetFetchEl.innerText = 'Fetch and pull PR\'s branch commands';
        resetFetchEl.setAttribute('class', 'commit-ref');
        resetFetchEl.setAttribute('id', 'pr_reset_fetch');

        var appendedEl = remoteBranchEl.querySelector('.flex-auto');
        appendedEl.appendChild(document.createElement('br'));
        if (remoteBranchName.split(':')[1]) {
          appendedEl.appendChild(branchNameEl);
          appendedEl.appendChild(cpSpanEl);
          appendedEl.appendChild(document.createTextNode('\u00A0\u00A0\u00A0'));
        }
        appendedEl.appendChild(resetFetchEl);
        appendedEl.appendChild(cpResetSpanEl);
    }
}

document.addEventListener('DOMContentLoaded', function () {
    appendFetchBranchName();
    var targetNode = document.documentElement || document.body;
    var config = { attributes: true, childList: true, subtree: true };
    var callback = function (mutations) {
        var count = 0;
        for (var i = 0; i < mutations.length; i++) {
            var mutation = mutations[i];
            if (!count && mutation.type === 'attributes' && mutation.attributeName === 'tabindex') {
                count++;
                appendFetchBranchName();
                document.getElementById('files') && targetNode.children[1] && targetNode.children[1].classList.remove('container-lg');
            }
        }
    };
    var observer = new MutationObserver(callback);
    (targetNode instanceof Element || targetNode instanceof HTMLDocument) && observer.observe(targetNode, config);
});