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