iCopy5

一键复制iCafe信息,支持生成gitMsg,支持多选,支持预览

  1. // ==UserScript==
  2. // @name iCopy5
  3. // @namespace http://tampermonkey.net/
  4. // @version 5.4.2
  5. // @description 一键复制iCafe信息,支持生成gitMsg,支持多选,支持预览
  6. // @author mzvast@gmail.com
  7. // @match https://console.cloud.baidu-int.com/devops/icafe/*
  8. // @grant none
  9. // @license MIT
  10. // @updateAt 2022-01-12
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. // Your code here...
  17. let panelRootEl = null;
  18. const initPanel = ()=>{
  19. panelRootEl = document.createElement('div');
  20. panelRootEl.innerHTML=`
  21. <div id="icode-master-v5" style="color:#fff;position:fixed;left:0;bottom:100px;z-index:999;background:black;width:50px;height:300px;"><br/>
  22. <box style="background:white;color:#5069e6;">iCopy5</box>
  23. <span id="word"></span>
  24. <img id="girl" style="width:50px;height:50px;border-radius:50%;" crossorigin="anonymous" alt="girl"/>
  25. <hr/>
  26. 模式
  27. <form name="myForm">
  28. <input type="radio" id="off" name="mode" value="0"
  29. >
  30. <label for="off">关闭</label>
  31.  
  32. <input type="radio" id="one" name="mode" value="1" checked>
  33. <label for="one">单选</label>
  34.  
  35. <input type="radio" id="multiple" name="mode" value="2">
  36. <label for="multiple">多选</label>
  37.  
  38. </form>
  39.  
  40. <hr/>
  41. <style>#cp-board:hover{width:250px;height:100px;background:#5069e6;}</style>
  42. <button style="color:#5069e6;background:white;outline:none;border:none;" id="clear">清空</button>
  43.  
  44. <p style="max-height:100px;overflow:auto;" id="cp-board">Empty</p>
  45.  
  46. </div>`
  47. document.body.appendChild (panelRootEl);
  48. }
  49. initPanel();
  50. let boardMsg = '';
  51. let mode= 1
  52. let boardEle = null;
  53. let girlEle = document.getElementById('girl');
  54. let wordEle = document.getElementById('word');
  55. const girlDict = ["https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcTZLZYzIckuEjbJAjMbm2kNEuKsM-fy9HAdHA&usqp=CAU",
  56. "https://5b0988e595225.cdn.sohucs.com/images/20190503/ba81c4b53eae4744a7cef75d5c5abf75.jpeg"
  57. ]
  58. const wordDict = ["加油!","真棒!","爱你!","么么哒", "太酷了","666","mua<3"];
  59. let girlIndex = -1;
  60. let wordIndex = -1;
  61. const setRandomGirl = ()=>{
  62. let newGirlIndex = Math.floor(Math.random()*girlDict.length)
  63. if(girlIndex===newGirlIndex) {
  64. return setRandomGirl(); // same index try again
  65. }
  66. girlIndex = newGirlIndex;
  67. girlEle.src=girlDict[girlIndex];
  68. }
  69. const setRandomWord = ()=>{
  70. let newWordIndex = Math.floor(Math.random()*wordDict.length)
  71. if(wordIndex===newWordIndex) {
  72. return setRandomWord(); // same index try again
  73. }
  74. wordIndex = newWordIndex;
  75. wordEle.innerHTML=wordDict[wordIndex];
  76. }
  77. setRandomGirl();
  78. setRandomWord();
  79.  
  80. // 初始化copy
  81. //
  82. const _execCommand = document.execCommand;
  83.  
  84. // 旧版本icafe用的execaCommand
  85. document.execCommand = async function(evtName){
  86. if(evtName ==='copy'){
  87. console.log('enter exec')
  88. // 由于前者使用必须要selection然后exec copy
  89. // 获取选中部分
  90. const text = window.getSelection().toString()
  91. main(text)
  92. }
  93. }
  94.  
  95. let copyFromNCW = false; // 是否使用的clipboardAPI
  96. // 新版本icafe用的navigator.clipboard.writeText
  97. const _writeText = navigator.clipboard.writeText; // save for later use
  98. // 劫持writeText方法
  99. navigator.clipboard.writeText = async (text) => {
  100. copyFromNCW = true;
  101. main(text);
  102. };
  103.  
  104. const addToBoard = (msg)=>{
  105. boardMsg = msg;
  106. }
  107. const updateBoard = ()=>{
  108. if(!boardEle) boardEle=document.getElementById('cp-board');
  109. boardEle.innerHTML= boardMsg;
  110. }
  111.  
  112. const makeCommitMsg = (cardType, issueId, fullTitle) => {
  113. let type = '✨feat'; // 默认值
  114. if(/缺陷BUG/i.test(cardType)) type = '🐛fix';
  115. else if(/视觉BUG/i.test(cardType)) type = '🎨fix';
  116. else if(/重构/i.test(fullTitle)) type = '♻️chore'
  117. let scope = '';
  118. let title = fullTitle.replace(/\s/g,'');
  119. if (title.indexOf('【') !== -1 && fullTitle.indexOf('】') !== -1) {
  120. const matchedScope = fullTitle
  121. .match(/【(.*?)】/g)
  122. .map(t => t.match(/【(.*)】/)[1]);
  123. scope = `:(${matchedScope.join(',')})`;
  124. title = title.match(/【.*】(.*)/)[1];
  125. }
  126. return `${type}${scope}:[${issueId}]${title}`;
  127. };
  128. const copyText = text => {
  129. // 新接口
  130. if(copyFromNCW){
  131. // this得指向navigator.clipboard实例
  132. _writeText.call(navigator.clipboard, text)
  133. return;
  134. }
  135. const el = document.createElement('textarea');
  136. el.value = text;
  137. document.body.appendChild(el);
  138. el.select();
  139. _execCommand('copy');
  140. document.body.removeChild(el);
  141. console.log('copied:', text);
  142. };
  143. const onChangeMode = e =>{
  144. mode = +e.target.value;
  145. }
  146. const onClear = e =>{
  147. addToBoard('');
  148. updateBoard();
  149. }
  150.  
  151. // 绑定radio
  152. let rad = document.myForm.mode;
  153. for (let i = 0; i < rad.length; i++) {
  154. rad[i].addEventListener('change', onChangeMode);
  155. }
  156.  
  157. let clearBtn = document.getElementById('clear');
  158. clearBtn.addEventListener('click', onClear);
  159.  
  160.  
  161. const handleSelectOne = (msg)=>{
  162. const [issueId,type, ...fullTitle] = msg.split(' ');
  163. const result = makeCommitMsg(type, issueId, fullTitle.join(''));
  164. addToBoard(result);
  165. updateBoard();
  166. copyText(result);
  167. }
  168.  
  169. const handleAppendOne = (msg)=>{
  170. const [issueId,type,...fullTitle] = msg.split(' ');
  171. const result = boardMsg.replace(']',`,${issueId}]`).concat(`,${fullTitle.join('')}`);
  172. addToBoard(result);
  173. updateBoard();
  174. copyText(result);
  175. }
  176.  
  177. const main = (msg) =>{
  178. console.log('main::',msg);
  179. const [issueId,type, ...fullTitle] = msg.split(' ');
  180. // 如果不是我们需要的msg,就不处理
  181. if(!type){
  182. return copyText(msg);
  183. }
  184.  
  185. if(mode===1){ // 单选
  186. handleSelectOne(msg);
  187. }else if(mode===2){ // 多选
  188. if(boardMsg==='') handleSelectOne(msg);
  189. else{
  190. handleAppendOne(msg);
  191. }
  192. }
  193.  
  194. setRandomGirl(); // change图片
  195. setRandomWord();// change文本
  196. }
  197.  
  198.  
  199. })();