diff --git a/src/js/ConfigStorage.js b/src/js/ConfigStorage.js index b6cef353..bcf6ea62 100644 --- a/src/js/ConfigStorage.js +++ b/src/js/ConfigStorage.js @@ -5,6 +5,9 @@ var ConfigStorage = { // key can be one string, or array of strings get: function(key, callback) { + if (GUI.isChromeApp()) { + chrome.storage.local.get(key,callback); + } else { //console.log('Abstraction.get',key); if (Array.isArray(key)) { var obj = {}; @@ -30,12 +33,17 @@ var ConfigStorage = { callback({}); } } + } }, // set takes an object like {'userLanguageSelect':'DEFAULT'} set: function(input) { + if (GUI.isChromeApp()) { + chrome.storage.local.set(input); + } else { //console.log('Abstraction.set',input); Object.keys(input).forEach(function (element) { window.localStorage.setItem(element, JSON.stringify(input)); }); + } } } diff --git a/src/js/gui.js b/src/js/gui.js index e508fe4d..4de1126c 100644 --- a/src/js/gui.js +++ b/src/js/gui.js @@ -59,8 +59,27 @@ var GUI_control = function () { else if (navigator.appVersion.indexOf("Linux") != -1) this.operating_system = "Linux"; else if (navigator.appVersion.indexOf("X11") != -1) this.operating_system = "UNIX"; else this.operating_system = "Unknown"; + + // Check the method of execution + this.nwGui = null; + try { + this.nwGui = require('nw.gui'); + this.Mode = GUI_Modes.NWJS; + } catch (ex) { + if (window.chrome && chrome.storage && chrome.storage.local) { + this.Mode = GUI_Modes.ChromeApp; + } else { + this.Mode = GUI_Modes.Other; + } + } }; +const GUI_Modes = { + NWJS: "NW.js", + ChromeApp: "Chrome", + Other: "Other" +} + // Timer managing methods // name = string @@ -358,5 +377,16 @@ GUI_control.prototype.selectDefaultTabWhenConnected = function() { }); }; +GUI_control.prototype.isChromeApp = function () { + return this.Mode == GUI_Modes.ChromeApp; +} +GUI_control.prototype.isNWJS = function () { + return this.Mode == GUI_Modes.NWJS; +} +GUI_control.prototype.isOther = function () { + return this.Mode == GUI_Modes.Other; +} + + // initialize object into GUI variable var GUI = new GUI_control(); diff --git a/src/js/main.js b/src/js/main.js index 579f72ba..39f58263 100644 --- a/src/js/main.js +++ b/src/js/main.js @@ -1,7 +1,5 @@ 'use strict'; -var nwGui = getNwGui(); - var googleAnalytics = analytics; var analytics = undefined; @@ -16,17 +14,6 @@ $(document).ready(function () { }); }); -function getNwGui() { - var gui = null; - try { - gui = require('nw.gui'); - } catch (ex) { - console.log("Could not require 'nw.gui', maybe inside chrome"); - } - - return gui; -} - function checkSetupAnalytics(callback) { if (!analytics) { setTimeout(function () { @@ -44,7 +31,7 @@ function checkSetupAnalytics(callback) { }; function getBuildType() { - return nwGui ? 'NW.js' : 'Chrome'; + return GUI.Mode; } function setupAnalytics(result) { @@ -79,8 +66,8 @@ function setupAnalytics(result) { analytics.sendEvent(analytics.EVENT_CATEGORIES.APPLICATION, 'AppClose', { sessionControl: 'end' }) } - if (nwGui) { - var win = nwGui.Window.get(); + if (GUI.isNWJS()) { + var win = GUI.nwGui.Window.get(); win.on('close', function () { sendCloseEvent(); @@ -90,7 +77,7 @@ function setupAnalytics(result) { // do not open the window policy.ignore(); // and open it in external browser - nwGui.Shell.openExternal(url); + GUI.nwGui.Shell.openExternal(url); }); } else { // Looks like we're in Chrome - but the event does not actually get fired @@ -293,7 +280,7 @@ function startProcess() { TABS.onboard_logging.initialize(content_ready); break; case 'cli': - TABS.cli.initialize(content_ready, nwGui); + TABS.cli.initialize(content_ready, GUI.nwGui); break; default: