//网站的host
window.pre_host = 'https://www.5ufclub.com';

/** @param {*} raw @param {*} fallback */
window.safeJsonParse = function (raw, fallback) {
    if (raw == null || raw === '') {
        return fallback;
    }
    if (typeof raw !== 'string') {
        return fallback;
    }
    try {
        return JSON.parse(raw);
    } catch (e) {
        return fallback;
    }
};

//流量超过以后，或者状态不可用使用此代理服务器
window.disbaled_server = 'black.5uf99.com:8443';

//site同步间隔，单位为:s
window.site_sync_limit = 30;

//账号检测时间间隔，单位为:s
window.auth_limit = 30;

//启动后多久检查新版本，单位为:s
window.update_limit = 120;


//用于获取网站域名的url地址
window.find_host = window.pre_host+'/data/crx/crx_host.txt';
window.find_host_2 = window.pre_host+'/data/crx/crx_host_2.txt';
//ajax超时时间
window.ajax_timeout = 5;

//切换服务器后，用于测试的url
window.test_url = 'http://www.google.com';

//统计代码
window.trace_code = '';

//特殊域名后缀
window.special_domain_suffix = ['com.cn', 'com.co.jp', 'co.jp', 'net.cn', 'org.cn', 'gov.cn','edu.cn', "tw.cn", "hn.cn", "hl.cn", "sh.cn", "hi.cn", "cp.cn", "hk.cn", "gx.cn", "sx.cn", "mo.cn", "ln.cn", "nm.cn", "ah.cn", "qh.cn", "hb.cn", "xj.cn", "gz.cn", "jl.cn", "nx.cn", "gd.cn", "sn.cn", "sc.cn", "he.cn", "zj.cn", "ha.cn", "tj.cn", "bj.cn", "sd.cn", "fj.cn", "yn.cn", "gs.cn", "xz.cn", "jx.cn", "ac.cn", "com.cn", "net.cn", "org.cn", "js.cn", "cn.com", "co.com", "nom.es", "com.es", "org.es", "com.hk", "com.im", "co.im", "net.im", "org.im", "plc.co.im", "net.in", "co.in", "co.jp", "or.kr", "co.kr", "org.lc", "co.lc", "com.lc", "net.lc", "in.net", "co.nz", "org.so", "net.so", "com.so", "com.tw", "org.tw", "co.uk", "gov.cn", "com.ph"];



//chrome.proxy.settings.clear({ scope: "regular" });
function set_proxy () {
    var block_list = localStorage.getItem('5uf_block_list');
    block_list = JSON.parse(block_list);

    var block_list_system = localStorage.getItem('5uf_block_list_system');
    block_list_system = JSON.parse(block_list_system);

    var white_list = localStorage.getItem('5uf_white_list');
    white_list = JSON.parse(white_list);

    var white_list_system = localStorage.getItem('5uf_white_list_system');
    white_list_system = JSON.parse(white_list_system);

    var proxy_mode = localStorage.getItem('proxy_mode');

    var user_status = localStorage.getItem('5uf_user_status');
    if (proxy_mode != 'close' && user_status != 'Password') {
        var pac =
            'dnsDomainIs=function(h,d){return h.indexOf(d,h.length-d.length)!==-1&&(d.length===h.length||h.indexOf("."+d,h.length-("."+d).length)!==-1)};'+
            "var FindProxyForURL = function(url, host){"+
            "host=host.toLowerCase();"+
            "var white_list_system = "+localStorage.getItem('5uf_white_list_system')+';'+
            "for(i=0;i<white_list_system.length;i++){"+
            "if (dnsDomainIs(host,white_list_system[i])){"+
            "return 'DIRECT';"+
            "}"+
            "}"+
            "return '"+window.disbaled_server+"';"+
            "}";
        //alert(pac);
        var config = {
            mode: "pac_script",
            pacScript: {
                data: pac
            }
        }
        chrome.proxy.settings.set({value: config, scope: 'regular'}, function(){});
        return;

    } else if (localStorage.getItem('5uf_server_selected_host') == null || localStorage.getItem('5uf_server_selected_host') == null) {
        var pac =
            "var FindProxyForURL = function(url, host){"+
            +"return 'DIRECT';"+
            "}";
        var config = {
            mode: "pac_script",
            pacScript: {
                data: pac
            }
        };
        chrome.proxy.settings.set({value: config, scope: 'regular'}, function(){});
        return ;
    } else if (proxy_mode == 'all') {
        //alert('mode : all');
        var pac =
            'dnsDomainIs=function(h,d){return h.indexOf(d,h.length-d.length)!==-1&&(d.length===h.length||h.indexOf("."+d,h.length-("."+d).length)!==-1)};'+
            "var FindProxyForURL = function(url, host){"+
            "host=host.toLowerCase();"+
            "var white_list = "+localStorage.getItem('5uf_white_list')+';'+
            "var white_list_system = "+localStorage.getItem('5uf_white_list_system')+';'+
            "for(i=0;i<white_list.length;i++){"+
            "if (dnsDomainIs(host,white_list[i])){"+
            "return 'DIRECT';"+
            "}"+
            "}"+
            "for(i=0;i<white_list_system.length;i++){"+
            "if (dnsDomainIs(host,white_list_system[i])){"+
            "return 'DIRECT';"+
            "}"+
            "}"+
            "return 'HTTPS "+localStorage.getItem('5uf_server_selected_host')+":"+(localStorage.getItem('5uf_server_selected_port') != 0 ? localStorage.getItem('5uf_server_selected_port') : '443')+"';"+
            "}";
        //alert(pac);
        var config = {
            mode: "pac_script",
            pacScript: {
                data: pac
            }
        };
        chrome.proxy.settings.set({value: config, scope: 'regular'}, function(){});
        return ;
    } else if (proxy_mode == 'close') {
        chrome.proxy.settings.set({ value: { mode: 'system' }, scope: 'regular' }, () => {
            if (chrome.runtime.lastError) {
                chrome.proxy.settings.clear({ scope: 'regular' });
            }
        });
        return ;
    } else if (proxy_mode == 'require') {
        //alert('mode : require');
        var pac =
            'dnsDomainIs=function(h,d){return h.indexOf(d,h.length-d.length)!==-1&&(d.length===h.length||h.indexOf("."+d,h.length-("."+d).length)!==-1)};'+
            "var FindProxyForURL = function(url, host){"+
            "host=host.toLowerCase();"+
            "var block_list = "+localStorage.getItem('5uf_block_list')+';'+
            "var block_list_system = "+localStorage.getItem('5uf_block_list_system')+';'+


            "var white_list = "+localStorage.getItem('5uf_white_list')+';'+
            "var white_list_system = "+localStorage.getItem('5uf_white_list_system')+';'+
            "for(i=0;i<white_list.length;i++){"+
            "if (dnsDomainIs(host,white_list[i])){"+
            "return 'DIRECT';"+
            "}"+
            "}"+
            "for(i=0;i<white_list_system.length;i++){"+
            "if (dnsDomainIs(host,white_list_system[i])){"+
            "return 'DIRECT';"+
            "}"+
            "}"+
            "for(i=0;i<block_list_system.length;i++){"+
            "if (dnsDomainIs(host,block_list_system[i])){"+
            "return 'HTTPS "+localStorage.getItem('5uf_server_selected_host')+":"+(localStorage.getItem('5uf_server_selected_port') != 0 ? localStorage.getItem('5uf_server_selected_port') : '443')+"';"+
            "}"+
            "}"+
            "for(i=0;i<block_list.length;i++){"+
            "if (dnsDomainIs(host,block_list[i])){"+
            "return 'HTTPS "+localStorage.getItem('5uf_server_selected_host')+":"+(localStorage.getItem('5uf_server_selected_port') != 0 ? localStorage.getItem('5uf_server_selected_port') : '443')+"';"+
            "}"+
            "}"+
            "return 'DIRECT';"+

            "}";
        //alert( "return 'HTTPS "+localStorage.getItem('5uf_server_selected_host')+":"+(localStorage.getItem('5uf_server_selected_port') != 0 ? localStorage.getItem('5uf_server_selected_port') : '443')+"';");
        var config = {
            mode: "pac_script",
            pacScript: {
                data: pac
            }
        }
        chrome.proxy.settings.set({value: config, scope: 'regular'}, function(){});
    }

}

//打印obj
function writeObj(obj){
    var description = "";
    for(var i in obj){
        var property=obj[i];
        description+=i+" = "+property+"\n";
    }
    alert(description);
}
