Github助手

添加Github文件下载、复制按钮

当前为 2018-02-01 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Github助手
  3. // @namespace https://github.com/yeomanye
  4. // @version 0.2.0
  5. // @description 添加Github文件下载、复制按钮
  6. // @require https://greasyfork.org/scripts/34143-debug/code/debug.js?version=246342
  7. // @require https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/1.7.1/clipboard.min.js
  8. // @require https://cdn.bootcss.com/jquery/2.1.4/jquery.min.js
  9. // @require https://cdn.bootcss.com/jquery.pjax/1.1.0/jquery.pjax.min.js
  10. // @author Ming Ye
  11. // @match https://github.com
  12. // @include https://github.com/*/*
  13. // @grant none
  14. // ==/UserScript==
  15.  
  16. (function() {
  17. 'use strict';
  18. myDebugger.debugD = true;
  19. var log = myDebugger.consoleFactory("github-btn","log",null);
  20. var debugTrue = myDebugger.debugTrue;
  21. // 初始化函数
  22. function init(){
  23. createDownLink();
  24. createCopyLink();
  25. bindImgClick();
  26. }
  27. //创建下载链接
  28. function createDownLink(){
  29. var mouseOverHandler = function(evt){
  30. // debugTrue();
  31. var elem = evt.currentTarget,
  32. aElm = elem.querySelector('.fileDownLink');
  33. aElm.style.visibility = 'visible';
  34. };
  35.  
  36. var mouseOutHandler = function(evt){
  37. // debugTrue();
  38. var elem = evt.currentTarget,
  39. aElm = elem.querySelector('.fileDownLink');
  40. aElm.style.visibility = 'hidden';
  41. };
  42.  
  43. var linkClick = function(evt){
  44. var elem = evt.currentTarget;
  45. var $link = $('<a></a>');
  46. $link.attr('href',elem.getAttribute('download-url'));
  47. $link.attr('download',elem.getAttribute('filename'));
  48. $link.get(0).click();
  49. };
  50. var $files = $('.octicon.octicon-file');
  51. // debugTrue();
  52. var origin = location.origin,
  53. href = location.href,
  54. path = href.replace(origin,'');
  55. if(path.indexOf('tree')<0)
  56. path += '/tree/master/';
  57. path = path.replace('tree','raw');
  58. $files.each(function(i,fileElm){
  59. var trElm = fileElm.parentNode.parentNode,
  60. cntElm = trElm.querySelector('.content'),
  61. cntA = cntElm.querySelector('a'),
  62. fileName = cntA.innerText,
  63. $a = $('<a></a>');
  64. $a.text('下载');
  65. $a.attr({class:'fileDownLink','download-url':path+fileName,'filename':fileName});
  66. $a.css({cursor:'pointer',visibility:'hidden'});
  67. cntElm.appendChild($a.get(0));
  68. log.logObj('tr',trElm);
  69. trElm.onmouseover=mouseOverHandler;
  70. trElm.onmouseout=mouseOutHandler;
  71. $a.on('click',linkClick);
  72. });
  73. }
  74. //创建复制链接
  75. function createCopyLink(){
  76. var tmpArr = location.href.split('/');
  77. tmpArr = tmpArr[tmpArr.length-1].split('.');//获取扩展名
  78. var excludeExts = ['jpg','md','markdown','MD','png'];
  79. if(tmpArr.length > 1 && excludeExts.indexOf(tmpArr[1]) >= 0) return;
  80. var $btnGroup = $('.file-actions .BtnGroup');
  81. if($btnGroup.length == 0)return;
  82. var $a = $('<a></a>');
  83. $a.attr({href:'#',class:'btn btn-sm BtnGroup-item copyButton'});
  84. $a.html('Copy');
  85. $btnGroup.append($a);
  86. var addClickHandler = function(){
  87. var $codes = $('.js-file-line-container .js-file-line'),
  88. text = "";
  89. $codes.each(function(index,code){
  90. log.logObj('code',code);
  91. text += code.innerText;
  92. if(code.innerText.indexOf('\n')<0) text += '\n';
  93. });
  94. $a.attr('data-clipboard-text',text);
  95. new Clipboard('.copyButton');
  96. log.logObj('text',text);
  97. };
  98. $a.one('click',function(evt){
  99. clearTimeout(timeout);
  100. addClickHandler();
  101. $a.click();
  102. });
  103. var timeout = setTimeout(addClickHandler,1000);
  104. }
  105. //点击图片处理函数
  106. function bindImgClick(){
  107. var $imgs = $('article img');
  108. var newImg = null;
  109. var $modal = null;
  110. var width = $(window).width(),height = $(window).height();
  111. var newImgOnload = function(){
  112. var imgWidth = newImg.width,imgHeight = newImg.height;
  113. if(imgWidth > width || imgHeight > height)
  114. if(height > width){
  115. newImg.width = width;
  116. }else{
  117. newImg.height = height;
  118. }
  119. newImg.style.marginLeft = (width - newImg.width)/2 + 'px';
  120. newImg.style.marginTop = (height - newImg.height)/2 + 'px';
  121. };
  122. var initModal = function(){
  123. $modal = $('<div></div>');
  124. newImg = new Image();
  125. $modal.css({position:'fixed',width:width+'px',height:height+'px','background-color':'rgba(0,0,0,0.5)',top:0,left:0,'z-index':-1,'padding-top':0,'padding-left':'auto',visibility:'hidden'});
  126. $modal.append(newImg);
  127. $('body').append($modal);
  128. $modal.one('click',function(e){
  129. $modal.css({'z-index':-1,'visibility':-1});
  130. });
  131. };
  132. var imgClickHandler = function(e){
  133. log('imgClickHandler');
  134. if(!$modal) initModal();
  135. $modal.css({visibility:'visible','z-index':999});
  136. var oldImg = e.currentTarget;
  137. newImg.src = oldImg.src;
  138. //计算宽高
  139. newImg.onload = newImgOnload;
  140. };
  141. $imgs.each(function(i,img){
  142. var aElm = img.parentNode;
  143. aElm.removeAttribute('href');
  144. var $img = $(img);
  145. $img.css('cursor','pointer').on('click',imgClickHandler);
  146. });
  147. }
  148. init();
  149. $(document).on('pjax:success',function(evt){
  150. log('pjax:success');
  151. init();
  152. });
  153. })();