Copy github file to clipboard

Add copy github file text to clipboard

スクリプトをインストールするには、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         Copy github file to clipboard
// @description  Add copy github file text to clipboard
// @namespace    org.adong
// @version      0.1
// @author       adong
// @homepageURL  https://github.com/ldong/github-copy-text
// @supportURL   https://github.com/ldong/github-copy-text/README.md
// @match        *://github.com/*
// @match        *://www.github.com/*
// @include      /^https?://git\.corp.*/.*$/
// @grant        GM_setClipboard
// @grant        GM_xmlhttpRequest
// ==/UserScript==

(function() {
    // copied from http://stackoverflow.com/a/36640126/2305243
    function CopyToClipboard(node) {
        var range;
        if (document.selection) {
            range = document.body.createTextRange();
            range.moveToElementText(node);
            range.select().createTextRange();
            document.execCommand("Copy");

        } else if (window.getSelection) {
            range = document.createRange();
            range.selectNode(node);
            window.getSelection().addRange(range);
            document.execCommand("Copy");
            alert("text copied");
        }
    }


    var copyButton = document.createElement('button');
    copyButton.classList = 'btn btn-sm BtnGroup-item copy-file-another';
    copyButton.textContent = 'Copy Text';

    copyButton.addEventListener('click', function(e) {
        e.preventDefault();
        var copyContentNode = document.querySelector(".file > .file-header").nextElementSibling;
        CopyToClipboard(copyContentNode);
    });


    var targetButtonGroupClass = '.BtnGroup.float-right';
    var targetButtonGroup = document.querySelector(targetButtonGroupClass);
    if (targetButtonGroup) {
        targetButtonGroup.appendChild(copyButton);
    } else {
        console.error('Please file a bug, or make a PR. ');
    }

})();