Pull request

Add extra copy buttons into pull request pages.

目前為 2019-11-26 提交的版本,檢視 最新版本

// ==UserScript==
// @name Pull request
// @namespace GitHub Scripts
// @description Add extra copy buttons into pull request pages.
// @icon 
// @run-at document-start
// @match *://github.com/*
// @grant none
// @version 1.0.4
// ==/UserScript==

function appendFetchBranchName() {
    var remoteBranchEl = document.getElementsByClassName('TableObject-item--primary')[0];
    if (!document.getElementById('pr_branch_name') && !document.getElementById('pr_reset_fetch') && remoteBranchEl) {
        var childRemoteNodes = remoteBranchEl.querySelectorAll('span');
        var lastChildRemoteNode = childRemoteNodes[childRemoteNodes.length - 1];
        var cpSpanEl = lastChildRemoteNode.cloneNode(true);
        cpSpanEl.setAttribute('id', 'pr_branch_name');
        var cpResetSpanEl = lastChildRemoteNode.cloneNode(true);
        cpSpanEl.setAttribute('id', 'pr_reset_fetch');
        var cpEl = cpSpanEl.firstElementChild;
        var remoteBranchName = cpEl.getAttribute('value');
        var branchName = remoteBranchName.split(':')[1];
        var pullNumber = document.getElementsByClassName('gh-header-number')[0].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="#js-repo-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');

        remoteBranchEl.appendChild(document.createElement('br'));
        remoteBranchEl.appendChild(branchNameEl);
        remoteBranchEl.appendChild(cpSpanEl);
        remoteBranchEl.appendChild(document.createTextNode('\u00A0\u00A0\u00A0'));
        remoteBranchEl.appendChild(resetFetchEl);
        remoteBranchEl.appendChild(cpResetSpanEl);
    }
}

document.addEventListener('DOMContentLoaded', function () {
    var targetNode = document.getElementById('js-repo-pjax-container');
    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();
            }
        }
    };
    var observer = new MutationObserver(callback);
    observer.observe(targetNode, config);
});

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址