|
| 1 | +const {classes: Cc, interfaces: Ci, utils: Cu} = Components; |
| 2 | +Cu.import("resource://gre/modules/Services.jsm"); |
| 3 | +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
| 4 | + |
| 5 | +XPCOMUtils.defineLazyGetter(this, "sss", function () { |
| 6 | + return Cc["@mozilla.org/content/style-sheet-service;1"] |
| 7 | + .getService(Ci.nsIStyleSheetService); |
| 8 | +}); |
| 9 | + |
| 10 | + |
| 11 | +var addonData = null; // Set this in startup() |
| 12 | +var scopes = {}; |
| 13 | +function require(module) |
| 14 | +{ |
| 15 | + if (!(module in scopes)) { |
| 16 | + let url = addonData.resourceURI.spec + "packages/" + module + ".js"; |
| 17 | + scopes[module] = { |
| 18 | + require: require, |
| 19 | + exports: {}, |
| 20 | + Cc: Components.classes, |
| 21 | + Ci: Components.interfaces, |
| 22 | + Cu: Components.utils |
| 23 | + }; |
| 24 | + Services.scriptloader.loadSubScript(url, scopes[module]); |
| 25 | + } |
| 26 | + return scopes[module].exports; |
| 27 | +} |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | +var pubnub; |
| 32 | + |
| 33 | +function init() { |
| 34 | + pubnub = require("pubnub").init({ |
| 35 | + publish_key : "demo", |
| 36 | + subscribe_key : "demo" |
| 37 | + }); |
| 38 | +} |
| 39 | + |
| 40 | +var prefsPrefix = "extension.pubnub."; |
| 41 | +var idPrefix = "pubnub-"; |
| 42 | + |
| 43 | +function alert(string) { |
| 44 | + Services.prompt.alert(Services.wm.getMostRecentWindow("navigator:browser"), "", string); |
| 45 | +} |
| 46 | + |
| 47 | +function onSubscribeCommand(event) { |
| 48 | + !pubnub && init(); |
| 49 | + if (event.target.id != idPrefix + "buttonSubscribe") { |
| 50 | + return; |
| 51 | + } |
| 52 | + var win = Services.wm.getMostRecentWindow("navigator:browser"); |
| 53 | + var doc = win.document; |
| 54 | + var input_channel = {value: ""}; |
| 55 | + var input_presence = {value: false}; |
| 56 | + var result1 = Services.prompt.prompt(null, "PubNub", "Enter Channel Name :", input_channel, null, {}); |
| 57 | + var result2 = Services.prompt.confirm(null, "PubNub", "Presence ? :", input_presence, null, {}); |
| 58 | + |
| 59 | + |
| 60 | + var window = Services.wm.getMostRecentWindow("navigator:browser"); |
| 61 | + |
| 62 | + var params = { |
| 63 | + channel : input_channel.value, |
| 64 | + callback : function(m){ |
| 65 | + var window = Services.wm.getMostRecentWindow("navigator:browser"); |
| 66 | + var notification = new window.Notification("PubNub", { |
| 67 | + body: JSON.stringify(m), |
| 68 | + tag: "pubnub", |
| 69 | + icon: "chrome://pubnub/skin/icon.png" |
| 70 | + }); |
| 71 | + } |
| 72 | + }; |
| 73 | + |
| 74 | + if (input_presence.value === true) { |
| 75 | + params['presence'] = function(m){ |
| 76 | + var window = Services.wm.getMostRecentWindow("navigator:browser"); |
| 77 | + var notification = new window.Notification("PubNub", { |
| 78 | + body: JSON.stringify(m), |
| 79 | + tag: "pubnub", |
| 80 | + icon: "chrome://pubnub/skin/icon.png" |
| 81 | + }); |
| 82 | + }; |
| 83 | + } |
| 84 | + |
| 85 | + if (result1) { |
| 86 | + pubnub.subscribe(params); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +function onPublishCommand(event) { |
| 91 | + !pubnub && init(); |
| 92 | + if (event.target.id != idPrefix + "buttonPublish") { |
| 93 | + return; |
| 94 | + } |
| 95 | + var win = Services.wm.getMostRecentWindow("navigator:browser"); |
| 96 | + var doc = win.document; |
| 97 | + var input_channel = {value: ""}; |
| 98 | + var input_message = {value: ""}; |
| 99 | + var result1 = Services.prompt.prompt(null, "PubNub", "Enter Channel Name:", input_channel, null, {}); |
| 100 | + var result2 = Services.prompt.prompt(null, "PubNub", "Enter a message:", input_message, null, {}); |
| 101 | + |
| 102 | + if (result1 && result2) { |
| 103 | + pubnub.publish({ |
| 104 | + channel : input_channel.value, |
| 105 | + message : input_message.value, |
| 106 | + callback : function(m){ |
| 107 | + var window = Services.wm.getMostRecentWindow("navigator:browser"); |
| 108 | + var notification = new window.Notification("PubNub", { |
| 109 | + body: JSON.stringify(m), |
| 110 | + tag: "pubnub", |
| 111 | + icon: "chrome://pubnub/skin/icon.png" |
| 112 | + }); |
| 113 | + } |
| 114 | + }) |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | + |
| 119 | +/* Save the current toolbarbutton position in preferences |
| 120 | +*/ |
| 121 | +function saveButtonPosition(event) { |
| 122 | + var button = event.target.ownerDocument.getElementById(idPrefix + "buttonSubscribe"); |
| 123 | + if (button) { |
| 124 | + Services.prefs.setCharPref(prefsPrefix + "toolbarID", button.parentNode.id); |
| 125 | + Services.prefs.setCharPref(prefsPrefix + "nextSiblingID", button.nextSibling.id); |
| 126 | + } else { |
| 127 | + Services.prefs.clearUserPref(prefsPrefix + "toolbarID"); |
| 128 | + Services.prefs.clearUserPref(prefsPrefix + "nextSiblingID"); |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +function loadIntoWindow(window) { |
| 133 | + if (!window || window.document.documentElement.getAttribute("windowtype") != "navigator:browser") |
| 134 | + return; |
| 135 | + |
| 136 | + let doc = window.document; |
| 137 | + |
| 138 | + let toolbox = doc.getElementById("navigator-toolbox"); |
| 139 | + if (toolbox) { |
| 140 | + let button1 = doc.createElement("toolbarbutton"); |
| 141 | + button1.setAttribute("id", idPrefix + "buttonSubscribe"); |
| 142 | + button1.setAttribute("label", "PubNub Subscribe"); |
| 143 | + button1.setAttribute("type", "button"); |
| 144 | + button1.setAttribute("class", |
| 145 | + "pubnub-button"); |
| 146 | + //button1.setAttribute("class", "toolbarbutton-1 chromeclass-toolbar-additional"); |
| 147 | + button1.setAttribute("orient", "horizontal"); |
| 148 | + toolbox.palette.appendChild(button1); |
| 149 | + if (Services.prefs.prefHasUserValue(prefsPrefix + "toolbarID")) { |
| 150 | + var toolbar = doc.getElementById(Services.prefs.getCharPref(prefsPrefix + "toolbarID")); |
| 151 | + var nextSibling = null; |
| 152 | + try { |
| 153 | + nextSibling = doc.getElementById(Services.prefs.getCharPref(prefsPrefix + "nextSiblingID")); |
| 154 | + } catch (ex) {} |
| 155 | + toolbar.insertItem(idPrefix + "buttonSubscribe", nextSibling); |
| 156 | + } |
| 157 | + window.addEventListener("aftercustomization", saveButtonPosition, false); |
| 158 | + window.addEventListener("command", onSubscribeCommand, false); |
| 159 | + |
| 160 | + let button2 = doc.createElement("toolbarbutton"); |
| 161 | + button2.setAttribute("id", idPrefix + "buttonPublish"); |
| 162 | + button2.setAttribute("label", "PubNub Publish"); |
| 163 | + button2.setAttribute("class", |
| 164 | + "pubnub-button"); |
| 165 | + //button2.setAttribute("class", "toolbarbutton-1 chromeclass-toolbar-additional"); |
| 166 | + button2.setAttribute("orient", "horizontal"); |
| 167 | + toolbox.palette.appendChild(button2); |
| 168 | + if (Services.prefs.prefHasUserValue(prefsPrefix + "toolbarID")) { |
| 169 | + var toolbar = doc.getElementById(Services.prefs.getCharPref(prefsPrefix + "toolbarID")); |
| 170 | + var nextSibling = null; |
| 171 | + try { |
| 172 | + nextSibling = doc.getElementById(Services.prefs.getCharPref(prefsPrefix + "nextSiblingID")); |
| 173 | + } catch (ex) {} |
| 174 | + toolbar.insertItem(idPrefix + "buttonPublish", nextSibling); |
| 175 | + } |
| 176 | + window.addEventListener("aftercustomization", saveButtonPosition, false); |
| 177 | + window.addEventListener("command", onPublishCommand, false); |
| 178 | + |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +function unloadFromWindow(window) { |
| 183 | + if (!window || window.document.documentElement.getAttribute("windowtype") != "navigator:browser") |
| 184 | + return; |
| 185 | + let doc = window.document; |
| 186 | +} |
| 187 | + |
| 188 | +var windowListener = { |
| 189 | + onOpenWindow: function(aWindow) { |
| 190 | + // Wait for the window to finish loading |
| 191 | + let domWindow = aWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowInternal || Ci.nsIDOMWindow); |
| 192 | + domWindow.addEventListener("load", function() { |
| 193 | + domWindow.removeEventListener("load", arguments.callee, false); |
| 194 | + loadIntoWindow(domWindow); |
| 195 | + }, false); |
| 196 | + }, |
| 197 | + |
| 198 | + onCloseWindow: function(aWindow) {}, |
| 199 | + onWindowTitleChange: function(aWindow, aTitle) {} |
| 200 | +}; |
| 201 | + |
| 202 | +function startup(aData, aReason) { |
| 203 | + addonData = aData; |
| 204 | + sss.loadAndRegisterSheet(Services.io.newURI("chrome://pubnub/skin/pubnub.css", null, null), sss.USER_SHEET); |
| 205 | + |
| 206 | + let wm = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator); |
| 207 | + |
| 208 | + // Load into any existing windows |
| 209 | + let windows = wm.getEnumerator("navigator:browser"); |
| 210 | + while (windows.hasMoreElements()) { |
| 211 | + let domWindow = windows.getNext().QueryInterface(Ci.nsIDOMWindow); |
| 212 | + loadIntoWindow(domWindow); |
| 213 | + } |
| 214 | + |
| 215 | + // Load into any new windows |
| 216 | + wm.addListener(windowListener); |
| 217 | + init(); |
| 218 | + } |
| 219 | + |
| 220 | +function shutdown(aData, aReason) { |
| 221 | + // When the application is shutting down we normally don't have to clean |
| 222 | + // up any UI changes made |
| 223 | + if (aReason == APP_SHUTDOWN) |
| 224 | + return; |
| 225 | + |
| 226 | + sss.unregisterSheet(Services.io.newURI("chrome://pubnub/skin/pubnub.css", null, null), sss.USER_SHEET); |
| 227 | + |
| 228 | + let wm = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator); |
| 229 | + |
| 230 | + // Stop listening for new windows |
| 231 | + wm.removeListener(windowListener); |
| 232 | + |
| 233 | + // Unload from any existing windows |
| 234 | + let windows = wm.getEnumerator("navigator:browser"); |
| 235 | + while (windows.hasMoreElements()) { |
| 236 | + let domWindow = windows.getNext().QueryInterface(Ci.nsIDOMWindow); |
| 237 | + unloadFromWindow(domWindow); |
| 238 | + } |
| 239 | + for (let key in scopes) { |
| 240 | + let scope = scopes[key]; |
| 241 | + for (let v in scope) |
| 242 | + scope[v] = null; |
| 243 | + } |
| 244 | + scopes = null; |
| 245 | +} |
| 246 | + |
| 247 | +function install(aData, aReason) { |
| 248 | + Services.prefs.setCharPref(prefsPrefix + "toolbarID", "nav-bar"); |
| 249 | + var win = Services.wm.getMostRecentWindow("navigator:browser"); |
| 250 | + if (win) { |
| 251 | +// win.openUILinkIn("FIRSTRUNPAGE", "tab"); |
| 252 | + } |
| 253 | +} |
| 254 | +function uninstall(aData, aReason) { |
| 255 | +} |
0 commit comments