Registry.require(["promise","helper","convert"],function(){var f=Registry.get("promise"),q=Registry.get("helper"),l=Registry.get("convert"),r=function(){var e=f();Registry.vendor(["vendor/jszip/jszip"],function(){r=f.Pledge;e.resolve()});return e.promise()},m=function(){var e,d=!1,g,b=function(){var a=f();return g=a};return{write:function(){var a=f();d=!1;e=new JSZip;a.resolve(e);return a.promise()},open:function(a){var c=b();d=!0;JSZip.loadAsync(a).then(function(a){e=a;c.resolve(e)},function(a){g&&
g.reject(a);c.reject(a)});return c.promise()},entries:function(){var a=b(),c=e.files;a.resolve(Object.keys(c).map(function(a){if((a=c[a])&&!a.dir)return{filename:a.name}}).filter(function(a){return a}));return a.promise()},get:function(a){var c=b();e.file(a.filename).async("arraybuffer").then(function(a){c.resolve(a)});return c.promise()},put:function(a,c,k){var n=b();try{e.file(a,c,{date:k?new Date(k):void 0}),n.resolve()}catch(d){n.reject(d)}return n.promise()},end:function(){var a=b();d?a.resolve():
e.generateAsync({type:"blob",compression:"DEFLATE",comment:"Created by Tampermonkey"}).then(function(b){a.resolve(b)},function(b){a.reject(b)});return a.promise()}}}();Registry.register("porter","5946",{zip:{create:function(e,d){var g=f(),b=0;r().then(function(){return m.write()}).then(function(a){var c=f(),k={},d=function(a,b){var c=[a,b].join(".");if(k[c]){var e;do e=a+" ("+k[c]+")",c=[e,b].join("."),k[c]++;while(k[c]);return d(e,b)}k[c]=1;return c},p=e.length,h=function(){if(!e.length)return c.resolve();
var a=e.shift(),k=a.meta.name.replace(/[\\\/$*?|]/g,"-"),t=d(k,"user.js"),v=d(k,"options.json"),w=d(k,"storage.json"),x=JSON.stringify({options:a.options,settings:a.settings,meta:a.meta}),u=a.storage?JSON.stringify(a.storage):null;b+=a.source.length;console.log("porter: add to zip",t,b);g.notify("Zip: "+Math.floor((p-e.length)/p*100)+"%");m.put(t,a.source,a.meta.modified).then(function(){b+=x.length;console.log("porter: add to zip",v,b);return m.put(v,x)}).then(function(){if(!u)return f.Pledge();
b+=u.length;console.log("porter: add to zip",w,b);return m.put(w,u)}).then(function(){if(!a.resources.length&&!a.requires.length)return f.Pledge();var b=[];["resources","requires"].forEach(function(c){a[c].forEach(function(a,k){if(void 0!==a.source){var e=a.meta.name.replace(/[\\\/$*?|]/g,"-"),d=l.MD5(c+a.meta.url),h=[t,d,e].join("-"),g=JSON.stringify(a.meta),e=l.str2arrbuf(a.source,"resources"==c?void 0:"UTF-8");b.push(m.put(h,e).then(function(){return m.put(h+"."+c+".json",g)}))}})});return f.when(b)}).fail(function(a){console.log("porter: add to zip failed",
a)}).always(function(){console.log("porter: add to zip -> next round");window.setTimeout(h,5)})};h();return c.promise()}).then(function(){console.log("porter: add global props");return d?m.put("Tampermonkey.global.json",JSON.stringify(d)):f.Pledge()}).then(function(){return m.end()}).done(function(a){g.resolve(a)}).fail(function(){g.reject()});return g.promise()},read:function(e){var d=f(),g;r().then(function(){return m.open(e)}).then(function(){return m.entries()}).then(function(b){var a=f(),c={},
e={},n=b.length,p=function(){if(b.length){var h=b.shift();console.log("porter: read from zip",h.filename);m.get(h).done(function(a){var b=h.filename.match(/((.*)\.(storage\.json)|(.*)\.(options\.json)|(.*)\.(global\.json)|(.*)\.(user\.js)|(.*)\.user\.js-[0-9a-f]+-.*\.(resources\.json)|(.*)\.user\.js-[0-9a-f]+-.*\.(requires\.json))$/);b&&(b=b.slice(1).filter(function(a){return a}));if(!b||3>b.length)e[h.filename]=a;else try{var d=b[1],f=b[2];c[d]=c[d]||{resources:{},requires:{}};a=l.arrbuf2str(a,"UTF-8");
"global.json"==f?g=JSON.parse(a):"user.js"==f?c[d].source=a:"options.json"==f?c[d].options=JSON.parse(a):"resources.json"==f?c[d].resources[h.filename]={name:h.filename,data:JSON.parse(a)}:"requires.json"==f?c[d].requires[h.filename]={name:h.filename,data:JSON.parse(a)}:"storage.json"==f&&(c[d].storage=JSON.parse(a))}catch(n){console.warn("porter: read from zip failed",n)}}).always(function(){d.notify("Zip: "+Math.floor((n-b.length)/n*100)+"%");window.setTimeout(p,5)})}else{var f=[];q.each(c,function(a,
b){var c=a.options||{};c.source=a.source;c.storage=a.storage;["resources","requires"].forEach(function(b){var d=a[b];d&&q.each(d,function(a,d){var f,h;f=a.name.replace("."+b+".json","");if(h=e[f])c[b]=f=c[b]||[],f.push({meta:a.data,source:l.arrbuf2str(h,"resources"==b?void 0:"UTF-8")})})});f.push(c)});a.resolve({scripts:f,global_settings:g})}};p();return a.promise()}).done(function(b){d.resolve(b)}).fail(function(){d.reject()});return d.promise()}},json:{create:function(e,d){var g=f(),b={created_by:"Tampermonkey",
version:"1",scripts:[],settings:d};e.forEach(function(a){var c={name:a.meta.name,options:a.options,storage:a.storage,enabled:a.settings.enabled,position:a.settings.position,file_url:a.meta.file_url,uuid:a.meta.uuid,source:l.Base64.encode(l.UTF8.encode(a.source))};["resources","requires"].forEach(function(b){var d=a[b];if(d&&d.length){var e=c[b]=[];d.forEach(function(a,b){if(void 0!==a.source){var c=a.meta,d=l.Base64.encode(l.UTF8.encode(a.source));e.push({meta:c,source:d})}})}});b.scripts.push(c)});
g.resolve(JSON.stringify(b));return g.promise()},read:function(e){var d=f(),g=function(b){if(b.trim()){var a=null;try{return a=JSON.parse(b),a.scripts=q.map(a.scripts,function(a){var b={meta:{uuid:a.uuid,name:a.name,file_url:a.file_url},settings:{enabled:a.enabled,position:a.position},options:a.options,storage:a.storage,source:l.UTF8.decode(l.Base64.decode(a.source))};["resources","requires"].forEach(function(c){var d=a[c];d&&d.length&&(b[c]=d.map(function(a){return{meta:a.meta,source:l.UTF8.decode(l.Base64.decode(a.source))}}))});
return b}),d.resolve({scripts:a.scripts,global_settings:a.settings})}catch(e){if(-1!=b.search("<body>")){var a=b.indexOf("<body>"),c=b.lastIndexOf("</body>");if(-1!=a&&-1!=c)return b=b.substr(a+6,c-(a+6)),g(b)}}}d.reject()};g(e);return d.promise()}}})});
