Automatically Check Promotion Details

Automatically check promotion details according to title contents

Fra og med 22.09.2017. Se den nyeste version.

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         Automatically Check Promotion Details
// @version      2.8
// @description  Automatically check promotion details according to title contents
// @author       Tiger
// @match        http://tool-bcg.bwe.io/editor/promo.php?*
// @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)return;
            //|\d+%?\s(\w+\s)+\D+\s?\d+\+?(\s(?!on)|$|,)
            this.matchRes = s.match(/ on \D+\s?\d+\+?|sitewide|site wide|(whole|entire) site|(all|every|each|any|your|all of your|entire) (order|orders|purchase|purchases|product|products|merchandise|item|items)|(((when (spend|spending|you spend)|with (order|purchase|orders|purchases) of) \D+\s?\d+\+?)|with \D+\d+\+?(\s(order|purchase|orders|purchases))+)($|\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;
                }
            }

        }
        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 = /(free|save|get|enjoy)?((\$|€|£|Rs|RS\.|¥|₦|CHF|USD|CAD|GBP|POUND|RMB|AUD|INR|EUR|US$|CA$|AU$)\s?)?(\d+)\s?(\$|€|£|Rs|RS\.|¥|₦|CHF|USD|CAD|GBP|POUND|RMB|AUD|INR|EUR|US$|CA$|AU$)?\s(for|on|off|discount|discounts|saving|savings|(\w+\s)+(gift card|gift certificate|voucher|gift voucher))\s?/gi;
            this.promoDetailCheck(1,regRule,this.moneyChecked,()=>{
                let currency =  (RegExp.$3||RegExp.$5).toLowerCase();
                this.checkBoxes[1].parentElement.nextElementSibling.value = this.getCurrency(currency);
                this.checkBoxes[1].parentElement.nextElementSibling.nextElementSibling.value = RegExp.$4;
            },()=>{
                this.checkBoxes[1].parentElement.nextElementSibling.value = this.originalCurrency;
                this.checkBoxes[1].parentElement.nextElementSibling.nextElementSibling.value ='';
            });
        }
        percentCheck()
        {
            let regRule = /((save|get|enjoy)\s)?(\d+|\d+\.\d+)%\s(off|discount|discounts|saving|savings|on|for)\s?/gi;
            this.promoDetailCheck(2,regRule,this.percentChecked,()=>{
                this.checkBoxes[2].parentElement.nextElementSibling.value = RegExp.$3;
            },()=>{
                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)\s?/gi;
            this.promoDetailCheck(3,regRule,this.clearanceChecked);
        }
        bngnCheck()
        {
            let regRule = /bogof|bogo fr|(^\d+|\s\d+) for \d+|(^\d+|\s\d+) for the price of \d+|buy \d+\s?,? get \d+ fr/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;
            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|rewards|bonus|cashback|cash back)(\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+)(\s|$)|(\d+)\s?([^0-9\s]+)(\s|$))/gi;
            this.promoDetailCheck(12,regRule,this.fromChecked,()=>{
                let currentCurrency = (RegExp.$5||RegExp.$9).toLowerCase();
                let currentNumber = RegExp.$6||RegExp.$8;
                this.checkBoxes[12].checked = true;
                this.checkBoxes[12].parentElement.nextElementSibling.value = this.getCurrency(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();
})();