iCode Helper

一键复制iCafe单信息,形成Git Commit Msg,支持旧版迭代计划和工作台

  1. // ==UserScript==
  2. // @name iCode Helper
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.3
  5. // @description 一键复制iCafe单信息,形成Git Commit Msg,支持旧版迭代计划和工作台
  6. // @author mzvast@gmail.com
  7. // @match http://newicafe.baidu.com/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. // Your code here...
  15. const makeCommitMsg = (cardType, issueId, fullTitle) => {
  16. let type = /bug/i.test(cardType)?'fix':'feat';
  17. let scope = '';
  18. let title = fullTitle.replace(/\s/g,'');
  19. if (title.indexOf('【') !== -1 && fullTitle.indexOf('】') !== -1) {
  20. const matchedScope = fullTitle
  21. .match(/【(.*?)】/g)
  22. .map(t => t.match(/【(.*)】/)[1]);
  23. scope = `:(${matchedScope.join(',')})`;
  24. title = title.match(/【.*】(.*)/)[1];
  25. }
  26. return `${type}${scope}:[${issueId}]${title}`;
  27. };
  28. const copyText = text => {
  29. const el = document.createElement('textarea');
  30. el.value = text;
  31. document.body.appendChild(el);
  32. el.select();
  33. document.execCommand('copy');
  34. document.body.removeChild(el);
  35. console.log('copied:', text);
  36. };
  37. document.addEventListener(
  38. 'click',
  39. e => {
  40. console.log(e.target);
  41. if (e.target) {
  42. if (e.target.matches('.titleValue.showIssueView.value')) {
  43. // console.log('.titleValue.showIssueView.value');
  44. const type = e.target.parentElement.parentElement.parentElement.nextElementSibling.children[0].children[0].textContent;
  45. const fullTitle = e.target.getAttribute('title');
  46. const issueId = e.target.getAttribute('data-issueid');
  47. const result = makeCommitMsg(type, issueId, fullTitle);
  48. copyText(result);
  49. } else if (e.target.matches('a.taskLink.titleLink')) {
  50. // console.log('a.taskLink.titleLink');
  51. const type = e.target.parentElement.parentElement.nextElementSibling.childNodes[1].textContent.replace(/\s/g,'');
  52. const fullTitle = e.target.text;
  53. const issueId = e.target
  54. .getAttribute('href')
  55. .match(/issue\/(.*)\/show/)[1];
  56. const result = makeCommitMsg(type, issueId, fullTitle);
  57. copyText(result);
  58. }
  59. }
  60. },
  61. true
  62. );
  63.  
  64. })();