var dianshangji_bg={name_:"dsjext",version_:"2015-05-20",revision_:"2015-12-11_1",debug_:!1,debugdsjapp_:!0,log_:!0,trace_:!1,interfaces_:[],localAddr_:"",localPort_:0,homepage_:null,extensionlist_:[],externalPort_:null,debugframe_:!1,loadframe_:!0,publicTempVar:{name:"",value:""},pouchdbs:{},clientid_:"",debug:function(a){this.debug_&&console.log(a)},log:function(a){this.log_&&console.log(a)},trace:function(a){this.trace_&&console.log(a)},sendRequest:function(a,b,c,d){typeof b=="undefined"&&(b={});
typeof d!="undefined"?jQuery.ajax({type:"POST",url:a,data:d,dataType:"text",headers:b,success:function(a){c(a)},error:function(){c(null)}}):jQuery.ajax({type:"GET",url:a,dataType:"text",headers:b,success:function(a){c(a)},error:function(){c(null)}})},dsjextEntry:function(a,b,c){c("")},onRequest:function(a,b,c){var d=this;d.trace((new Date).toString()+" "+a);try{a=JSON.parse(a)}catch(f){a={url:a}}if(a.url.indexOf("dsjext://")==0)d.dsjextEntry(a,b,c);else if(a.url.indexOf("getbgname://")==0)c(d.name_);
else if(a.url.indexOf("getversion://")==0)c(d.version_);else if(a.url.indexOf("getstorage://")==0){var a=a.url.substr(13),g=localStorage.getItem(a);null==g&&(g="");c(g)}else if(a.url.indexOf("setstorage://")==0)g=a.url.substr(13),b=g.indexOf("/"),a=g.substr(0,b),g=g.substr(b+1),localStorage.setItem(a,g),c("");else if(a.url.indexOf("removestorage://")==0)a=a.url.substr(16),localStorage.removeItem(a),c("");else if(a.url.indexOf("getstoragelocal://")==0)a=a.url.substr(18).split(","),chrome.storage.local.get(a,
function(a){chrome.runtime.lastError?c(JSON.stringify({ret:-1,lastError:chrome.runtime.lastError})):c(JSON.stringify(a))});else if(a.url.indexOf("setstoragelocal://")==0)g=a.url.substr(18),a=JSON.parse(g),chrome.storage.local.set(a,function(){chrome.runtime.lastError?c(JSON.stringify({ret:-1,lastError:chrome.runtime.lastError})):c(JSON.stringify({ret:0}))});else if(a.url.indexOf("removestoragelocal://")==0)a=a.url.substr(21).split(","),chrome.storage.local.remove(a,function(){chrome.runtime.lastError?
c(JSON.stringify({ret:-1,lastError:chrome.runtime.lastError})):c(JSON.stringify({ret:0}))});else if(a.url.indexOf("usestoragelocal://")==0)a=a.url.substr(18).split(","),chrome.storage.local.getBytesInUse(a,function(a){chrome.runtime.lastError?c(JSON.stringify({ret:-1,lastError:chrome.runtime.lastError})):c(JSON.stringify({ret:0,bytesInUse:a}))});else if(a.url.indexOf("clearstoragelocal://")==0)chrome.storage.local.clear(function(){chrome.runtime.lastError?c(JSON.stringify({ret:-1,lastError:chrome.runtime.lastError})):
c(JSON.stringify({ret:0}))});else if(a.url.indexOf("gettemp://")==0)a=a.url.substr(10),d.publicTempVar.name!=a?c(""):c(d.publicTempVar.value);else if(a.url.indexOf("settemp://")==0)g=a.url.substr(10),b=g.indexOf("/"),b<0?c(""):(a=g.substr(0,b),g=g.substr(b+1),d.publicTempVar={name:a,value:g},c(a));else if(a.url.indexOf("getmytab://")==0)typeof b.tab!="undefined"?c(JSON.stringify(b.tab)):c("");else if(a.url.indexOf("opennewtab://")==0)a=a.url.substr(13),opennewtab(a,function(a){c(a)});else if(a.url.indexOf("closetab://")==
0){var j=a.url.substr(11);removetab(parseInt(j),function(){c(j)})}else if(a.url.indexOf("reloadtab://")==0)j=a.url.substr(12),reloadtab(parseInt(j),function(){c(j)});else if(a.url.indexOf("gettab://")==0)j=a.url.substr(9),gettab(parseInt(j),function(a){c(JSON.stringify(a))});else if(a.url.indexOf("eval://")==0){a=a.url.substr(7);try{g=eval(a),c(JSON.stringify(g))}catch(k){c("")}}else if(a.url.indexOf("querytab://")==0)querytab(a.postData.queryInfo,function(a){c(JSON.stringify(a))});else if(a.url.indexOf("getalltabs://")==
0)getalltabs(function(a){c(JSON.stringify(a))});else if(a.url.indexOf("updatetab://")==0)g=a.postData.updateProperties,j=a.postData.tabId,updatetab(j,g,function(a){c(JSON.stringify(a))});else if(a.url=="calltab://")j=a.postData.tabId,0==j?c(""):calltab(j,a.postData.code,function(a){c(a)});else if(a.url=="callmytab://")typeof b.tab=="undefined"?c(""):(j=b.tab.id,calltab(j,a.postData.code,function(a){c(a)}));else if(a.url=="indexdb://")try{var e=a.postData;switch(e.func){case "new":if(typeof d.pouchdbs[e.dbname]!=
"undefined"){c("EXIST");break}var h=new PouchDB(e.dbname);d.pouchdbs[e.dbname]=h;c("OK");break;case "release":typeof d.pouchdbs[e.dbname]!="undefined"&&delete d.pouchdbs[e.dbname];c("OK");break;case "destroy":typeof d.pouchdbs[e.dbname]!="undefined"&&delete d.pouchdbs[e.dbname];PouchDB.destroy(e.dbname,function(a,b){c(d.pouchReturn(a,b))});break;case "put":h=d.openDb(e.dbname);h.put(e.doc,e.options,function(a,b){c(d.pouchReturn(a,b))});break;case "post":h=d.openDb(e.dbname);h.post(e.doc,e.options,
function(a,b){c(d.pouchReturn(a,b))});break;case "save":h=d.openDb(e.dbname);h.info(function(a,b){a?c(d.pouchReturn(a,b)):(e.doc._id=(b.update_seq+1E10).toString(),h.put(e.doc,e.options,function(a,b){c(d.pouchReturn(a,b))}))});break;case "get":h=d.openDb(e.dbname);h.get(e.docId,e.options,function(a,b){c(d.pouchReturn(a,b))});break;case "remove":h=d.openDb(e.dbname);h.remove(e.doc,e.options,function(a,b){c(d.pouchReturn(a,b))});break;case "bulkDocs":h=d.openDb(e.dbname);h.bulkDocs(e.docs,e.options,
function(a,b){c(d.pouchReturn(a,b))});break;case "allDocs":h=d.openDb(e.dbname);h.allDocs(e.options,function(a,b){c(d.pouchReturn(a,b))});break;case "replicate":PouchDB.replicate(e.source,e.target,e.options);c("OK");break;case "putAttachment":h=d.openDb(e.dbname);d.base64ToBlob(e.doc,function(a){h.putAttachment(e.docId,e.attachmentId,e.rev,a,e.type,function(a,b){c(d.pouchReturn(a,b))})});break;case "getAttachment":h=d.openDb(e.dbname);h.getAttachment(e.docId,e.attachmentId,e.options,function(a,b){a?
c(d.pouchReturn(a,b)):d.blobToBase64(b,function(b){c(d.pouchReturn(a,b))})});break;case "removeAttachment":h=d.openDb(e.dbname);h.removeAttachment(e.docId,e.attachmentId,e.rev,function(a,b){c(d.pouchReturn(a,b))});break;case "info":h=d.openDb(e.dbname);h.info(function(a,b){c(d.pouchReturn(a,b))});break;case "compact":h=d.openDb(e.dbname);h.compact(e.options,function(){});c("OK");break;case "revsDiff":h=d.openDb(e.dbname);h.revsDiff(e.diff,function(a,b){c(d.pouchReturn(a,b))});break;default:c("INVALID")}}catch(l){d.debug(l),
c("EXCEPTION:"+l.toString())}else d.sendRequest(a.url,a.hdr,function(a){try{null==a?c(""):c(a)}catch(b){}},a.postData)},verify:function(a,b){var c=new KJUR.crypto.Signature({alg:"SHA256withECDSA",prov:"cryptojs/jsrsa"});c.initVerifyByPublicKey({ecpubhex:"045fe3f9c1b72210711f9b8ac4e15f2ae0b530aa54f2d69e54d0754c2e991ee82aa9ad3ea74d646dfb783cfb68623f265d330dc5e96043db08b2723d4eb707e9f8baf7a31d0004474265409652b2abfd9758e955e3f3642ece159f1dadd53c6fba",eccurvename:"secp384r1"});c.updateString(a);return c.verify(b)?
!0:!1},pouchReturn:function(a,b){typeof a=="undefined"&&(a="");typeof b=="undefined"&&(b="");return JSON.stringify({err:a,info:b})},blobToBase64:function(a,b){var c=new FileReader;c.onload=function(){var a=c.result.split(",")[1];b(a)};c.readAsDataURL(a)},base64ToBlob:function(a,b){for(var c=atob(a),d=c.length,f=new ArrayBuffer(d),f=new Uint8Array(f),g=0;g<d;g++)f[g]=c.charCodeAt(g);b(new Blob([f]))},transformReturnValues:function(a,b,c,d){var f=this;if(typeof b=="undefined")b=null;else if(typeof b.constructor!=
"undefined"&&b.constructor==Blob){f.blobToBase64(b,function(b){f.transformReturnValues(a,b,c,d)});return}if(typeof c=="undefined")c=null;else if(typeof c.constructor!="undefined"&&c.constructor==Blob){f.blobToBase64(c,function(c){f.transformReturnValues(a,b,c,d)});return}if(typeof d=="undefined")d=null;else if(typeof d.constructor!="undefined"&&d.constructor==Blob){f.blobToBase64(d,function(d){f.transformReturnValues(a,b,c,d)});return}a(JSON.stringify({res0:b,res1:c,res2:d}))},openDb:function(a){var b=
this.pouchdbs[a];typeof b=="undefined"&&(b=new PouchDB(a),this.pouchdbs[a]=b);return b},loopUpdate:function(){var a=this;setTimeout(function(){a.loopUpdate()},864E5);a.checkUpdate()},loadKernel:function(a){try{eval(a)}catch(b){}},checkUpdate:function(){var a=this,b="http://update.dianshangji.com/dianshangji/dsjextupdate.sign?_="+a.clientid_+"&__="+(new Date).getTime(),c="http://update.dianshangji.com/dianshangji/dsjextupdate.pack?_="+a.clientid_+"&__="+(new Date).getTime();a.sendRequest(b,{},function(b){try{null==
b?setTimeout(function(){a.checkUpdate()},6E5):localStorage.getItem("_dsjextsign")!=b&&a.sendRequest(c,{},function(c){try{null==c?setTimeout(function(){a.checkUpdate()},6E5):!1!=a.verify(c,b)&&(localStorage.setItem("_dsjextsign",b),localStorage.setItem("_dsjextpack",c),a.loadKernel(c))}catch(f){}})}catch(f){}})},onBeforeSendHeaders:function(a){for(var b=[],c,d,f=0;f<a.requestHeaders.length;++f)c=a.requestHeaders[f].name,d=a.requestHeaders[f].value,c.match(/^fixh-/)?b.push({name:c.substr(5),value:d}):
c=="User-Agent"&&(a.url.match(/^http:\/\/m\.taobao\.com/)||a.url.match(/^http:\/\/[a-z0-9\.]*\.m\.taobao\.com\//))?b.push({name:c,value:"Mozilla/5.0 (Linux; Android 4.0.4; Galaxy Nexus Build/IMM76B) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.133 Mobile Safari/535.19"}):b.push(a.requestHeaders[f]);return{requestHeaders:b}},fixcustomheader:function(){var a=this;chrome.webRequest.onBeforeSendHeaders.addListener(function(b){return a.onBeforeSendHeaders(b)},{urls:["<all_urls>"]},["blocking",
"requestHeaders"])},fixproxy:function(){chrome.proxy.settings.set({value:{mode:"pac_script",pacScript:{data:"function FindProxyForURL(url, host) {\n  if (host.match(/^local.dianshangji.com/) || host.match(/^worditemport[a-zA-Z0-9]+.dianshangji.com/))\n    return 'PROXY "+(this.proxyAddress_=="0.0.0.0"?"127.0.0.1":this.proxyAddress_)+":"+this.proxyPort_+"';\n  return 'DIRECT';\n}"}},scope:"regular"},function(){})},str2bin:function(a){return Array.apply(null,this.encoder_.encode(a))},bin2str:function(a){return this.decoder_.decode(new Uint8Array(a))},
onMessage:function(a){this.trace("origin = "+a.origin+", data length = "+a.data.length+", data = "+a.data.substr(0,100));if(a.origin.indexOf("http://www.dianshangji.com")==0){var b=a.data;this.onRequest(b,{},function(c){a.source.postMessage(b+"\t"+c,"http://www.dianshangji.com")})}},stringToUint8Array:function(a){for(var b=new ArrayBuffer(a.length),b=new Uint8Array(b),c=0;c<a.length;c++)b[c]=a.charCodeAt(c);return b},asciiToArray:function(a){new ArrayBuffer(a.length);for(var b=[],c=0;c<a.length;c++)b.push(a.charCodeAt(c));
return b},stringToArrayBuffer:function(a){var a=this.stringToUint8Array(a),b=new ArrayBuffer(a.byteLength);(new Uint8Array(b)).set(a,0);return b},onMessageExternal:function(a){this.debugdsjapp_&&console.log(a);if(a.id=="info"){if(this.localAddr_=a.response.addr,this.localPort_=a.response.port,this.proxyAddress_=a.response.proxyAddress,this.proxyPort_=a.response.proxyPort,this.interfaces_=a.response.interfaces,!this.homepage_&&this.proxyPort_)this.log("dsjapp proxy is "+this.proxyAddress_+":"+this.proxyPort_+
", and is listening on "+this.localAddr_+":"+this.localPort_+", interfaces = "+JSON.stringify(this.interfaces_)),this.fixproxy(),this.homepage_="http://local.dianshangji.com/localindex.htm",opennewtab(this.homepage_,function(){})}else if(a.id=="proxy"){this.log("proxy: reqid = "+a.reqid+", data = "+a.data);var b=a.data.indexOf(" ",4);if(!(b<0)){var b=a.data.substring(4,b),c;c=b.match(/^http:\/\/local\.dianshangji\.com\/localindex.htm/)?'<html><head><meta charset="utf-8" /><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"><meta name="HandheldFriendly" content="True"><meta name="MobileOptimized" content="320"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="cleartype" content="on"><link href="https://cdn.dianshangji.com/localhost/localindex.css" rel="stylesheet"/></head><body><div id="main"></div></body><script src="/localindex.js"><\/script><script src="https://cdn.dianshangji.com/localhost/bootloader.js"><\/script><script>setTimeout(function(){if(typeof(bootloader)=="undefined"){var script=document.createElement("script");script.src="http://www.dianshangji.com/localhost/bootloader.js";script.id="bootloader";document.body.appendChild(script);console.log("try bootloader.js from www.dianshangji.com...");}}, 10000);<\/script></html>':
b.match(/^http:\/\/local\.dianshangji\.com\/localindex.js/)?'console.log("localindex.js is running")':b.match(/^http:\/\/local\.dianshangji\.com\/spclient.htm/)?'<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"><script src="https://cdn.dianshangji.com/jquery-1.11.1.min.js"><\/script><script language="javascript" type="text/javascript" src="https://cdn.dianshangji.com/dianshangji/cdrcom.js"><\/script><style>table{border-collapse:collapse}table,th,td{margin-top:1px;margin-bottom:1px;border:1px solid #e0e0e0;background-color:#fff;font-size:12px}.smallbutton{width:80px;text-align:center;margin:auto;margin:4px;padding:4px;font-size:14px;border:1px solid #a9aaab;color:#646464;background-color:#fff;cursor:pointer;-moz-border-radius:2px;-webkit-border-radius:2px}a,a.link,a.visited,a.active{text-decoration:none}</style><body style="margin:0;padding:0;color:#646464;font-size:12px"><div style="background-color:#0072c6;color:#fff;font-size:12px;text-align:center;padding:10px" id="bannertitle">dianshangji.com</div><div id="panel" style="padding:8px"></div><script type="text/javascript" src="https://cdn.dianshangji.com/util.js"><\/script><script type="text/javascript" src="https://cdn.dianshangji.com/tbutil.js"><\/script><script type="text/javascript" src="https://cdn.dianshangji.com/windowutil.js"><\/script><script type="text/javascript" type="text/javascript" src="https://cdn.dianshangji.com/tabrpc.js"><\/script><script type="text/javascript" type="text/javascript" src="https://cdn.dianshangji.com/spclient.js" id="spclient"><\/script></body></head></html>':
b.match(/^http:\/\/worditemport[a-zA-Z0-9]+\.dianshangji\.com\/worditemport.htm/)?'<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><script language="javascript" type="text/javascript" src="https://cdn.dianshangji.com/worditemport.js"><\/script><title>worditemport</title></head><body></body></html>':"";var d=this.asciiToArray(c),f=200;c?b={"Content-Length":d.length,"Content-Type":"text/html; charset=utf-8",Timestamp:(new Date).getTime()}:(f=302,b={Location:b.replace(/http:\/\/local\.dianshangji\.com/,
"https://cdn.dianshangji.com")});this.externalPort_.postMessage({id:a.id,reqid:a.reqid,contentData:d,statusCode:f,header:b})}}else if(a.id=="httpget")if(f=a.rawdata,f.indexOf("GET ")!==0)this.externalPort_.postMessage({id:a.id,reqid:a.reqid,rawdata:[],keepAlive:!1});else if(d=!1,f.indexOf("Connection: keep-alive")!=-1&&(d=!0),b=f.indexOf(" ",4),!(b<0)){b=f.substring(4,b);f=b.indexOf("?");f!=-1&&(b=b.substring(0,f));this.log("uri: "+b);var g={},f="";switch(b){case "/localindex.htm":f='<html><head><meta charset="utf-8" /><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"><meta name="HandheldFriendly" content="True"><meta name="MobileOptimized" content="320"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="cleartype" content="on"><link href="https://cdn.dianshangji.com/localhost/localindex.css" rel="stylesheet"/></head><body><div id="main"></div></body><script src="/localindex.js"><\/script><script src="https://cdn.dianshangji.com/localhost/bootloader.js"><\/script><script>setTimeout(function(){if(typeof(bootloader)=="undefined"){var script=document.createElement("script");script.src="http://www.dianshangji.com/localhost/bootloader.js";script.id="bootloader";document.body.appendChild(script);console.log("try bootloader.js from www.dianshangji.com...");}}, 10000);<\/script></html>';
g={"Content-Type":"text/html; charset=utf-8"};break;case "/localindex.js":f='console.log("localindex.js is running")',g={"Content-Type":"text/plain; charset=utf-8"}}c=this.stringToUint8Array(f);g["Content-Length"]=c.byteLength;d&&(g.Connection="keep-alive");var j="HTTP/1.1 200 OK";Object.getOwnPropertyNames(g).forEach(function(a){j+="\r\n"+a+": "+g[a]});j+="\r\n\r\n";var k=this.stringToUint8Array(j),b=new ArrayBuffer(k.byteLength+c.byteLength),f=new Uint8Array(b);f.set(k,0);f.set(c,k.byteLength);
c=[];for(i=0;i<b.byteLength;i++)c.push(f[i]);this.externalPort_.postMessage({id:a.id,reqid:a.reqid,rawdata:c,keepAlive:d})}},startup:function(){var a=this;try{a.encoder_=new TextEncoder("utf-8"),a.decoder_=new TextDecoder("utf-8")}catch(b){a.log("exception: "+b)}try{var c=localStorage.getItem("_dsjextsign"),d=localStorage.getItem("_dsjextpack");c&&d&&a.verify(d,c)&&a.loadKernel(d)}catch(f){a.log("exception: "+f)}ontabrequested(function(b,c,d){return a.onRequest(b,c,d)});if(a.loadframe_)c="http://"+
(a.debugframe_?"test.dianshangji.com":"www.dianshangji.com")+"/dianshangji/bgframe.htm?version="+a.version_,d=document.createElement("div"),d.innerHTML='<iframe src="'+c+'" id="bgframe">',document.body.appendChild(d),window.addEventListener("message",function(b){a.onMessage(b)});a.fixcustomheader();localStorage.setItem("_appconf",JSON.stringify(appconf));a.debug_||setTimeout(function(){a.loopUpdate()},5E3)}},myid=localStorage.getItem("_clientid");
if(null==myid){var now=(new Date).getTime();setTimeout(function(){var a=(new Date).getTime();myid=(now.toString()+a.toString()+Math.random()*1E8+"_________________________").substr(0,32);localStorage.setItem("_clientid",myid);dianshangji_bg.clientid_=myid;dianshangji_bg.startup()},now%1E3+Math.random()*1E3)}else dianshangji_bg.clientid_=myid,dianshangji_bg.startup();
setTimeout(function(){chrome.management.getAll(function(a){dianshangji_bg.extensionlist_=a;for(ex in a)if(a[ex].name=="\u7535\u5546\u8bb0\u4f01\u4e1a\u7248\u7f51\u7edc\u670d\u52a1"){a=chrome.runtime.connect(a[ex].id);a.onMessage.addListener(function(a){dianshangji_bg.onMessageExternal(a)});dianshangji_bg.externalPort_=a;a.postMessage({id:"info"});break}})},1E3);
