Created
May 20, 2013 20:46
-
-
Save ispedals/5615409 to your computer and use it in GitHub Desktop.
Example addon showing how to call the filepicker on Firefox for Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Cc = Components.classes; | |
const Ci = Components.interfaces; | |
const Cu = Components.utils; | |
Cu.import("resource://gre/modules/Services.jsm"); | |
function log(str) { Services.console.logStringMessage(str); } | |
function showToast(aWindow, message) { aWindow.NativeWindow.toast.show(message, "short"); } | |
var g; | |
function show(window) { | |
var filePicker = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker); | |
filePicker.init(window.BrowserApp, null, 1); | |
var rv =filePicker.show(); | |
if (rv == Ci.nsIFilePicker.returnOK || rv == Ci.nsIFilePicker.returnReplace) { | |
var file = filePicker.fileURL.spec; | |
showToast(window, file); | |
} | |
} | |
function loadIntoWindow(window) { | |
if (!window) | |
return; | |
g=window.NativeWindow.menu.add("FilePicker", null, show(window)); | |
} | |
function unloadFromWindow(window) { | |
if (!window) | |
return; | |
window.NativeWindow.menu.remove(g); | |
} | |
/** | |
* bootstrap.js API | |
*/ | |
var windowListener = { | |
onOpenWindow: function(aWindow) { | |
// Wait for the window to finish loading | |
let domWindow = aWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowInternal || Ci.nsIDOMWindow); | |
domWindow.addEventListener("load", function() { | |
domWindow.removeEventListener("load", arguments.callee, false); | |
loadIntoWindow(domWindow); | |
}, false); | |
}, | |
onCloseWindow: function(aWindow) { | |
}, | |
onWindowTitleChange: function(aWindow, aTitle) { | |
} | |
}; | |
function startup(aData, aReason) { | |
// Load into any existing windows | |
let windows = Services.wm.getEnumerator("navigator:browser"); | |
while (windows.hasMoreElements()) { | |
let domWindow = windows.getNext().QueryInterface(Ci.nsIDOMWindow); | |
loadIntoWindow(domWindow); | |
} | |
// Load into any new windows | |
Services.wm.addListener(windowListener); | |
} | |
function shutdown(aData, aReason) { | |
// Stop listening for new windows | |
Services.wm.removeListener(windowListener); | |
// Unload from any existing windows | |
let windows = Services.wm.getEnumerator("navigator:browser"); | |
while (windows.hasMoreElements()) { | |
let domWindow = windows.getNext().QueryInterface(Ci.nsIDOMWindow); | |
unloadFromWindow(domWindow); | |
} | |
} | |
function install(aData, aReason) { | |
} | |
function uninstall(aData, aReason) { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment