iCopy5

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

04.11.2020 itibariyledir. En son verisyonu görün.

Bu betiği kurabilmeniz için Tampermonkey, Greasemonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği yüklemek için Tampermonkey gibi bir uzantı yüklemeniz gerekir.

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Userscripts gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği indirebilmeniz için ayrıca Tampermonkey gibi bir eklenti kurmanız gerekmektedir.

Bu komut dosyasını yüklemek için bir kullanıcı komut dosyası yöneticisi uzantısı yüklemeniz gerekecek.

(Zaten bir kullanıcı komut dosyası yöneticim var, kurmama izin verin!)

Bu stili yüklemek için Stylus gibi bir uzantı yüklemeniz gerekir.

Bu stili yüklemek için Stylus gibi bir uzantı kurmanız gerekir.

Bu stili yükleyebilmek için Stylus gibi bir uzantı yüklemeniz gerekir.

Bu stili yüklemek için bir kullanıcı stili yöneticisi uzantısı yüklemeniz gerekir.

Bu stili yüklemek için bir kullanıcı stili yöneticisi uzantısı kurmanız gerekir.

Bu stili yükleyebilmek için bir kullanıcı stili yöneticisi uzantısı yüklemeniz gerekir.

(Zateb bir user-style yöneticim var, yükleyeyim!)

// ==UserScript==
// @name         iCopy5
// @namespace    http://tampermonkey.net/
// @version      5.1.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://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.join(''));
        addToBoard(result);
        updateBoard();
        copyText(result);
    }

    const handleAppendOne = (msg)=>{
        const [issueId,type,...fullTitle] = msg.split(' ');
        const result = boardMsg.replace(']',`,${issueId}]`).concat(`,${fullTitle.join('')}`);
        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
    );

})();