Registry.require(["promise","helper","convert"],function(){var f=Registry.get("promise"),q=Registry.get("helper"),l=Registry.get("convert"),h=function(){var b=f();Registry.vendor(["vendor/saveas/filesaver"],function(){h=f.Pledge;b.resolve()});return b.promise()},m=function(){var b=f();Registry.vendor(["vendor/zip_js/zip","vendor/zip_js/inflate","vendor/zip_js/deflate"],function(){m=f.Pledge;zip.workerScriptsPath=rea.extension.getURL("/vendor/zip_js/");b.resolve()});return b.promise()},k=function(){var b,
d=!1,e,c=function(){var a=f();return e=a};return{write:function(){var a=f();d=!1;zip.createWriter(new zip.BlobWriter,function(c){b=c;a.resolve(c)},a.reject);return a.promise()},open:function(a){var r=c();d=!0;zip.createReader(new zip.BlobReader(a),function(a){b=a;r.resolve(a)},function(a){e&&e.reject(a)});return r.promise()},entries:function(){var a=c();b.getEntries(function(c){a.resolve(c)});return a.promise()},get:function(a){var b=c(),e=new zip.TextWriter;a.getData(e,b.resolve);return b.promise()},
put:function(a,e,d){var f=c();try{b.add(a,new zip.TextReader(e),f.resolve,function(){},{lastModDate:d?new Date(d):void 0})}catch(k){f.reject(k)}return f.promise()},end:function(){var a=c();d?(b.close(),a.resolve()):b.close(a.resolve);return a.promise()}}}(),p={zip:{create:function(b,d){var e=f(),c=0;m().then(function(){return k.write()}).then(function(a){var d=f(),g={},n=function(a,c){var b=[a,c].join(".");if(g[b]){var e;do e=a+" ("+g[b]+")",b=[e,c].join("."),g[b]++;while(g[b]);return n(e,c)}g[b]=
1;return b},h=b.length,u=function(){if(!b.length)return d.resolve();var a=b.shift(),g=n(a.meta.name,"user.js"),s=n(a.meta.name,"options.json"),t=n(a.meta.name,"storage.json"),l=JSON.stringify({options:a.options,settings:a.settings,meta:a.meta}),m=a.storage?JSON.stringify(a.storage):null;c+=a.source.length;console.log("porter: add to zip",g,c);e.notify({item:h-b.length,of:h});k.put(g,a.source,a.meta.modified).then(function(){c+=l.length;console.log("porter: add to zip",s,c);return k.put(s,l)}).then(function(){if(!m)return f.Pledge();
c+=m.length;console.log("porter: add to zip",t,c);return k.put(t,m)}).fail(function(a){console.log("porter: add to zip failed",a)}).always(function(){console.log("porter: add to zip -> next round");window.setTimeout(u,5)})};u();return d.promise()}).then(function(){console.log("porter: add global props");return d?k.put("Tampermonkey.global.json",JSON.stringify(d)):f.Pledge()}).then(function(){return k.end()}).done(function(a){e.resolve(a)}).fail(function(){e.reject()});return e.promise()},read:function(b){var d=
f(),e;m().then(function(){return k.open(b)}).then(function(c){return k.entries()}).then(function(c){var a=f(),b={},g=c.length,n=function(){if(c.length){var f=c.shift();console.log("porter: read from zip",f.filename);k.get(f).done(function(a){var c=f.filename.match(/(.*)\.(storage\.json|options\.json|global\.json|user\.js)$/);if(c&&!(3>c.length))try{var d=c[1],g=c[2];b[d]=b[d]||{};"global.json"==g?e=JSON.parse(a):"user.js"==g?b[d].source=a:"options.json"==g?b[d].options=JSON.parse(a):"storage.json"==
g&&(b[d].storage=JSON.parse(a))}catch(h){console.warn("porter: read from zip failed",h)}}).always(function(){d.notify({item:g-c.length,of:g});window.setTimeout(n,5)})}else{var h=[];q.each(b,function(a,c){var b=a.options||{};b.source=a.source;b.storage=a.storage;h.push(b)});a.resolve({scripts:h,global_settings:e})}};n();return a.promise()}).done(function(c){d.resolve(c)}).fail(function(){d.reject()});return d.promise()},download:function(b,d){var e=f();h().then(function(){return p.zip.create(b,d).progress(function(c){e.notify(c)})}).done(function(c){saveAs(c,
"tmScripts.zip");e.resolve.apply(this,arguments)}).fail(function(){e.reject.apply(this,arguments)});return e.promise()}},plain:{download:function(b,d){return h().done(function(){var e=new Blob([d],{type:"text/plain"});saveAs(e,b+".user.js")})}},json:{create:function(b,d){var e=f();h().done(function(){var c={created_by:"Tampermonkey",version:"1",scripts:[],settings:d};b.forEach(function(a){a={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))};c.scripts.push(a)});e.resolve(JSON.stringify(c))});return e.promise()},read:function(b){var d=f();h().done(function(){var e=function(c){if(c.trim()){var a=null;try{return a=JSON.parse(c),a.scripts=q.map(a.scripts,function(a){a.source=l.UTF8.decode(l.Base64.decode(a.source));return a}),d.resolve({scripts:a.scripts,global_settings:a.settings})}catch(b){if(-1!=c.search("<body>")){var a=c.indexOf("<body>"),f=c.lastIndexOf("</body>");
if(-1!=a&&-1!=f)return c=c.substr(a+6,f-(a+6)),e(c)}}}d.reject()};e(b)});return d.promise()},download:function(b,d){return h().then(function(){return p.json.create(b,d)}).done(function(b){b=new Blob([b],{type:"text/plain"});saveAs(b,"tmScripts.txt")})}}};Registry.register("porter","58",p)});
