Mercurial > powerbox
diff extension/modules/content-injector.js @ 3:413435fc6202
We now show a notification box when a page calls window.powerbox.request().
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Thu, 06 Aug 2009 15:50:24 -0700 |
parents | 889c2fd4c9cf |
children | 1c02976d8809 |
line wrap: on
line diff
--- a/extension/modules/content-injector.js Thu Aug 06 14:53:41 2009 -0700 +++ b/extension/modules/content-injector.js Thu Aug 06 15:50:24 2009 -0700 @@ -4,8 +4,45 @@ const Ci = Components.interfaces; const Cu = Components.utils; +const REQUEST_BOX_NAME = "powerbox_superpower_request"; +const REQUEST_BOX_TEXT = "This web page wants superpowers."; +const REQUEST_BOX_BUTTON_TEXT = "Huh?"; + Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); +function getParentChromeWindow(window) { + var mainWindow = window.QueryInterface(Ci.nsIInterfaceRequestor) + .getInterface(Ci.nsIWebNavigation) + .QueryInterface(Ci.nsIDocShellTreeItem) + .rootTreeItem + .QueryInterface(Ci.nsIInterfaceRequestor) + .getInterface(Ci.nsIDOMWindow); + return mainWindow.wrappedJSObject; +} + +function showRequestNotificationBox(window, caps, callback) { + var tabbrowser = getParentChromeWindow(window).getBrowser(); + var browser = tabbrowser.getBrowserForDocument(window.document); + + if (browser) { + var nBox = tabbrowser.getNotificationBox(browser); + + var oldNotification = nBox.getNotificationWithValue(REQUEST_BOX_NAME); + if (oldNotification) + nBox.removeNotification(oldNotification); + + nBox.appendNotification(REQUEST_BOX_TEXT, + REQUEST_BOX_NAME, + null, + nBox.PRIORITY_INFO_MEDIUM, + [{accessKey: null, + // TODO: Implement a real callback. + callback: function() {}, + label: REQUEST_BOX_BUTTON_TEXT, + popup: null}]); + } +} + function buildRequestor(window) { return function request(caps, callback) { if (!(typeof(caps) == "object" || @@ -19,7 +56,7 @@ caps = new XPCSafeJSObjectWrapper(caps); callback = new XPCSafeJSObjectWrapper(callback); - // TODO: Present UI to user and inject requested capabilities. + showRequestNotificationBox(window, caps, callback); }; }