From 8655a2866cfe82cdbc810214e0031444e2de1925 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Thu, 13 Mar 2025 11:51:49 +0100 Subject: [PATCH] advanced syntax/script: print widget id in console.log messages --- src/client/widgets/common/widget.js | 20 ++++++++++++++++++-- src/client/widgets/vm.js | 8 +++++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/client/widgets/common/widget.js b/src/client/widgets/common/widget.js index 4b97f478..0f044eee 100644 --- a/src/client/widgets/common/widget.js +++ b/src/client/widgets/common/widget.js @@ -836,7 +836,15 @@ class Widget extends EventEmitter { if (!this.parsers[code]) this.parsers[code] = vm.compile(code, defaultScope) - let r = this.parsers[code](jsScope, this.parsersLocalScope) + let r + vm.setWidget(this) + try { + r = this.parsers[code](jsScope, this.parsersLocalScope) + } catch (err) { + vm.setWidget() + throw err + } + vm.setWidget() if (r === undefined) r = '' @@ -852,7 +860,15 @@ class Widget extends EventEmitter { if (!this.parsers[code]) this.parsers[code] = vm.compile('return ' + code.trim(), defaultScope) - let r = this.parsers[code](jsScope, this.parsersLocalScope) + let r + vm.setWidget(this) + try { + r = this.parsers[code](jsScope, this.parsersLocalScope) + } catch (err) { + vm.setWidget() + throw err + } + vm.setWidget() if (r === undefined) r = '' diff --git a/src/client/widgets/vm.js b/src/client/widgets/vm.js index 0c32f403..76948391 100644 --- a/src/client/widgets/vm.js +++ b/src/client/widgets/vm.js @@ -75,7 +75,13 @@ class Vm { registerGlobals() { - this.sandbox.contentWindow.console = console + this.sandbox.contentWindow.console = { + ...console, + log: (msg)=>{ + this.getWidget().log(msg) + } + } + this.sandbox.contentWindow.setTimeout = this.sandbox.contentWindow.setInterval = ()=>{ throw 'setTimeout and setInterval can\'t be used in the JS sandbox'