(function(){ var loadScript = function(url, callback) { var script = document.createElement("script"); script.type = "text/javascript"; if (script.readyState){ script.onreadystatechange = function(){ if (script.readyState == "loaded" || script.readyState == "complete") { script.onreadystatechange = null; callback(); } }; } else { script.onload = function() { callback(); }; } script.src = url; document.getElementsByTagName("head")[0].appendChild(script); }; var fix = function($) {var debug = m4u_debug;
var debug_txt = '';
var country_name = 'United States';
var country_code = 'US';
var country_code_lower = 'us';
var delay = '';
window.redirector_country_code = country_code;
window.redirector_country_code_lower = country_code_lower;
window.redirector_country_name = country_name;
if(window.m4u_test_mode)
{
country_name = window.test_country_name;
country_code = window.test_country_code;
country_code_lower = window.test_country_code_lower;
}
//var bots = ['Googlebot','Bingbot','Slurp','DuckDuckBot','Baiduspider','YandexBot','Sogou','Exabot','facebot','facebookexternalhit','ia_archiver'];
if(typeof m4u_exclude_ips != 'undefined')
{
var cur_ip = '18.119.102.108';
var ex_ips = m4u_exclude_ips.split(',');
if(ex_ips.indexOf(cur_ip)>=0)
return;
}
var css = '';
function closeDebugOut()
{
return '##NL##***** REDIRECTOR TEST MODE OUTPUT END *****##NL####NL##';
}
function setNoRedirectionCookie(v)
{
document.cookie = "m4u_no_redirection=true; path=/;";
}
function isSetNoRedirectionCookie()
{
var name = "m4u_no_redirection=";
var cki = document.cookie.split(';');
for(var i=0;i < cki.length; i++)
{
var var_name = cki[i].trim();
if(var_name.indexOf(name)==0)
return true;
}
return false;
}
function delNoRedirectionCookie()
{
document.cookie = "m4u_no_redirection=; path=/;expires=Thu, 01 Jan 1970 00:00:00 UTC";
}
function setCookie(v)
{
document.cookie = "m4u_executed="+v+"; path=/;";
}
function addExecutedRuleCountToCookie(rule_id)
{
var exec_rules = getExecutedRulesCookie();
if(exec_rules===undefined)
exec_rules = {};
if(exec_rules[rule_id]===undefined)
exec_rules[rule_id] = 0;
exec_rules[rule_id] += 1;
setCookie(JSON.stringify(exec_rules));
}
function getExecutedRuleCount(rule_id)
{
var exec_rules = getExecutedRulesCookie();
if(exec_rules===undefined)
return 0;
if(exec_rules[rule_id]===undefined)
return 0;
return exec_rules[rule_id];
}
function delCookie()
{
document.cookie = "m4u_executed=; path=/;expires=Thu, 01 Jan 1970 00:00:00 UTC";
}
function getExecutedRulesCookie()
{
var name = "m4u_executed=";
var cki = document.cookie.split(';');
for(var i=0;i < cki.length; i++)
{
var var_name = cki[i].trim();
if(var_name.indexOf(name)==0)
{
var var_arr = var_name.split(name);
if(var_arr[1].length>1)
{
var exec_rules = JSON.parse(var_arr[1]);
return exec_rules;
}
else
return {};
}
}
}
function displayBanner(txt)
{
var svg = '';
var tf = '';
$(document).ready(function(){
$('body').append(tf+'
');
$('#close').click(function(){
$('.m4u_popup').hide();
$('#close_float').hide();
$('.overlay_screen').hide();
$('body').css('overflow','auto');
});
// $('.overlay_screen').show();
$('#close_float').show();
$('.m4u_popup').show();
// $('body').css('overflow','hidden');
});
}
function replaceTags()
{
$('m4u_country_code').replaceWith(country_code);
$('m4u_country').replaceWith(country_name);
$('m4u_country_flag').replaceWith('' );
}
function formatBannerText(txt,qs)
{
txt = txt.replace(/@COUNTRY@/g, country_name );
txt = txt.replace(/@COUNTRY_CODE@/g, country_code_lower );
txt = txt.replace(/@COUNTRY_FLAG@/g, "" );
txt = txt.replace(/@COUNTRY_ICON@.png/g, country_code_lower+".svg" );
txt = txt.replace(/@QUERY_STRING@/g, qs );
return txt;
}
async function filterRules()
{
debug_txt = '***** REDIRECTOR TEST MODE OUTPUT BEGIN *****##NL####NL##';
var bl = 'en';
var cc = country_code;
var qs = location.href;
var ml_lang = null;
var pn = location.pathname;
pn = pn.replace(/(\/collections\/)(\S*?)(\/products\/)(\S*)/g, '$3$4');
if(typeof root_path !== 'undefined' && enabled_locales !== 'undefined')
{
ml_lang = root_path.substr(1);
if(ml_lang!='')
pn = pn.replace(root_path, '');
}
var target_url_with_locale = '';
console.log(pn);
var host = location.protocol+'//'+location.hostname;
var pn_arr = pn.split('/');
if(pn_arr[0]=='admin')
return;
if(window.m4u_test_mode)
{
qs = '';
bl = window.test_locale;
pn = window.test_path;
host = 'https://'+window.m4u_domain;
}
var bl_arr = bl.split('-');
var bl_lang = bl_arr[0];
var bl_cty = bl_arr[1];
var qs_arr = qs.split('?');
qs = '';
for(var xx=0;xx < qs_arr.length; xx++)
{
if(qs_arr[xx].indexOf('stop_redirect')==0)
{
console.log('Redirector: Stopping redirection for browser session.')
setNoRedirectionCookie();
}
else if(qs_arr[xx].indexOf('start_redirect')==0)
{
console.log('Redirector: Restarting redirection for browser session.')
delNoRedirectionCookie();
}
}
if(isSetNoRedirectionCookie())
return;
var source_params = new Array();
if(qs_arr.length > 1)
qs ='?'+qs_arr[1];
debug_txt +='VISITOR COUNTRY CODE: '+cc+'##NL##';
debug_txt +='VISITOR BROWSER LANGUAGE: '+bl+'##NL##';
debug_txt +='CURRENT URL: '+host+pn+'##NL##';
debug_txt +='QUERY STRING: '+qs+'##NL##';
var rules_source_url_filtered = new Array();
var rule_ids = Object.keys(m4u_redirects);
/**
* Filter source URL
**/
for(var i=0;i< rule_ids.length;i++)
{
var r = m4u_redirects[rule_ids[i]];
r = JSON.parse(r);
var is_product_redirect = false;
if(r['origin_url']=='PRODUCTS' && pn.indexOf('/products/') >= 0)
is_product_redirect = true;
if(r['origin_url']==pn || r['origin_url'] == '*' || is_product_redirect)
rules_source_url_filtered[rule_ids[i]] = r;
}
/**
* Filter country
**/
var rf_keys = Object.keys(rules_source_url_filtered);
var rules_country_filtered = new Array();
for(var j=0; j < rf_keys.length;j++)
{
var r = rules_source_url_filtered[rf_keys[j]];
if(r['countries'] =='*' || r['countries'].indexOf(cc) >=0 )
rules_country_filtered[rf_keys[j]] = r;
}
/**
* Filter locale
**/
var rf_keys = Object.keys(rules_country_filtered);
var rules_locales_filtered = new Array();
for(var j=0; j < rf_keys.length;j++)
{
var r = rules_country_filtered[rf_keys[j]];
var loc = r['locale'].toLowerCase();
if(loc =='*' || loc==bl || loc == bl_lang )
rules_locales_filtered[rf_keys[j]] = r;
}
/**
* BEST MATCH
**/
var rf_keys = Object.keys(rules_locales_filtered);
if(rf_keys.length == 0)
{
debug_txt += 'NO MATCHING RULES FOUND.';
if(debug=='true')
formatOutput(debug_txt+closeDebugOut());
return;
}
debug_txt +='NUMBER OF MATCHING RULES: '+rf_keys.length+'##NL##';
var primary = secondary = tertiary = quarteriary = rest = '';
for(var j=0; j < rf_keys.length;j++)
{
var r = rules_locales_filtered[rf_keys[j]];
var loc = r['locale'].toLowerCase();
var cty = r['countries'];
var src = r['origin_url'];
if(src !='*' && cty.indexOf('*') < 0 && loc.indexOf('-') )
{
primary = rf_keys[j];
break;
}
if(cty.indexOf('*') < 0 && loc.indexOf('-') >= 0 )
secondary = rf_keys[j];
if(cty.indexOf('*') < 0 && loc.indexOf('*') < 0 )
tertiary = rf_keys[j];
if(cty.indexOf('*') < 0 )
quarteriary = rf_keys[j];
else
rest = rf_keys[j];
}
var to_exec = '';
if(primary!='')
to_exec = primary;
else if(secondary!='')
to_exec = secondary;
else if(tertiary!='')
to_exec = tertiary;
else if(quarteriary!='')
to_exec = quarteriary;
else if(rest!='')
to_exec = rest;
var m = rules_locales_filtered[to_exec];
var mt = rtype = m['type'];
if(mt=='ml_switch_language')
mt ='switch language';
var exec = m['execution'];
var btxt = '';
if(mt=='redirect')
{
var t_url = m['target_url'];
if(t_url.indexOf('{PRODUCT}')>=0)
{
var t_arr = pn.split('/products/');
if(t_arr.length==2)
t_url = t_url.replace('{PRODUCT}',t_arr[1]);
} else if(t_url.indexOf('/*')>=0)
{
t_url = t_url.replace('/*',pn);
}
if(t_url.indexOf('://')< 0)
{
console.log(ml_lang);
if(ml_lang!='' && ml_lang!=null)
target_url_with_locale=host+'/'+ml_lang+t_url;
else
target_url_with_locale=host + t_url;
t_url = target_url_with_locale;
}
var strip_t_url = stripURL(t_url);
var new_qs = createParamSupersetQS(location.href,t_url);
mt += ' to: '+strip_t_url+new_qs+'##NL##';
debug_txt += 'NEW QUERY STRING: '+new_qs+'##NL##';
if( (strip_t_url==host+pn) && (qs==new_qs) )
{
debug_txt += 'SOURCE URL MATCHES TARGET URL, EXITING.';
if(debug=='true')
formatOutput(debug_txt+closeDebugOut());
return;
}
}
else if(mt=='switch language')
mt += ' to: '+m['language']+'##NL##';
else
{
mt= 'show '+mt+'##NL##';
var ftxt = formatBannerText(m['banner_txt'],new_qs);
btxt = ftxt+'##NL####NL##';
}
debug_txt +='BEST MATCH: '+ mt+'##NL##';
if(btxt!='')
debug_txt +=btxt;
debug_txt +='THIS RULE WILL BE EXECUTED ';
var rule_cnt = getExecutedRuleCount(to_exec);
var needs_execution = false;
if(exec == '0')
{
needs_execution = true;
debug_txt+='ON EVERY VISIT.'+'##NL##';
if(m['type']=='ml_switch_language')
debug_txt+='WARNING: CUSTOMERS WON\'T BE ABLE TO MANUALLY SELECT A LANGUAGE!'+'##NL##';
}
else
{
if(rule_cnt < 1)
needs_execution = true;
debug_txt += 'ONCE.'+'##NL##';
if(debug!='true')
addExecutedRuleCountToCookie(to_exec);
}
if(needs_execution && debug!='true')
{
//const myTimeout = setTimeout(myGreeting, 5000);
if(delay!='')
await sleep(delay);
addExecutedRuleCountToCookie(to_exec);
if(rtype=='banner')
displayBanner(ftxt);
else if(rtype=='redirect')
{
console.log('target = '+strip_t_url+new_qs);
location.href= strip_t_url+new_qs;
}
else if(rtype=='ml_switch_language')
getLang(m['language']);
}
debug_txt += 'RULE HAS BEEN EXECUTED '+rule_cnt+ ' TIMES.'+'##NL####NL##';
debug_txt += closeDebugOut();
if(debug=='true')
formatOutput(debug_txt);
}
const sleep = (delay) => new Promise((resolve) => setTimeout(resolve, delay))
function formatOutput(txt)
{
var d_arr = txt.split('##NL##');
var con_out = d_arr.join('\n\r');
var html_out = d_arr.join('
');
console.log(con_out);
$('#redirector_output').html(html_out);
$('#redirector_output').show();
}
function extractRequestParams(url)
{
var params = new Array();
var qs = url.split('?');
if(qs.length>1)
params = qs[1].split('&');
return params;
}
function createParamSupersetQS(url1,url2)
{
var p = new Array();
var p1 = extractRequestParams(url1);
var p2 = extractRequestParams(url2);
if($('#redirector_output').length)
p1 = new Array();
for(var i=0;i< p1.length;i++)
p[p1[i]] = '';
for(var i=0;i< p2.length;i++)
p[p2[i]] = '';
p= Object.keys(p);
if(p.length==0)
return '';
else
{
var qs = p.join('&');
return '?'+qs;
}
}
function stripURL(url)
{
var url_arr = url.split('?');
url = url_arr[0];
url_arr = url.split('#');
url = url_arr[0];
return url;
}
function redirect(url,debug)
{
var qs = location.href;
var qs_arr = qs.split('?');
var wc_index = url.indexOf('/*');
if(wc_index == url.length-2)
{
url = url.substring(0,wc_index);
url += location.pathname;
}
if(qs_arr.length>1)
{
var params = qs_arr[1].split('&');
var new_qs_arr = new Array();
for(var i=0;i< params.length;i++)
{
if(params[i].indexOf('lang=')!=0)
new_qs_arr.push(params[i]);
}
var new_qs = new_qs_arr.join('&');
url+='?'+new_qs;
}
location.href=url;
}
function switchLang(lang)
{
var new_ml =false;
var ml_dom = {};
if(typeof ml_domains != 'undefined')
ml_dom = ml_domains;
if(typeof ml_domains_mf != 'undefined')
ml_dom = ml_domains_mf;
var qs = createParamSupersetQS(location.href,'');
var dest = '';
var path = location.pathname;
var update_url = '';
if(typeof root_path !== 'undefined' && enabled_locales !== 'undefined')
{
new_ml =true;
if(root_path=='/')
path = req_path;
else
path = path.replace(root_path, '');
if(lang!=def_loc)
path='/'+lang+path;
}
if(ml_dom[lang]!=null)
{
update_url = 'https://'+ml_dom[lang];
dest ='https://'+ml_dom[lang]+path+qs;
}
else
{
update_url = location.protocol+'//'+location.host;
dest =path+qs;
}
update_url = update_url+'/cart/update?attributes[ml_lang]='+lang;
if(new_ml)
{
location.href = dest;
return;
}
$('body').append('');
$('#ml_cart_update').attr('src', update_url)
.on('error', function() {
location.href = dest;
});
return;
}
function getLang(target)
{
if(typeof root_path !== 'undefined' && enabled_locales !== 'undefined')
{
var ml_lang = root_path.substr(1);
if(ml_lang=='')
ml_lang=def_loc;
if(target!=ml_lang)
{
if(debug=='true')
console.log('SWITCHING LANGUAGE TO '+ target+'.');
switchLang(target);
}
else
{
if(debug=='true')
console.log('CURRENT LANGUAGE IS ALREADY '+ target+', EXITING.'+closeDebugOut());
return;
}
return;
}
$.ajax({
url : '/cart.js',
type : 'GET',
crossDomain: false,
dataType:'json',
success : function(data)
{
if(typeof ml_def_language_mf !== 'undefined')
def_loc = ml_def_language_mf;
var ml_lang = data['attributes']['ml_lang'];
if(ml_lang==null)
ml_lang=def_loc;
if(target!=ml_lang)
{
if(debug=='true')
console.log('SWITCHING LANGUAGE TO '+ target+'.');
switchLang(target);
}
else
{
if(debug=='true')
console.log('CURRENT LANGUAGE IS ALREADY '+ target+', EXITING.'+closeDebugOut());
return;
}
},
error : function(request,error)
{
}
});
}
/*
var ua = 'Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)';
for(var i=0;i< bots.length;i++)
{
if(ua.indexOf(bots[i])>=0)
return;
}
*/
if(!$('style[redirector]').length)
$('head').append(css);
filterRules();
$(document).ready(function(){
replaceTags();
var country_located_event = new CustomEvent('RedirectorCountryLocated',
{
detail:
{
'country_code' : country_code,
'country_code_lower' : country_code_lower,
'country_name' :country_name
}
});
document.dispatchEvent(country_located_event);
}
);
};
if ((typeof jQuery === 'undefined') || (parseFloat(jQuery.fn.jquery) < 3))
{
loadScript('//modules4u.biz/shopify/js/jquery/jquery-3.2.1.min.js', function() {
jQuery32 = jQuery.noConflict(true);
fix(jQuery32);
}
);
}
else
{ fix(jQuery); }
})();