function ActiveXConfig(a){void 0===a&&(a={version:3,rules:{},defaultRules:{},scriptContents:{},scripts:{},order:[],notify:[],issues:[],blocked:[],misc:{lastUpdate:0,logEnabled:!1,tracking:!0,verbose:3}});a=ActiveXConfig.convertVersion(a);a.removeInvalidItems();a.update();return a}var settingKey="setting";clsidPattern=/[^0-9A-F][0-9A-F]{8}-([0-9A-F]{4}-){3}[0-9A-F]{12}[^0-9A-F]/;
var agents={ie9:"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)",ie8:"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; WOW64; Trident/4.0)",ie7:"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; WOW64;)",ff7win:"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0.1) Gecko/20100101 Firefox/7.0.1",ip5:"Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3",ipad5:"Mozilla/5.0 (iPad; CPU OS 5_0 like Mac OS X) AppleWebKit/534.46(KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3"};
function getUserAgent(a){if(!a||!(a in agents))return"";var b=0<=navigator.userAgent.indexOf("WOW64"),a=agents[a];b||(a=a.replace(" WOW64;",""));return a}
ActiveXConfig.convertVersion=function(a){if(3==a.version)return a.blocked||(a.blocked=[]),a.__proto__=ActiveXConfig.prototype,a;if(2==a.version){var b=function(a){var a=a.trim(),b=a.match(/###(.*)/);return b?{pattern:a.match(/(.*)###/)[1].trim(),title:b[1].trim()}:{pattern:a,title:"Rule"}},c=new ActiveXConfig;a.logEnabled&&(c.misc.logEnabled=!0);for(var d=a.url_plain.split("\n"),e=0;e<d.length;++e){var f=c.createRule(),g=b(d[e]);f.title=g.title;g=g.pattern;"r/"==g.substr(0,2)?(f.type="regex",f.value=
g.substr(2)):(f.type="wild",f.value=g);c.addCustomRule(f,"convert")}a=a.trust_clsids.split("\n");for(e=0;e<a.length;++e)f=c.createRule(),f.type="clsid",g=b(a[e]),f.title=g.title,f.value=g.pattern,c.addCustomRule(f,"convert");firstUpgrade=!0;return c}};
ActiveXConfig.prototype={shouldEnable:function(a){return this.pageRule?this.pageRule:(a=this.getFirstMatchedRule(a,this.clsidRules))?a:null},createRule:function(){return{title:"Rule",type:"wild",value:"",userAgent:"",scriptItems:""}},removeInvalidItems:function(){function a(a,b){"object"!=typeof a[b]&&(console.log("reset ",b),a[b]={});for(var c in a[b]){var g=!0;"object"!=typeof a[b][c]&&(g=!1);g&&a[b][c].identifier!=c&&(g=!1);g||(console.log("remove corrupted item ",c," in ",b),delete a[b][c])}}
if(!this.pageSide){a(this,"rules");a(this,"defaultRules");a(this,"scripts");a(this,"issues");for(var b=[],c=0;c<this.order.length;++c)this.getItem(this.order[c])?b.push(this.order[c]):console.log("remove order item ",this.order[c]);this.order=b}},addCustomRule:function(a,b){if(this.validateRule(a)){var c=this.createIdentifier();a.identifier=c;this.rules[c]=a;this.order.push({status:"custom",position:"custom",identifier:c});this.update();trackAddCustomRule(a,b)}},updateDefaultRules:function(a){var b=
!1,c={};if("object"!=typeof this.defaultRules)console.log("Corrupted default rules, reload all"),b=!0,this.defaultRules={};else for(var d in this.defaultRules)d in a||(console.log("Remove rule "+d),c[d]=!0,delete this.defaultRules[d]);var e=[];for(d=0;d<this.order.length;++d)("custom"==this.order[d].position||!b&&!c[this.order[d].identifier])&&e.push(this.order[d]);this.order=e;b=0;for(d in a)d in this.defaultRules||(this.addDefaultRule(a[d],b),b++),this.defaultRules[d]=a[d]},loadDefaultConfig:function(){var a=
this;$.ajax({url:"/settings/setting.json",dataType:"json",success:function(b){console.log("Update default rules");a.updateDefaultRules(b);a.update()}});$.ajax({url:"/settings/scripts.json",dataType:"json",success:function(b){console.log("Update scripts");a.scripts=b;a.loadAllScripts();a.update()}});$.ajax({url:"/settings/issues.json",dataType:"json",success:function(b){a.issues=b;a.update()}})},getPageConfig:function(a){var b={pageSide:!0};b.version=this.version;b.verbose=this.misc.verbose;b.logEnabled=
this.misc.logEnabled;b.pageRule=this.getFirstMatchedRule({href:a});b.pageRule?(a=this.getScripts(b.pageRule.script),b.pageScript=a.page,b.extScript=a.extension):b.clsidRules=this.cache.clsidRules;return b},getFirstMatchedRule:function(a,b){var c=!1;b||(b=this.cache.validRules,c=!0);if(Array.isArray(b))for(var d=0;d<b.length;++d){if(this.isRuleMatched(b[d],a,c?d:-1))return b[d]}else for(d in b)if(this.isRuleMatched(b[d],a))return b[d];return null},getMatchedIssue:function(a){return this.getFirstMatchedRule(a,
this.issues)},activeRule:function(a){for(var b=0;b<this.order.length;++b)if(this.order[b].identifier==a.identifier){"disabled"==this.order[b].status&&(this.order[b].status="enabled",trackAutoEnable(a.identifier));break}this.update()},validateRule:function(a){var b=!0;try{if("wild"==a.type)b&=null!=this.convertUrlWildCharToRegex(a.value);else if("regex"==a.type)b&=""!=a.value;else if("clsid"==a.type){var c=a.value.toUpperCase();if(!clsidPattern.test(c)||55<c.length)b=!1}}catch(d){b=!1}return b},isRuleMatched:function(a,
b,c){if("wild"==a.type||"regex"==a.type){var d;0<=c?d=this.cache.regex[c]:"wild"==a.type?d=this.convertUrlWildCharToRegex(a.value):"regex"==a.type&&(d=RegExp("^"+a.value+"$","i"));if(b.href&&d.test(b.href))return!0}else if("clsid"==a.type&&b.clsid&&(a=clsidPattern.exec(a.value.toUpperCase()),b=clsidPattern.exec(b.clsid.toUpperCase()),a&&b&&a[0]==b[0]))return!0;return!1},getScripts:function(a){var b={page:"",extension:""};if(!a)return b;for(var a=a.split(" "),c=0;c<a.length;++c)if(a[c]&&this.scripts[a[c]]){var d=
a[c],e="// ",e=e+(a[c]+"\n"),e=e+this.scriptsContents[d],e=e+"\n\n";b[this.scripts[d].context]+=e}return b},convertUrlWildCharToRegex:function(a){try{var b=function(a,b){b||(b="*");return a.replace(/([\.\\\/\?\{\}\+\[\]])/g,"\\$1").replace("*",b)},a=a.toLowerCase();"<all_urls>"==a&&(a="*://*/*");var c=/^(.*?):\/\/(\*?[^\/\*]*)\/(.*)$/i.exec(a);if(null==c)return null;var d=c[2],e=c[3],f="^"+b(c[1],"[^:]*")+":\\/\\/",f=f+(b(d,"[^\\/]*")+"/"),f=f+b(e,".*");return RegExp(f,"i")}catch(g){return null}},
update:function(){this.pageSide||(this.updateCache(),this.cache.listener&&this.cache.listener.trigger("update"),this.save())},addDefaultRule:function(a,b){console.log("Add new default rule: ",a);var c=null;if("clsid"==a.type)for(var d in this.rules){if(this.isRuleMatched(this.rules[d],{href:"not-a-URL-/",clsid:a.value},-1)){c=this.rules[d];break}}else if(a.keyword&&a.testUrl)for(d in this.rules)if(this.isRuleMatched(this.rules[d],{href:a.testUrl},-1)&&-1!=this.rules[d].value.toLowerCase().indexOf(a.keyword)){c=
this.rules[d];break}var e="disabled";if(c){console.log("Convert custom rule",c," to default",a);e="enabled";delete this.rules[c.identifier];for(d=0;d<this.order.length;++d)if(this.order[d].identifier==c.identifier){this.order.splice(d,1);break}}this.order.splice(b,0,{position:"default",status:e,identifier:a.identifier});this.defaultRules[a.identifier]=a},updateCache:function(){if(!this.pageSide){this.cache={validRules:[],regex:[],clsidRules:[],userAgentRules:[],listener:(this.cache||{}).listener};
for(var a=0;a<this.order.length;++a)if(0<=["custom","enabled"].indexOf(this.order[a].status)){var b=this.getItem(this.order[a]),c=this.cache.validRules.push(b)-1;"clsid"==b.type?this.cache.clsidRules.push(b):"wild"==b.type?this.cache.regex[c]=this.convertUrlWildCharToRegex(b.value):"regex"==b.type&&(this.cache.regex[c]=RegExp("^"+b.value+"$","i"));""!=b.userAgent&&this.cache.userAgentRules.push(b)}}},save:function(){if("chrome-extension:"==location.protocol){var a=this.cache;delete this.cache;var b=
this.scriptsContents;delete this.scriptsContents;localStorage[settingKey]=JSON.stringify(this);this.cache=a;this.scriptsContents=b}},createIdentifier:function(){var a="custom_"+Date.now()+"_"+this.order.length+"_",b;do b=a+Math.round(65536*Math.random());while(this.getItem(b));return b},getItem:function(a){var b=a;"string"!=typeof b&&(b=a.identifier);return b in this.rules?this.rules[b]:this.defaultRules[b]},loadAllScripts:function(){this.scriptsContents={};setting=this;for(var a in this.scripts){var b=
this.scripts[a];$.ajax({url:"/settings/"+b.url,datatype:"text",context:b,success:function(a){setting.scriptsContents[this.identifier]=a}})}}};function loadLocalSetting(){var a=void 0;if(localStorage[settingKey])try{a=JSON.parse(localStorage[settingKey])}catch(b){a=void 0}a||(firstRun=!0);return new ActiveXConfig(a)}function clearSetting(){localStorage.removeItem(settingKey);setting=loadLocalSetting()};
