Blob.js 2.3 KB

1
  1. /* @source http://purl.eligrey.com/github/Blob.js/blob/master/Blob.js */(function(c){c.URL=c.URL||c.webkitURL;if(c.Blob&&c.URL)try{new Blob;return}catch(b){return}var p=c.BlobBuilder||c.WebKitBlobBuilder||c.MozBlobBuilder||function(b){var c=function(a){return Object.prototype.toString.call(a).match(/^\[object\s(.*)\]$/)[1]},g=function(){this.data=[]},e=function(a,d,b){this.data=a;this.size=a.length;this.type=d;this.encoding=b},h=g.prototype,k=e.prototype,q=b.FileReaderSync,r=function(a){this.code=this[this.name=a]},t="NOT_FOUND_ERR SECURITY_ERR ABORT_ERR NOT_READABLE_ERR ENCODING_ERR NO_MODIFICATION_ALLOWED_ERR INVALID_STATE_ERR SYNTAX_ERR".split(" "),l=t.length,f=b.URL||b.webkitURL||b,u=f.createObjectURL,v=f.revokeObjectURL,m=f,w=b.btoa,x=b.atob,p=b.ArrayBuffer,n=b.Uint8Array;for(e.fake=k.fake=!0;l--;)r.prototype[t[l]]=l+1;f.createObjectURL||(m=b.URL={});m.createObjectURL=function(a){var d=a.type;null===d&&(d="application/octet-stream");if(a instanceof e)return d="data:"+d,"base64"===a.encoding?d+";base64,"+a.data:"URI"===a.encoding?d+","+decodeURIComponent(a.data):w?d+";base64,"+w(a.data):d+","+encodeURIComponent(a.data);if(u)return u.call(f,a)};m.revokeObjectURL=function(a){"data:"!==a.substring(0,5)&&v&&v.call(f,a)};h.append=function(a){var d=this.data;if(n&&(a instanceof p||a instanceof n)){var b="";a=new n(a);for(var f=0,g=a.length;f<g;f++)b+=String.fromCharCode(a[f]);d.push(b)}else if("Blob"===c(a)||"File"===c(a))if(q)b=new q,d.push(b.readAsBinaryString(a));else throw new r("NOT_READABLE_ERR");else a instanceof e?"base64"===a.encoding&&x?d.push(x(a.data)):"URI"===a.encoding?d.push(decodeURIComponent(a.data)):"raw"===a.encoding&&d.push(a.data):("string"!==typeof a&&(a+=""),d.push(unescape(encodeURIComponent(a))))};h.getBlob=function(a){arguments.length||(a=null);return new e(this.data.join(""),a,"raw")};h.toString=function(){return"[object BlobBuilder]"};k.slice=function(a,b,c){var d=arguments.length;3>d&&(c=null);return new e(this.data.slice(a,1<d?b:this.data.length),c,this.encoding)};k.toString=function(){return"[object Blob]"};k.close=function(){this.size=0;delete this.data};return g}(c);c.Blob=function(b,c){c=c?c.type||"":"";var g=new p;if(b)for(var e=0,h=b.length;e<h;e++)g.append(b[e]);return g.getBlob(c)}})("undefined"!==typeof self&&self||"undefined"!==typeof window&&window||this.content||this);