iCopy5

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

Pada tanggal 25 September 2020. Lihat %(latest_version_link).

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         iCopy5
// @namespace    http://tampermonkey.net/
// @version      5.0
// @description  一键复制iCafe信息,支持生成gitMsg,支持多选,支持预览
// @author       [email protected]
// @match        http://newicafe.baidu.com/v5/space/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Your code here...
    let panelRootEl = null;
    const initPanel = ()=>{
        panelRootEl = document.createElement('div');
        panelRootEl.innerHTML=`
<div id="icode-master-v5"  style="color:#fff;position:fixed;left:0;top:50%;z-index:999;background:rgba(255,255,255,0.1);width:50px;height:500px;"><br/>
<box style="background:white;color:#5069e6;">iCopy5</box>
<span id="word"></span>
<img id="girl" style="width:50px;height:50px;border-radius:50%;" crossorigin="anonymous" alt="girl"/>
<hr/>
模式
<form name="myForm">
  <input type="radio" id="off" name="mode" value="0"
         >
  <label for="off">关闭</label>

  <input type="radio" id="one" name="mode" value="1" checked>
  <label for="one">单选</label>

  <input type="radio" id="multiple" name="mode" value="2">
  <label for="multiple">多选</label>

</form>

<hr/>
<style>#cp-board:hover{width:250px;height:100px;background:#5069e6;}</style>
<button style="color:#5069e6;background:white;outline:none;border:none;" id="clear">清空</button>

<p style="max-height:100px;overflow:auto;" id="cp-board">Empty</p>

</div>`
        document.body.appendChild (panelRootEl);
    }
    initPanel();
    let boardMsg = '';
    let mode= 1
    let boardEle = null;
    let girlEle = document.getElementById('girl');
    let wordEle = document.getElementById('word');
    const girlDict = ["https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcTZLZYzIckuEjbJAjMbm2kNEuKsM-fy9HAdHA&usqp=CAU",
                      "https://lh3.googleusercontent.com/proxy/tNwHD7z57OHQXuuGm4y4XPzKX7vEFmcI9VMu3smStgwqC_LnN35ghGYC0_2I8GHAmI0OsQgJON0tyAzsGDFbX97CjY_vGhZ8tQ",
                      "https://5b0988e595225.cdn.sohucs.com/images/20190503/ba81c4b53eae4744a7cef75d5c5abf75.jpeg"
                     ]
    const wordDict = ["加油!","真棒!","爱你!","么么哒", "太酷了","666","mua<3"];
    let girlIndex = -1;
    let wordIndex = -1;
    const setRandomGirl = ()=>{
        let newGirlIndex = Math.floor(Math.random()*girlDict.length)
        if(girlIndex===newGirlIndex) {
            return setRandomGirl(); // same index try again
        }
        girlIndex = newGirlIndex;
        girlEle.src=girlDict[girlIndex];
    }
    const setRandomWord = ()=>{
        let newWordIndex = Math.floor(Math.random()*wordDict.length)
        if(wordIndex===newWordIndex) {
            return setRandomWord(); // same index try again
        }
        wordIndex = newWordIndex;
        wordEle.innerHTML=wordDict[wordIndex];
    }
    setRandomGirl();
    setRandomWord();
    const addToBoard = (msg)=>{
        boardMsg = msg;
    }
    const updateBoard = ()=>{
        if(!boardEle) boardEle=document.getElementById('cp-board');
        boardEle.innerHTML= boardMsg;
    }

    const makeCommitMsg = (cardType, issueId, fullTitle) => {
        let type = /bug/i.test(cardType)?'fix':'feat';
        let scope = '';
        let title = fullTitle.replace(/\s/g,'');
        if (title.indexOf('【') !== -1 && fullTitle.indexOf('】') !== -1) {
            const matchedScope = fullTitle
            .match(/【(.*?)】/g)
            .map(t => t.match(/【(.*)】/)[1]);
            scope = `:(${matchedScope.join(',')})`;
            title = title.match(/【.*】(.*)/)[1];
        }
        return `${type}${scope}:[${issueId}]${title}`;
    };
    const copyText = text => {
        const el = document.createElement('textarea');
        el.value = text;
        document.body.appendChild(el);
        el.select();
        document.execCommand('copy');
        document.body.removeChild(el);
        console.log('copied:', text);
    };
    const onChangeMode = e =>{
        mode = +e.target.value;
    }
    const onClear = e =>{
        addToBoard('');
        updateBoard();
    }

    // 绑定radio
    let rad = document.myForm.mode;
    for (let i = 0; i < rad.length; i++) {
        rad[i].addEventListener('change', onChangeMode);
    }

    let clearBtn = document.getElementById('clear');
    clearBtn.addEventListener('click', onClear);


    const handleSelectOne = (msg)=>{
        const [issueId,type,fullTitle] = msg.split(' ');
        const result = makeCommitMsg(type, issueId, fullTitle);
        addToBoard(result);
        updateBoard();
        copyText(result);
    }

    const handleAppendOne = (msg)=>{
        const [issueId,type,fullTitle] = msg.split(' ');
        const result = boardMsg.replace(']',`,${issueId}]`).concat(`,${fullTitle}`);
        addToBoard(result);
        updateBoard();
        copyText(result);
    }

    document.addEventListener(
        'click',
        e => {
            console.log('click target',e.target);
            if (e.target) {
                if (e.target.matches('.titleValue.showIssueView.value')) {
                    // console.log('.titleValue.showIssueView.value');
                    const type = e.target.parentElement.parentElement.parentElement.nextElementSibling.children[0].children[0].textContent;
                    const fullTitle = e.target.getAttribute('title');
                    const issueId = e.target.getAttribute('data-issueid');
                    const result = makeCommitMsg(type, issueId, fullTitle);
                    copyText(result);
                } else if (e.target.matches('a.taskLink.titleLink')) {
                    // console.log('a.taskLink.titleLink');
                    const type = e.target.parentElement.parentElement.nextElementSibling.childNodes[1].textContent.replace(/\s/g,'');
                    const fullTitle = e.target.text;
                    const issueId = e.target
                    .getAttribute('href')
                    .match(/issue\/(.*)\/show/)[1];
                    const result = makeCommitMsg(type, issueId, fullTitle);
                    copyText(result);
                } else if (e.target.matches('[class^=issue-sequence-text-root-]')) {
                    if(mode===0) return;
                    e.preventDefault();
                    e.stopImmediatePropagation(); // 屏蔽icafe原生操作
                    const msg = e.target.parentElement.parentElement.getAttribute('data-clipboard-text');
                    console.log(msg);

                    if(mode===1){ // 单选
                        handleSelectOne(msg);
                    }else if(mode===2){ // 多选
                        if(boardMsg==='') handleSelectOne(msg);
                        else{
                            handleAppendOne(msg);
                        }
                    }

                    setRandomGirl(); // change图片
                    setRandomWord();// change文本

                }
            }
        },
        true
    );

})();