Github辅助按钮

Github文件下载和复制按钮

目前為 2018-01-30 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Github辅助按钮
// @namespace    https://github.com/yeomanye
// @version      0.0.2
// @description  Github文件下载和复制按钮
// @require      https://greasyfork.org/scripts/34143-debug/code/debug.js?version=246342
// @require      https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/1.7.1/clipboard.min.js
// @author       Ming Ye
// @match        https://github.com
// @include      https://github.com/*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    myDebugger.debugD = false;
    var log = myDebugger.consoleFactory("github-btn","log",null);
    var debugTrue = myDebugger.debugTrue;  
    // 初始化函数
    function init(){
        createDownLink();
        createCopyLink();
    }
    //创建下载链接
    function createDownLink(){
        var mouseOverHandler = function(evt){
        // debugTrue();
        var elem = evt.currentTarget,
            aElm = elem.querySelector('.fileDownLink');
        aElm.style.display = 'inline-block';
        };

        var mouseOutHandler = function(evt){
            // debugTrue();
            var elem = evt.currentTarget,
                aElm = elem.querySelector('.fileDownLink');
            aElm.style.display = 'none';
        };

        var linkClick = function(evt){
            var elem = evt.currentTarget;
            var link = document.createElement('a');
            link.setAttribute('href',elem.getAttribute('download-url'));
            link.setAttribute('download',elem.getAttribute('filename'));
            link.click();
        };
        
        var nodeList = document.querySelectorAll('.octicon.octicon-file');
        // debugTrue();
        var origin = location.origin,
            href = location.href,
            path = href.replace(origin,'');
        if(path.indexOf('tree')<0)
            path += '/tree/master/';
        path = path.replace('tree','raw');
        for(var i=0,len=nodeList.length;i<len;i++){
            var trElm = nodeList[i].parentNode.parentNode,
                cntElm = trElm.querySelector('.content'),
                cntA = cntElm.querySelector('a'),
                fileName = cntA.innerText,
                aElm = document.createElement('a');
            aElm.innerText = '下载';
            aElm.className = 'fileDownLink';
            aElm.style.cursor = 'pointer';
            aElm.style.display = 'none';
            aElm.setAttribute('download-url',path+fileName);
            aElm.setAttribute('filename',fileName);
            cntElm.appendChild(aElm);
            log.logObj('tr',trElm);
            trElm.onmouseover=mouseOverHandler;
            trElm.onmouseout=mouseOutHandler;
            aElm.onclick = linkClick;
        }
    }
    //创建复制链接
    function createCopyLink(){
        var btnGroup = document.querySelector('.file-actions .BtnGroup');
        var aElm = document.createElement('a');
        if(!btnGroup)return;
        aElm.href = '#';
        aElm.innerHTML = 'Copy';
        aElm.className = 'btn btn-sm BtnGroup-item copyButton';
        btnGroup.appendChild(aElm);
        var addClickHandler = function(){
            var container = document.querySelector('.js-file-line-container'),
                codeArr = container.querySelectorAll('.js-file-line'),
                text = "";
            for(var code of codeArr){
                text += code.innerText ;
                if(code.innerText.indexOf('\n')<0) text += '\n';
            }
            aElm.setAttribute('data-clipboard-text',text);
            new Clipboard('.copyButton');
            log.logObj('text',text);     
        };
        aElm.onclick = function(evt){
            clearTimeout(timeout);
            addClickHandler();
            aElm.click();
        };
        var timeout = setTimeout(addClickHandler,1000);
    }
    init();
    // log.logObj('$',$);
})();