diff --git a/src/client/index.mjs b/src/client/index.mjs index ff95ffbc..e9599d3a 100644 --- a/src/client/index.mjs +++ b/src/client/index.mjs @@ -29,7 +29,10 @@ function init() { } if (document.requestStorageAccess) { - document.requestStorageAccess().then(init, init) + document.requestStorageAccess().then(init, ()=>{ + console.error('Could not obtain storage access, the app might not work correctly unless the browser\'s settings are updated') + init() + }) } else { init() } diff --git a/src/client/managers/cache.mjs b/src/client/managers/cache.mjs index 0d4a2b1e..b28ae0a7 100644 --- a/src/client/managers/cache.mjs +++ b/src/client/managers/cache.mjs @@ -4,17 +4,31 @@ class Cache { set(key, value, persistent = true) { - var storage = persistent ? localStorage : sessionStorage + try { + + var storage = persistent ? localStorage : sessionStorage + + storage.setItem(keyPrefix + key, JSON.stringify(value)) + + } catch(e){ + console.error(`Could not write to ${persistent ? 'localStorage' : 'sessionStorage'}`) + } - storage.setItem(keyPrefix + key, JSON.stringify(value)) } get(key, persistent = true) { - var storage = persistent ? localStorage : sessionStorage + try { - return JSON.parse(storage.getItem(keyPrefix + key)) + var storage = persistent ? localStorage : sessionStorage + + return JSON.parse(storage.getItem(keyPrefix + key)) + + } catch(e){ + console.error(`Could not read from ${persistent ? 'localStorage' : 'sessionStorage'}`) + return null + } } diff --git a/src/client/ui/ui-code-editor.mjs b/src/client/ui/ui-code-editor.mjs index ceac60c2..b0f91a5d 100644 --- a/src/client/ui/ui-code-editor.mjs +++ b/src/client/ui/ui-code-editor.mjs @@ -207,6 +207,9 @@ class CodeEditor { if (this.language === 'javascript') { let globals = {} + for (var k in scriptGlobals) { + globals[k] = true + } if (this.name === 'onKeyboard') { globals = {type: true, key: true, code: true, ctrl: true, shift: true, alt: true, meta: true} } else if (this.name === 'onCreate') { @@ -230,7 +233,7 @@ class CodeEditor { globals.this = false if (this.syntaxChecker !== false) editor.getSession().$worker.send('changeOptions', [{ - globals: {...scriptGlobals, ...globals} + globals: globals }]) } diff --git a/src/client/widgets/scripts/script-vm.mjs b/src/client/widgets/scripts/script-vm.mjs index 9f862b54..4170bedc 100644 --- a/src/client/widgets/scripts/script-vm.mjs +++ b/src/client/widgets/scripts/script-vm.mjs @@ -53,9 +53,7 @@ class ScriptVm extends Vm { registerGlobals() { - super.registerGlobals() - - this.sandbox.contentWindow.set = (id, value, extraOptions = {send: true, sync: true})=>{ + this.globals.set = (id, value, extraOptions = {send: true, sync: true})=>{ this.checkContext('set') @@ -102,7 +100,7 @@ class ScriptVm extends Vm { } - this.sandbox.contentWindow.setVar = (id, name, value)=>{ + this.globals.setVar = (id, name, value)=>{ this.checkContext('setVar') @@ -137,7 +135,7 @@ class ScriptVm extends Vm { } - this.sandbox.contentWindow.send = (target, address, ...args)=>{ + this.globals.send = (target, address, ...args)=>{ this.checkContext('send') @@ -165,7 +163,7 @@ class ScriptVm extends Vm { } - this.sandbox.contentWindow.get = (id)=>{ + this.globals.get = (id)=>{ this.checkContext('get') @@ -184,7 +182,7 @@ class ScriptVm extends Vm { } - this.sandbox.contentWindow.getVar = (id, name)=>{ + this.globals.getVar = (id, name)=>{ this.checkContext('getVar') @@ -198,7 +196,7 @@ class ScriptVm extends Vm { } - this.sandbox.contentWindow.getProp = (id, prop)=>{ + this.globals.getProp = (id, prop)=>{ this.checkContext('getProp') @@ -213,7 +211,7 @@ class ScriptVm extends Vm { } - this.sandbox.contentWindow.updateProp = (id, prop)=>{ + this.globals.updateProp = (id, prop)=>{ this.checkContext('updateProp') @@ -228,7 +226,7 @@ class ScriptVm extends Vm { } - this.sandbox.contentWindow.updateCanvas = (id)=>{ + this.globals.updateCanvas = (id)=>{ this.checkContext('updateCanvas') @@ -241,7 +239,7 @@ class ScriptVm extends Vm { } - this.sandbox.contentWindow.getIndex = (id = 'this')=>{ + this.globals.getIndex = (id = 'this')=>{ this.checkContext('getIndex') @@ -250,7 +248,7 @@ class ScriptVm extends Vm { } - this.sandbox.contentWindow.getScroll = (id)=>{ + this.globals.getScroll = (id)=>{ this.checkContext('getScroll') @@ -266,7 +264,7 @@ class ScriptVm extends Vm { } - this.sandbox.contentWindow.setScroll = (id, x, y)=>{ + this.globals.setScroll = (id, x, y)=>{ this.checkContext('setScroll') @@ -280,7 +278,7 @@ class ScriptVm extends Vm { } - this.sandbox.contentWindow.httpGet = (url, callback)=>{ + this.globals.httpGet = (url, callback)=>{ this.checkContext('httpGet') @@ -301,7 +299,7 @@ class ScriptVm extends Vm { } - this.sandbox.contentWindow.stateGet = (filter)=>{ + this.globals.stateGet = (filter)=>{ this.checkContext('stateGet') @@ -321,7 +319,7 @@ class ScriptVm extends Vm { } - this.sandbox.contentWindow.stateSet = (state, extraOptions = {})=>{ + this.globals.stateSet = (state, extraOptions = {})=>{ this.checkContext('stateSet') @@ -331,7 +329,7 @@ class ScriptVm extends Vm { } - this.sandbox.contentWindow.storage = { + this.globals.storage = { setItem: (k, v)=>{ this.checkContext('storage.setItem') @@ -352,7 +350,7 @@ class ScriptVm extends Vm { } - this.sandbox.contentWindow.setTimeout = (id, callback, timeout)=>{ + this.globals.setTimeout = (id, callback, timeout)=>{ this.checkContext('setTimeout') @@ -386,7 +384,7 @@ class ScriptVm extends Vm { } - this.sandbox.contentWindow.clearTimeout = (id)=>{ + this.globals.clearTimeout = (id)=>{ this.checkContext('clearTimeout') @@ -397,7 +395,7 @@ class ScriptVm extends Vm { } - this.sandbox.contentWindow.setInterval = (id, callback, timeout)=>{ + this.globals.setInterval = (id, callback, timeout)=>{ this.checkContext('setInterval') @@ -430,7 +428,7 @@ class ScriptVm extends Vm { } - this.sandbox.contentWindow.clearInterval = (id)=>{ + this.globals.clearInterval = (id)=>{ this.checkContext('clearInterval') @@ -442,7 +440,7 @@ class ScriptVm extends Vm { } - this.sandbox.contentWindow.setFocus = (id)=>{ + this.globals.setFocus = (id)=>{ this.checkContext('setFocus') @@ -461,7 +459,7 @@ class ScriptVm extends Vm { } - this.sandbox.contentWindow.unfocus = ()=>{ + this.globals.unfocus = ()=>{ this.checkContext('unfocus') @@ -471,7 +469,7 @@ class ScriptVm extends Vm { } - this.sandbox.contentWindow.setFocusable = (focusable)=>{ + this.globals.setFocusable = (focusable)=>{ this.checkContext('setFocusable') setFOCUSABLE(!!focusable) @@ -492,7 +490,7 @@ class ScriptVm extends Vm { } } - this.sandbox.contentWindow.toolbar = (...args)=>{ + this.globals.toolbar = (...args)=>{ this.checkContext('toolbar') @@ -524,7 +522,7 @@ class ScriptVm extends Vm { } - this.sandbox.contentWindow.getToolbar = (id)=>{ + this.globals.getToolbar = (id)=>{ this.checkContext('toolbar') @@ -538,7 +536,7 @@ class ScriptVm extends Vm { } - this.sandbox.contentWindow.openUrl = (url)=>{ + this.globals.openUrl = (url)=>{ this.checkContext('openUrl') @@ -548,7 +546,7 @@ class ScriptVm extends Vm { } - this.sandbox.contentWindow.runAs = (id, callback)=>{ + this.globals.runAs = (id, callback)=>{ this.checkContext('runAs') @@ -569,7 +567,7 @@ class ScriptVm extends Vm { } - this.sandbox.contentWindow.reload = (keepState = true)=>{ + this.globals.reload = (keepState = true)=>{ this.checkContext('reload') @@ -586,16 +584,16 @@ class ScriptVm extends Vm { } - this.sandbox.contentWindow.Image = Image + this.globals.Image = Image - this.sandbox.contentWindow.getNavigator = ()=>{ + this.globals.getNavigator = ()=>{ this.checkContext('getNavigator') return navigator } - this.sandbox.contentWindow.browseFile = (options, callback)=>{ + this.globals.browseFile = (options, callback)=>{ this.checkContext('browseFile') if (typeof options === 'function') { callback = options @@ -629,18 +627,7 @@ class ScriptVm extends Vm { } } - - this.globals = {} - - for (var imports of ['set', 'get', 'getProp', 'getIndex', 'updateProp', 'send', 'httpGet', 'stateGet', 'stateSet', 'storage', - 'setInterval', 'clearInterval', 'setTimeout', 'clearTimeout', 'setFocus', 'unfocus' , 'setFocusable', 'setScroll', 'getScroll', 'toolbar', - 'getToolbar', 'openUrl', 'getVar', 'setVar', 'runAs', 'reload', 'Image', 'updateCanvas', 'getNavigator', 'browseFile']) { - this.globals[imports] = true - } - - for (var k in this.sandbox.contentWindow) { - if (window[k] === undefined) this.globals[k] = true - } + super.registerGlobals() } diff --git a/src/client/widgets/vm.mjs b/src/client/widgets/vm.mjs index 294b9e53..4a8b2cbd 100644 --- a/src/client/widgets/vm.mjs +++ b/src/client/widgets/vm.mjs @@ -52,6 +52,7 @@ class Vm { this.sandbox.contentWindow.document.close() // global context + this.globals = {} this.registerGlobals() // detach sandbox @@ -74,7 +75,7 @@ class Vm { registerGlobals() { - this.sandbox.contentWindow.console = { + this.globals.console = { ...console, log: (...msg)=>{ msg = msg.map(m => typeof m == 'object' ? JSON.stringify(m) : m) @@ -82,11 +83,16 @@ class Vm { } } - this.sandbox.contentWindow.setTimeout = - this.sandbox.contentWindow.setInterval = ()=>{ + this.globals.setTimeout = + this.globals.setInterval = ()=>{ throw 'setTimeout and setInterval can\'t be used in the JS sandbox' } - this.sandbox.contentWindow.globals = globals + + this.globals.globals = globals + + for (var k in this.globals) { + this.sandbox.contentWindow[k] = this.globals[k] + } }