adapt FileSaver.js to allow including it as a content script in Firefox for Android add-ons

This commit is contained in:
minj
2013-10-21 00:09:59 +03:00
parent 0f804d9c12
commit 3109255efe
+17 -3
View File
@@ -14,7 +14,7 @@
/*! @source http://purl.eligrey.com/github/FileSaver.js/blob/master/FileSaver.js */
var saveAs = saveAs
|| (navigator.msSaveOrOpenBlob && navigator.msSaveOrOpenBlob.bind(navigator))
|| (typeof navigator !== 'undefined' && navigator.msSaveOrOpenBlob && navigator.msSaveOrOpenBlob.bind(navigator))
|| (function(view) {
"use strict";
var
@@ -116,9 +116,20 @@ var saveAs = saveAs
}
if (can_use_save_link) {
object_url = get_object_url(blob);
// FF for Android has a nasty garbage collection mechanism
// that turns all objects that are not pure javascript into 'deadObject'
// this means `doc` and `save_link` are unusable and need to be recreated
// `view` is usable though:
doc = view.document;
save_link = doc.createElementNS("http://www.w3.org/1999/xhtml", "a");
save_link.href = object_url;
save_link.download = name;
click(save_link);
var event = doc.createEvent("MouseEvents");
event.initMouseEvent(
"click", true, false, view, 0, 0, 0, 0, 0
, false, false, false, false, 0, null
);
save_link.dispatchEvent(event);
filesaver.readyState = filesaver.DONE;
dispatch_all();
return;
@@ -213,6 +224,9 @@ var saveAs = saveAs
view.addEventListener("unload", process_deletion_queue, false);
return saveAs;
}(self));
}(typeof self !== 'undefined' ? self : typeof this !== 'undefined' && this.content ? this.content : null));
// `self` is undefined in Firefox for Android content script context
// while `this` is nsIContentFrameMessageManager
// with an attribute `content` that corresponds to the window
if (typeof module !== 'undefined') module.exports = saveAs;