function GCB_loadScript(url, success) {
     var script = document.createElement('script');
     script.src = url;
     var head = document.getElementsByTagName('head')[0],
     done = false;
     head.appendChild(script);
     script.onload = script.onreadystatechange = function() {
        if (!done && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete')) {
             done = true;
             success();
             script.onload = script.onreadystatechange = null;
             head.removeChild(script);
        }
    };
}

function GCB_disableRightClick() {
    document.addEventListener('contextmenu', function(event) {
        event.preventDefault();
    });
}

function GCB_main() {
    console.log('%c------ Blocky: Fraud Country Blocker by Effective Apps is Initializing ------', 'color: cyan');
    console.log('%c------ Contact us at support@effectify.co for help and questions about the app ------', 'color: cyan');
    if (window.GCB_SCRIPT_INJECTED === undefined) {
        window.isLs = GCB_isLSAvailable();
        window.GCB_SCRIPT_INJECTED = true;
        if ('3' === '7' && null === GCB_getLsWithExpiry('gcb_payg_i')) {
            jQuery.get('https://app.blocky-app.com/ri/c625c6c287cc11eeb892aa6d36f34195.js');
            GCB_setLsWithExpiry('gcb_payg_i', 'true', 604800000);
        }

        window.GCB_BLOCKED_COUNTRIES = 'AF,AX,AL,DZ,AS,AD,AO,AI,AQ,AG,AR,AM,AW,AU,AT,AZ,BS,BH,BD,BB,BY,BE,BZ,BJ,BM,BT,BO,BA,BW,BV,BR,IO,BN,BG,BF,BI,KH,CM,CV,KY,CF,TD,CL,CN,CX,CC,CO,KM,CG,CD,CK,CR,CI,HR,CU,CY,CZ,DK,DJ,DM,DO,EC,EG,SV,GQ,ER,EE,ET,FK,FO,FJ,FI,FR,GF,PF,TF,GA,GM,GE,DE,GH,GI,GR,GL,GD,GP,GU,GT,GG,GN,GW,GY,HT,HM,VA,HN,HK,HU,IS,IN,ID,IR,IQ,IM,IL,IT,JM,JP,JE,JO,KZ,KE,KI,KP,KR,KW,KG,LA,LV,LB,LS,LR,LY,LI,LT,LU,MO,MK,MG,MW,MY,MV,ML,MT,MH,MQ,MR,MU,YT,MX,FM,MD,MC,MN,MS,MA,MZ,MM,NA,NR,NP,NL,AN,NC,NZ,NI,NE,NG,NU,NF,MP,NO,OM,PK,PW,PS,PA,PG,PY,PE,PH,PN,PL,PT,QA,RE,RO,RU,RW,SH,KN,LC,PM,VC,WS,SM,ST,SA,SN,CS,RS,ME,SC,SL,SG,SK,SI,SB,SO,ZA,GS,ES,LK,SD,SR,SJ,SZ,SE,CH,SY,TW,TJ,TZ,TH,TL,TG,TK,TO,TT,TN,TR,TM,TC,TV,UG,UA,AE,UZ,VU,VE,VN,VG,WF,EH,YE,ZM,ZW';
        window.GCB_BLOCKED_IPS = '';
        window.GCB_WHITELISTED_IPS = '40.94.*.*,159.223.180.5,3.145.136.100,13.59.221.251,52.53.241.115,107.181.178.72,192.168.0.16,107.181.178.80,10.0.0.184,192.168.0.116,192.168.0.107,192.168.0.1,100.126.8.169,174.249.178.135,104.160.56.187,207.244.127.144,173.166.164.121,161.129.143.75,170.130.183.150,20.230.74.51,193.36.224.182,192.168.1.2,167.71.116.8,149.88.16.35,18.144.38.65,208.127.186.29,136.22.20.94,1.158.145.28,64.23.180.11,18.217.230.205,99.13.46.20,49.217.127.203,54.241.188.154,104.28.203.57,173.239.218.216,45.131.194.21,137.184.164.199,209.249.184.134,44.204.104.64,81.17.123.181,208.70.150.252,68.105.28.11,136.144.43.191,185.141.119.101,193.239.87.140,186.151.92.248,149.28.115.120,212.102.40.24,107.170.38.12,74.37.252.247,101.188.67.134,15.181.181.231,54.215.101.225,18.216.204.157,24.59.177.251,174.128.181.56,130.105.201.26,198.203.28.121,137.220.63.53,37.19.221.181,68.66.114.190,18.88.79.124,162.120.160.81,73.114.242.40,10.0.0.234,18.117.249.10,10.170.164.222,149.102.254.69,104.28.50.153,121.214.107.220,15.181.199.133,190.234.23.179,185.141.119.63,208.67.7.147,147.235.206.34,139.177.147.136,165.22.225.248,66.115.181.189,165.22.178.186,174.128.181.19,202.90.91.17,162.120.136.178,54.158.201.28,202.229.102.174,2.56.191.7,172.59.69.62,64.31.3.198,134.122.8.114,143.198.40.244,64.31.43.149,199.87.208.228,72.90.137.4,64.42.180.27,54.177.5.87,86.48.46.59,185.169.0.71,208.115.247.243,54.176.39.123,50.84.137.26,15.181.177.121,143.244.44.169,138.84.56.217,74.249.26.124,255.255.255.0,177.93.27.201,193.187.131.161,107.122.93.41,18.116.25.224,67.84.175.78,172.245.96.168,212.102.33.180,185.255.130.162,207.244.80.25,212.102.33.75,173.239.218.166,15.181.199.201,18.144.14.104,3.14.133.71,50.158.41.66,198.54.133.184,54.241.66.199,143.44.128.32,122.52.17.11,98.193.120.131,185.205.247.19,208.115.228.153,208.115.228.150,212.102.33.211,173.169.26.190,146.70.165.13,66.68.8.48,66.115.181.180,64.187.128.86,10.252.0.198,149.22.81.153,103.63.158.210,174.128.181.135,76.70.105.144,78.157.218.117,104.28.85.159,104.28.111.172,66.115.181.108,104.28.111.173,18.213.118.106,54.219.0.182,23.227.148.141,136.226.64.111,185.238.231.106,72.128.32.8,167.71.16.143,52.55.106.56,10.0.0.10,162.253.129.75,24.162.145.111,188.210.38.86,174.128.182.120,187.141.65.251,100.126.17.157,64.42.180.235,3.141.44.167,104.28.58.17,3.101.75.121,107.175.102.144,193.43.135.27,165.232.146.248,3.15.235.11,77.74.195.60,45.8.19.217,64.31.25.120,154.47.25.108,138.199.42.156,162.120.165.1,153.156.214.98,54.224.85.83,74.63.235.233,151.19.87.57,3.101.30.106,67.207.86.80,54.145.97.215,142.215.114.17,3.17.154.75,73.195.200.14,139.60.77.67,67.20.1.242,13.41.64.41,162.120.138.193,180.191.164.194,92.119.177.22,150.220.4.32,69.67.150.204,69.162.73.5,174.128.181.164,104.28.58.2,66.115.181.69,104.28.50.50,15.181.145.235,3.128.186.242,23.116.225.152,153.167.32.18,173.205.180.121,72.41.0.11,198.54.132.135,67.213.103.117,94.140.8.153,163.123.194.44,88.216.90.65,75.230.9.103,104.28.50.146,64.42.180.18,189.202.251.134,37.19.200.142,45.86.211.0,45.141.153.37,198.37.111.11,104.140.22.198,174.128.182.183,13.56.58.91,174.128.182.198,45.86.210.144,73.172.227.21,23.26.254.147,45.92.229.193,64.31.3.152,54.213.248.201,3.235.85.253,122.150.205.241,104.28.196.77,104.28.39.23,2.56.191.254,137.220.57.102,99.175.90.192,74.115.7.24,64.31.3.56,96.253.119.221,5.180.61.77,149.36.48.139,67.55.94.85,172.56.196.18,67.6.172.158,174.107.232.16,174.219.244.140,173.239.213.31,31.32.213.213,206.217.142.232,104.28.50.213,130.195.219.51,181.209.144.183,104.28.50.208,172.58.164.240,68.43.127.252,204.116.169.148,174.128.182.22,104.28.32.245,45.76.19.74,104.28.50.185,54.193.9.48,104.28.32.197,98.47.36.255,94.140.11.223,174.175.56.212,104.28.32.149,104.28.94.207,35.148.44.147,71.93.128.7,3.87.156.184,45.42.219.21,104.28.94.189,104.28.50.183,71.38.86.220,54.236.58.111,187.210.107.184,76.253.161.73,3.133.150.141,104.28.55.8,104.28.50.176,174.128.182.52,54.201.11.224,186.151.92.142,212.102.40.139';
        window.EFFECTIVE_APPS_GCB_BLOCKED_MESSAGE = `<head></head><body style="background-color: #FFFFFF;"><p style="text-align: center;"><span style="font-size: 36px;">Device Location Blocked</span></p>
<p style="text-align: center;"><span style="font-size: 18px;">Sorry, you cannot visit our online shop from your device's current location. It is either not in a country we ship our products, or your device's IP is blocked due to the use of a proxy or VPN service.&nbsp;</span></p>
<p style="text-align: center;"><span style="font-size: 18px;">Online shoppers residing in the countries we ship our products -- United States, Canada, and United Kingdom --&nbsp; may <a href="https://forms.gle/nGFJTXRrBtcCvVKg7" target="_blank" rel="noopener">request the addition of their device's IP</a> to be able to shop our online store.</span></p></body>`;
        window.EFFECTIVE_APPS_GCB_IB_BLOCKED = false;
        window.EFFECTIVE_APPS_GCB_CB_BLOCKED = false;

        if ('False' === 'True') {
            GCB_disableRightClick();
        }

        if (location.href.indexOf('gcb_preview=true') > -1) {
            blockPage(false);
            return;
        }

        if (location.href.indexOf('gcb_override=true') > -1 && 'True' === 'True') {
            GCB_setLsWithExpiry('gcb_override', 'true', 300000);
        }

        if ('True' === 'True' && (GCB_getLsWithExpiry('gcb_override') === 'true' || (typeof Shopify !== 'undefined' && Shopify.designMode === true) || document.getElementById('admin-bar-iframe') !== null)) {
            return;
        }

        if ('False' === 'True') {
            GCB_superBlocker();
        }

        if ('True' === 'True' || 'False' === 'True') {
            try {
                GCB_getGeolocationCountry(runCBCRLogic);
            }
            catch(err) {
            }
        }
        else {
            GCB_getGeolocationCountry(function(geolocationCountry, ipAddress) { GCB_lIp(ipAddress, geolocationCountry, false); })
        }

        if ('False' === 'True') {
            try {
                GCB_getIPAddress(runIBLogic);
            }
            catch(err) {

            }
        }
        else {
            GCB_getIPAddress(function(ipAddress, geolocationCountry) { GCB_lIp(ipAddress, geolocationCountry, false); })
        }
    }
}

function GCB_rCr(callback) {
    jQuery.get('https://app.blocky-app.com/rcr/c625c6c287cc11eeb892aa6d36f34195');
    callback();
}

function GCB_rCb() {
    jQuery.get('https://app.blocky-app.com/rcb/c625c6c287cc11eeb892aa6d36f34195');
}

function GCB_rIb() {
    jQuery.get('https://app.blocky-app.com/rib/c625c6c287cc11eeb892aa6d36f34195');
}


function blockPage(saveLS=true) {
    window.GCB_BLOCKED_PAGE_INTERVAL = setInterval(function() {
        document.querySelector("html").innerHTML = window.EFFECTIVE_APPS_GCB_BLOCKED_MESSAGE;
    }, 1000);
    if (saveLS) {
        GCB_setLsWithExpiry('effective_apps_gcb_blocked', 'true', 604800000);
    }
}

function runCBCRLogic(geolocationCountry, ipAddress) {
    if ('False' === 'True') {
        let redirectionRules = JSON.parse('[]');
        for (var i=0; i < redirectionRules.length; i++) {
            var rule = redirectionRules[i];
            if (rule['country'] === geolocationCountry) {
                if (location.href.indexOf(rule['url']) === -1) {
                    GCB_rCr(function() {
                        location.href = rule['url'];
                    });
                }
                break;
            }
        }
    }

    if ('True' === 'True') {
        let blockedCountries = window.GCB_BLOCKED_COUNTRIES.split(',');
        let whitelistedIpAddresses = window.GCB_WHITELISTED_IPS.split(',');
        if (whitelistedIpAddresses.indexOf(ipAddress) > -1) {
            GCB_lIp(ipAddress, geolocationCountry, false);
            return;
        }
        else if (window.GCB_WHITELISTED_IPS.indexOf("*") > -1) {
            for (let whitelistedIpPattern of whitelistedIpAddresses) {
                var wlIpPattern = whitelistedIpPattern.replaceAll('*', '(\\d{1,3})').replaceAll('.', '\\.');
                var wlIpRegex = new RegExp(wlIpPattern, 'gm');
                if (ipAddress.match(wlIpRegex) !== null) {
                    GCB_lIp(ipAddress, geolocationCountry, false);
                    return;
                }
            }
        }

        if (blockedCountries.indexOf(geolocationCountry) > -1 && window.EFFECTIVE_APPS_GCB_IB_BLOCKED === false) {
            window.EFFECTIVE_APPS_GCB_CB_BLOCKED = true;
            GCB_rCb();
            blockPage();
            GCB_lIp(ipAddress, geolocationCountry, true);
            return;
        }
        else {
            GCB_lIp(ipAddress, geolocationCountry, false);
            return;
        }
    }
}

function GCB_lIp(ip_address, country_code, was_blocked) {
    if ('True' === 'True' && GCB_getLsWithExpiry('already_reported') === null) {
        browser = GCB_getBrowserType();
        jQuery.post('https://app.blocky-app.com/li/c625c6c287cc11eeb892aa6d36f34195', {"browser": browser, "ip_address": ip_address, "country_code": country_code, "was_blocked": was_blocked });
        GCB_setLsWithExpiry('already_reported', 'true', 3600000);
    }
}

function runIBLogic(ipAddress, geolocationCountry) {
    let blockedIpAddresses = window.GCB_BLOCKED_IPS.split(',');
    let whitelistedIpAddresses = window.GCB_WHITELISTED_IPS.split(',');
    if (whitelistedIpAddresses.indexOf(ipAddress) > -1) {
        GCB_lIp(ipAddress, geolocationCountry, false);
        return;
    }
    else if (window.GCB_WHITELISTED_IPS.indexOf("*") > -1) {
        for (let whitelistedIpPattern of whitelistedIpAddresses) {
            var wlIpPattern = whitelistedIpPattern.replaceAll('*', '(\\d{1,3})').replaceAll('.', '\\.');
            var wlIpRegex = new RegExp(wlIpPattern, 'gm');
            if (ipAddress.match(wlIpRegex) !== null) {
                GCB_lIp(ipAddress, geolocationCountry, false);
                return;
            }
        }
    }

    if (blockedIpAddresses.indexOf(ipAddress) > -1 && window.EFFECTIVE_APPS_GCB_CB_BLOCKED === false) {
        window.EFFECTIVE_APPS_GCB_IB_BLOCKED = true;
        GCB_rIb();
        GCB_lIp(ipAddress, geolocationCountry, true);
        blockPage();
        return;
    }
    else if (window.GCB_BLOCKED_IPS.indexOf("*") > -1 && window.EFFECTIVE_APPS_GCB_CB_BLOCKED === false) {
        for (let blockedIpPattern of blockedIpAddresses) {
            var bIpPattern = blockedIpPattern.replaceAll('*', '(\\d{1,3})').replaceAll('.', '\\.');
            var blockedIpRegex = new RegExp(bIpPattern, 'gm');
            if (ipAddress.match(blockedIpRegex) !== null) {
                GCB_rIb();
                GCB_lIp(ipAddress, geolocationCountry, true);
                blockPage();
                return;
            }
        }

        GCB_lIp(ipAddress, geolocationCountry, false);
    }
    else {
        GCB_lIp(ipAddress, geolocationCountry, false);
    }
}

function GCB_setLsWithExpiry(key, value, ttl) {
    if (window.isLs === false) {
        return false;
    }

	const now = new Date()
	const item = {
		value: value,
		expiry: now.getTime() + ttl,
	}
	localStorage.setItem(key, JSON.stringify(item))
}

function GCB_getLsWithExpiry(key) {
    if (window.isLs === false) {
        return null;
    }

	const itemStr = localStorage.getItem(key)
	if (!itemStr) {
		return null
	}

	const item = JSON.parse(itemStr)
	const now = new Date()
	if (now.getTime() > item.expiry) {
		localStorage.removeItem(key)
		return null
	}

	return item.value
}

function GCB_superBlocker() {
    const geolocationServices = ['https://ipwhois.app/json/', 'https://ip-api.io/json', 'https://ipapi.co/json', 'https://get.geojs.io/v1/ip/geo.json'];
    let intervalCounter = 0;
    window.GCB_COUNTRY_SUPER_BLOCKER_INTERVAL = setInterval(function() {
        for (let service of geolocationServices) {
            try {
                if (['https://ip-api.io/json', 'https://ipapi.co/json'].indexOf(service) > -1 && intervalCounter > 2) {
                    continue;
                }

                jQuery.getJSON(service).then(function(response) {
                    const countryCode = response.country_code;
                    const ipAddress = response.ip;
                    if (ipAddress === undefined || countryCode === undefined) {
                        return;
                    }

                    if ('True' === 'True') {
                        runCBCRLogic(countryCode, ipAddress);
                    }
                    if ('False' === 'True') {
                        runIBLogic(ipAddress, countryCode);
                    }
                    if ('True' === 'True') {
                        try {
                            jQuery.getJSON('https://app.blocky-app.com/cini/').then(function(response) {
                                if (response.result === true) {
                                    document.querySelector("html").innerHTML = window.EFFECTIVE_APPS_GCB_BLOCKED_MESSAGE;
                                }
                            });
                            jQuery.getJSON('https://app.blocky-app.com/ci/' + ipAddress).then(function(response) {
                                if (response.result === true) {
                                    document.querySelector("html").innerHTML = window.EFFECTIVE_APPS_GCB_BLOCKED_MESSAGE;
                                }
                            });
                        }
                        catch(err) {
                        }
                    }
                });
            }
            catch(err) {
                continue;
            }
        }
    }, 1000);
}

function GCB_getGeolocationCountry(callback) {
    jQuery.getJSON('https://ipv4.geojs.io/v1/ip/geo.json', function(result) {
        callback(result.country_code, result.ip);
    });
}

function GCB_getIPAddress(callback) {
    jQuery.getJSON('https://ipv4.geojs.io/v1/ip/geo.json', function(result) {
        callback(result.ip, result.country_code);
    });
}

function GCB_getBrowserType() {
    try {
        const userAgent = navigator.userAgent.toLowerCase();
        if (userAgent.includes("chrome") && !userAgent.includes("edg") && !userAgent.includes("opr") && !userAgent.includes("brave")) {
            if (navigator.brave) {
                return "b";
            }

            return "c";
        } else if (userAgent.includes("brave")) {
            return "b";
        } else if (userAgent.includes("firefox")) {
            return "ff";
        } else if (userAgent.includes("safari") && !userAgent.includes("chrome") && !userAgent.includes("chromium")) {
            return "s";
        } else if (userAgent.includes("edg")) {
            return "e";
        } else if (userAgent.includes("opr") || userAgent.includes("opera")) {
            return "o";
        } else if (userAgent.includes("trident") || userAgent.includes("msie")) {
            return "ie";
        }

        return "ot";
    }
    catch(err) {
        return "ot";
    }
}

function GCB_isLSAvailable() {
    var test = 'test';
    try {
        localStorage.setItem(test, test);
        localStorage.removeItem(test);
        return true;
    } catch(e) {
        return false;
    }
}

if (typeof jQuery === 'undefined') {
    GCB_loadScript('https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.min.js', GCB_main);
}
else {
    GCB_main();
}