Skip to content

Instantly share code, notes, and snippets.

@notmasteryet
Created July 1, 2011 05:26
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save notmasteryet/1057924 to your computer and use it in GitHub Desktop.
Save notmasteryet/1057924 to your computer and use it in GitHub Desktop.
Hack for IE to support pdf.js
(function() {
try {
var a = new Uint8Array(1);
return; //no need
} catch(e) { }
function subarray(start, end) {
return this.slice(start, end);
}
function set_(array, offset) {
if (arguments.length < 2) offset = 0;
for (var i = 0, n = array.length; i < n; ++i, ++offset)
this[offset] = array[i] & 0xFF;
}
// we need typed arrays
function TypedArray(arg1) {
var result;
if (typeof arg1 === "number") {
result = new Array(arg1);
for (var i = 0; i < arg1; ++i)
result[i] = 0;
} else
result = arg1.slice(0);
result.subarray = subarray;
result.buffer = result;
result.byteLength = result.length;
result.set = set_;
if (typeof arg1 === "object" && arg1.buffer)
result.buffer = arg1.buffer;
return result;
}
window.Uint8Array = TypedArray;
window.Uint32Array = TypedArray;
window.Int32Array = TypedArray;
})();
(function() {
if ("response" in XMLHttpRequest.prototype ||
"mozResponseArrayBuffer" in XMLHttpRequest.prototype ||
"mozResponse" in XMLHttpRequest.prototype ||
"responseArrayBuffer" in XMLHttpRequest.prototype)
return;
Object.defineProperty(XMLHttpRequest.prototype, "response", {
get: function() {
return new Uint8Array( new VBArray(this.responseBody).toArray() );
}
});
})();
(function() {
if ("btoa" in window)
return;
var digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
window.btoa = function(chars) {
var buffer = "";
var i, n;
for (i = 0, n = chars.length; i < n; i += 3) {
var b1 = chars.charCodeAt(i) & 0xFF;
var b2 = chars.charCodeAt(i + 1) & 0xFF;
var b3 = chars.charCodeAt(i + 2) & 0xFF;
var d1 = b1 >> 2, d2 = ((b1 & 3) << 4) | (b2 >> 4);
var d3 = i + 1 < n ? ((b2 & 0xF) << 2) | (b3 >> 6) : 64;
var d4 = i + 2 < n ? (b3 & 0x3F) : 64;
buffer += digits.charAt(d1) + digits.charAt(d2) + digits.charAt(d3) + digits.charAt(d4);
}
return buffer;
};
})();
@SalmaKhater
Copy link

Hi notmasteryet ,
can you explain how use this code to support pdf.js in IE ?

Thanks

@notmasteryet
Copy link
Author

It's already landed as a part of the pdf.js project. Some pdf s can be opened via ie 9, e.g.tracemonkey paper

@sureshmuttagi
Copy link

can any one explain how we are getting the byte content. or just give some description about "mozResponse"

@Superdrac
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment