Automatically Check Promotion Details

Automatically check promotion details according to title contents

Versione datata 02/10/2017. Vedi la nuova versione l'ultima versione.

// ==UserScript==
// @name         Automatically Check Promotion Details
// @version      4.4.0
// @description  Automatically check promotion details according to title contents
// @author       Tiger
// @match        http://tool-bcg.bwe.io/editor/promo.php?*
// @exclude      /^http://tool-bcg.bwe.io/editor/promo.php\?site=cs\w{2}&action=edit*/
// @grant        none
// @namespace http://tampermonkey.net/
// ==/UserScript==

(function() {
    class AutoCheck
    {
        constructor(scope,title,checkBoxes,originalCurrency)
        {
            this.scope = scope;
            this.title = title;
            this.matchRes = '';
            this.checkBoxes = checkBoxes;
            this.len = checkBoxes.length;
            this.originalCurrency = originalCurrency;
            this.sitewide = false;//辅助clearance
            this.sitewideChecked = this.percentChecked = this.moneyChecked = this.clearanceChecked = this.fsChecked = this.rebateChecked =this.rewardChecked = this.ftChecked = this.fdChecked =this.fgChecked= this.fsamChecked = this.bngnChecked = this.fromChecked = false;
            this.lastCurrency = '';
            this.lastNumber = '';
            this.initScopeCheck();
            this.initPromoCheck();
        }
        initScopeCheck()
        {
            this.scope[2].checked = true;
            this.sitewideCheck();
        }
        sitewideCheck()
        {
            let s = this.title.value;
            if(!s||s.match(/(new customer)|student/gi))return;
            //|\d+%?\s(\w+\s)+\D+\s?\d+\+?(\s(?!on)|$|,)
            this.matchRes = s.match(/ on \D+\s?\d+\+?|sitewide|site wide|site-wide|storewide|store wide|store-wide|(whole|entire) (site|store)|everything|(all|every|each|any|your|all of your|entire|one|\d+) (order(s?)|next order|purchase(s?)|next purchase|product(s?)|merchandise|item(s?)|brand(s?))|(((when (spend|spending|you spend)|with (order(s?)|purchase(s?)) of) \D+\s?\d+\+?)|with \D+\d+\+?(\s(order(s?)|purchase(s?)))+)($|\s(?!on))/gi);
            if(this.matchRes===null)
            {
                this.sitewideChecked = false;
                this.scope[0].checked = false; 
                this.scope[2].checked = true; 
            }
            else
            {
                if(!this.sitewideChecked)
                {
                    this.sitewideChecked = true;
                    this.scope[0].checked = true;
                    this.checkBoxes[3].checked = false;
                }
            }

        }
        promoDetailCheck(index,regRule,flag,FnSuccess=function(){},FnFail=function(){},specialCheck=false)
        {
            let s = this.title.value;
            if(!s)return;
            this.matchRes = s.match(regRule);
            switch(specialCheck)
            {
                case 1:
                    if(this.matchRes===null)
                    {
                        this.checkBoxes[index].checked = false;
                        FnFail();
                    }
                    else
                    {
                        FnSuccess();
                    }
                    break;
                default:
                    if(this.matchRes===null)
                    {
                        flag = false;
                        this.checkBoxes[index].checked = false;
                        FnFail();
                    }
                    else
                    {
                        if(!flag)
                        {
                            flag  = true;
                            this.checkBoxes[index].checked = true;
                            FnSuccess();
                        }
                    }
                    break;
            }
        }
        fsCheck()
        {
            let regRule = /((free|complimentary)+(.*)?(shipp|shipment|fs|s&h|delivery|postage|p&p|click)|((\$|€|£|Rs|RS\.|¥|₦|CHF|USD|CAD|GBP|POUND|RMB|AUD|INR|EUR|US$|CA$|AU$)\s?0 ship))/gi;
            this.promoDetailCheck(0,regRule,this.fsChecked);
        }
        getCurrency(currency)
        {
            switch(currency)
            {
                case '$':
                case 'usd':
                case 'cad':
                case 'aud':
                case 'us$':
                case 'ca$':
                case 'au$':
                    currency = 'dollar';
                    break;
                case '£':
                case 'gbp':
                case 'pound':
                    currency = 'pound';
                    break;
                case '€':
                case 'eur':
                    currency = 'euro';
                    break;
                case 'rmb':
                case '¥':
                case 'cny':
                    currency = 'rmb';
                    break;
                case 'rs':
                case 'inr':
                case 'rs.':
                    currency = 'rupee';
                    break;
                case '₦':
                    currency = 'naira';
                    break;
                case 'chf':
                    currency = 'chf';
                    break;
            }
            return currency;
        }
        moneyCheck()
        {
            let regRule = /(save\s)?(up to |over |more than |at least )?((\$|€|£|Rs|RS\.|¥|₦|CHF|USD|CAD|GBP|POUND|RMB|AUD|INR|EUR|US$|CA$|AU$)\s?)?(\d+|\d+\.\d+|(\d{1,3},)?\d{1,3},\d{3})\s?(\$|€|£|Rs|RS\.|¥|₦|CHF|USD|CAD|GBP|POUND|RMB|AUD|INR|EUR|US$|CA$|AU$)?\s(off|on|discounts?|savings?|(\w+\s)+(gift cards?|gift certificates?|vouchers?|gift vouchers?))\s?/gi;
            this.promoDetailCheck(1,regRule,this.moneyChecked,()=>{
                let currencySymbol = this.getCurrency((RegExp.$4||RegExp.$7).toLowerCase());
                let currencyNumber = RegExp.$5.includes(',')? RegExp.$5.replace(/,/gi,''):RegExp.$5;
                this.checkBoxes[1].parentElement.nextElementSibling.value = currencySymbol ;
                this.checkBoxes[1].parentElement.nextElementSibling.nextElementSibling.value = currencyNumber;
            },()=>{
                this.checkBoxes[1].parentElement.nextElementSibling.value = this.originalCurrency;
                this.checkBoxes[1].parentElement.nextElementSibling.nextElementSibling.value ='';
            });
        }
        percentCheck()
        {
            let regRule = /(save\s(up to |at least |more than |over )?(\d+|\d+\.\d+)%(\s|$))|((\d+|\d+\.\d+)%\s(off|discount(s?)|saving(s?))(\s|$))/gi;
            this.promoDetailCheck(2,regRule,this.percentChecked,()=>{
                let percentNumber = RegExp.$3 || RegExp.$6;
                this.checkBoxes[2].parentElement.nextElementSibling.value = percentNumber;
            },()=>{
                this.checkBoxes[2].parentElement.nextElementSibling.value = '';
            });
        }
        clearanceCheck()
        {
            this.sitewide = document.querySelector('input[value=SITE_WIDE]').checked;
            if(this.sitewide)return;
            let regRule = /\s?(clearan|closeout|sale|reduced|outlet)\s?/gi;
            this.promoDetailCheck(3,regRule,this.clearanceChecked);
        }
        bngnCheck()
        {
            let regRule = /buy one,? get one free|bogof|bogo fr|(^\d+|\s\d+) for \d+|(^\d+|\s\d+) for the price of \d+|buy \d+\s?,? get \d+ fr|buy \d+ [^,]+, get \d+ free/gi;
            this.promoDetailCheck(4,regRule,this.bngnChecked);
        }
        fgCheck()
        {
            //let regRule = /\s?free\s(gift|gifts)?(?!shipping|ship|shipment|delivery|click|download|trial|(\w+\s)*sample|\D+\s?\d+\+?\s(\w+\s)+(gift card|gift certificate|voucher|gift voucher))/gi;
let regRule = /\s?(free|complimentary)\s(gift|gifts)/gi;
            this.promoDetailCheck(5,regRule,this.fgChecked,()=>{
                this.checkBoxes[5].checked = true;
            },function(){},true);
        }
       
        fsamCheck()
        {
            let regRule = /\s?free\s(\w+\s)*sample/gi;
            this.promoDetailCheck(6,regRule,this.fgChecked,()=>{
                this.checkBoxes[6].checked = true;
            },function(){},true);
        }
        fdCheck()
        {
            let regRule = /(free)+(.*)?\sdownload/gi;
            this.promoDetailCheck(7,regRule,this.fdChecked);
        }
        ftCheck()
        {
            let regRule = /(free)+(.*)?\strial/gi;
            this.promoDetailCheck(8,regRule,this.ftChecked);
        }
        rebateCheck()
        {
            let regRule = /\srebate(\s|$)/gi;
            this.promoDetailCheck(9,regRule,this.rebateChecked);
        }
        rewardCheck()
        {
            let regRule = /\s(reward|bonus|cashback|cash back|point)s?(\s|$)/gi;
            this.promoDetailCheck(10,regRule,this.rewardChecked);
        }
        otherCheck()
        {
            let s = this.title.value;
            if(!s)
            {
                this.checkBoxes[11].checked = false;
                return;
            }
            let isAnyoneChecked = false;
            for(let i=0; i<this.len; i++)
            {
                if(i===11)continue;
                if(this.checkBoxes[i].checked)
                {
                    isAnyoneChecked = true;
                    this.checkBoxes[11].checked = false;
                    break;
                }
            }
            if(!isAnyoneChecked)
            {
                this.checkBoxes[11].checked = true;
            }
        }
        fromCheck()
        {
            let regRule = /\s*(from|(start|starting|starts)\s(from|at)|as low as|low to)\s(([^0-9\s]+)\s?(\d+|\d+\.\d+|(\d{1,3},)?\d{1,3},\d{3})(\s|$)|(\d+|\d+\.\d+|(\d{1,3},)?\d{1,3},\d{3})\s?([^0-9\s]+)(\s|$))/gi;
            this.promoDetailCheck(12,regRule,this.fromChecked,()=>{
                let currentCurrency = this.getCurrency((RegExp.$5||RegExp.$11).toLowerCase());
                let currentNumber = RegExp.$6||RegExp.$9;
                this.checkBoxes[12].checked = true;
                this.checkBoxes[12].parentElement.nextElementSibling.value = currentCurrency;
                this.checkBoxes[12].parentElement.nextElementSibling.nextElementSibling.value = currentNumber;
            },()=>{
                this.checkBoxes[12].parentElement.nextElementSibling.value = this.originalCurrency;
                this.checkBoxes[12].parentElement.nextElementSibling.nextElementSibling.value = '';
            },true);
        }
        initPromoCheck()
        {
                this.percentCheck();
                this.moneyCheck();
                this.clearanceCheck();
                this.bngnCheck();
                this.fgCheck();
                this.fsamCheck();
                this.fsCheck();
                this.fdCheck();
                this.ftCheck();
                this.rebateCheck();
                this.rewardCheck();
                this.fromCheck();
                this.otherCheck();
                this.sitewideCheck();
        }
        bindEvent()
        {
            this.title.addEventListener('input',(e)=>{
                this.initPromoCheck();
            },false);
        }
        exec()
        {
            this.bindEvent();
        }
    }

    let oAutoCheck = new AutoCheck(document.querySelectorAll("input[name='scope_apply']"),document.querySelector('#title'),document.querySelectorAll('#PromotionDetail > div > p > span > input[type=checkbox]'),document.querySelector("[name='money_type[money]']").value);
    oAutoCheck.exec();
    document.querySelector('input[value=SITE_WIDE]').addEventListener('click',function(){
        document.querySelector('input[value=sale_clearance]').checked = false;
    },false);
})();