mesak / family_autosplit_num

// ==UserScript==
// @name         family_autosplit_num
// @namespace    http://mesak.tw
// @version      1.1
// @description  simple to key in number
// @author       Mesak
// @match        https://www.family.com.tw/marketing/LotteryEvent/attend.aspx?id=*
// @grant        none
// @license      MIT
// ==/UserScript==
console.log('inclode family_autosplit_num');
(function() {
    'use strict';
    let $ = window.$;
    let output = '';
    function addCssLink(url){
        var link = window.document.createElement('link');
        link.rel = 'stylesheet';
        link.type = 'text/css';
        link.href = url;
        document.getElementsByTagName("head")[0].appendChild(link);
    }
    function setValue( $td , value){
        let v = split_count(value);
        $.each( v , function(i,n){
            $td.find('input').eq(i).val(n);
        })
    }
    function split_count(t){
        let result;
        if( t.length == 16 )
        {
            result = t.match(/(\w{4})(\w{4})(\w{4})(\w{4})/);
        }else{
            result = t.match(/(\w{5})(\w{5})(\w{5})(\w{5})/);
        }
        result.shift();
        return result;
    }
    if( $('input[id^="ctl00_ContentPlaceHolder1_ctl00_Num"]').length )
    {
        let tds = [];

        addCssLink('https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css');
        $('input[id^="ctl00_ContentPlaceHolder1_ctl00_Num"][id$="_1"]').each(function(i,n){
            tds.push( $(n).parent() );
        });

        output = '<hr><div class="container">'+
            '<div class="row">'+
              '<div class="col-sm-10">'+
                '<div class="input-group mb-3"><div class="input-group-prepend"><span class="input-group-text">請輸入完整兌獎<br>編號,輸入完畢按<br>填入即可自動轉換</span></div>'+
                 '<textarea id="txt-autokeyarea" class="form-control" rows="6"></textarea>'+
                '</div>'+
              '</div>'+
              '<div class="col-sm-2 align-self-center">'+
                '<button type="button" id="btn-keyarea" class="btn btn-block btn-primary">填入</button>'+
              '</div>'+
            '</div>'+
          '</div>'

        $('div[id$="ShowPanel"]').append(output);

        $('#btn-keyarea').click(function(e){
            let vs = $('#txt-autokeyarea').val().toUpperCase();
            $('#txt-autokeyarea').val("");
            $.each( vs.split('\n') , function(i,n){
                for(let x in tds){
                    if( !$(tds[x]).data('value') )
                    {
                        $(tds[x]).data('value',n);
                        setValue( $(tds[x]) , n)
                        break;
                    }
                }
            });
        })
    }
})();