Skip to content

Commit ca53342

Browse files
authored
Chrome Apps don't get window.localStorage (#1560)
Chrome Apps don't get window.localStorage
2 parents 11de751 + 9fa5cca commit ca53342

File tree

3 files changed

+43
-18
lines changed

3 files changed

+43
-18
lines changed

src/js/ConfigStorage.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
var ConfigStorage = {
66
// key can be one string, or array of strings
77
get: function(key, callback) {
8+
if (GUI.isChromeApp()) {
9+
chrome.storage.local.get(key,callback);
10+
} else {
811
//console.log('Abstraction.get',key);
912
if (Array.isArray(key)) {
1013
var obj = {};
@@ -30,12 +33,17 @@ var ConfigStorage = {
3033
callback({});
3134
}
3235
}
36+
}
3337
},
3438
// set takes an object like {'userLanguageSelect':'DEFAULT'}
3539
set: function(input) {
40+
if (GUI.isChromeApp()) {
41+
chrome.storage.local.set(input);
42+
} else {
3643
//console.log('Abstraction.set',input);
3744
Object.keys(input).forEach(function (element) {
3845
window.localStorage.setItem(element, JSON.stringify(input));
3946
});
47+
}
4048
}
4149
}

src/js/gui.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,27 @@ var GUI_control = function () {
5959
else if (navigator.appVersion.indexOf("Linux") != -1) this.operating_system = "Linux";
6060
else if (navigator.appVersion.indexOf("X11") != -1) this.operating_system = "UNIX";
6161
else this.operating_system = "Unknown";
62+
63+
// Check the method of execution
64+
this.nwGui = null;
65+
try {
66+
this.nwGui = require('nw.gui');
67+
this.Mode = GUI_Modes.NWJS;
68+
} catch (ex) {
69+
if (window.chrome && chrome.storage && chrome.storage.local) {
70+
this.Mode = GUI_Modes.ChromeApp;
71+
} else {
72+
this.Mode = GUI_Modes.Other;
73+
}
74+
}
6275
};
6376

77+
const GUI_Modes = {
78+
NWJS: "NW.js",
79+
ChromeApp: "Chrome",
80+
Other: "Other"
81+
}
82+
6483
// Timer managing methods
6584

6685
// name = string
@@ -358,5 +377,16 @@ GUI_control.prototype.selectDefaultTabWhenConnected = function() {
358377
});
359378
};
360379

380+
GUI_control.prototype.isChromeApp = function () {
381+
return this.Mode == GUI_Modes.ChromeApp;
382+
}
383+
GUI_control.prototype.isNWJS = function () {
384+
return this.Mode == GUI_Modes.NWJS;
385+
}
386+
GUI_control.prototype.isOther = function () {
387+
return this.Mode == GUI_Modes.Other;
388+
}
389+
390+
361391
// initialize object into GUI variable
362392
var GUI = new GUI_control();

src/js/main.js

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
'use strict';
22

3-
var nwGui = getNwGui();
4-
53
var googleAnalytics = analytics;
64
var analytics = undefined;
75

@@ -16,17 +14,6 @@ $(document).ready(function () {
1614
});
1715
});
1816

19-
function getNwGui() {
20-
var gui = null;
21-
try {
22-
gui = require('nw.gui');
23-
} catch (ex) {
24-
console.log("Could not require 'nw.gui', maybe inside chrome");
25-
}
26-
27-
return gui;
28-
}
29-
3017
function checkSetupAnalytics(callback) {
3118
if (!analytics) {
3219
setTimeout(function () {
@@ -44,7 +31,7 @@ function checkSetupAnalytics(callback) {
4431
};
4532

4633
function getBuildType() {
47-
return nwGui ? 'NW.js' : 'Chrome';
34+
return GUI.Mode;
4835
}
4936

5037
function setupAnalytics(result) {
@@ -79,8 +66,8 @@ function setupAnalytics(result) {
7966
analytics.sendEvent(analytics.EVENT_CATEGORIES.APPLICATION, 'AppClose', { sessionControl: 'end' })
8067
}
8168

82-
if (nwGui) {
83-
var win = nwGui.Window.get();
69+
if (GUI.isNWJS()) {
70+
var win = GUI.nwGui.Window.get();
8471
win.on('close', function () {
8572
sendCloseEvent();
8673

@@ -90,7 +77,7 @@ function setupAnalytics(result) {
9077
// do not open the window
9178
policy.ignore();
9279
// and open it in external browser
93-
nwGui.Shell.openExternal(url);
80+
GUI.nwGui.Shell.openExternal(url);
9481
});
9582
} else {
9683
// Looks like we're in Chrome - but the event does not actually get fired
@@ -293,7 +280,7 @@ function startProcess() {
293280
TABS.onboard_logging.initialize(content_ready);
294281
break;
295282
case 'cli':
296-
TABS.cli.initialize(content_ready, nwGui);
283+
TABS.cli.initialize(content_ready, GUI.nwGui);
297284
break;
298285

299286
default:

0 commit comments

Comments
 (0)