From 7329b8fd199940dfcf6069839f9be17cda0d3d5c Mon Sep 17 00:00:00 2001 From: tophf Date: Mon, 29 Apr 2024 17:11:20 +0300 Subject: [PATCH] fix: warn about non-cloneable objects + persist _Error callback + import `callbacks` directly --- src/injected/content/bridge.js | 1 + src/injected/util/index.js | 7 ++++++- src/injected/web/bridge.js | 8 +------- src/injected/web/index.js | 7 ++++--- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/injected/content/bridge.js b/src/injected/content/bridge.js index b18d12a1e0..996ad19d5e 100644 --- a/src/injected/content/bridge.js +++ b/src/injected/content/bridge.js @@ -45,6 +45,7 @@ const bridge = { data = data.data; } try { + if (!handle) throw data; if (handle === REIFY) { handle = true; res = bridge[REIFY]; diff --git a/src/injected/util/index.js b/src/injected/util/index.js index f02047d4e3..5b94efe17d 100644 --- a/src/injected/util/index.js +++ b/src/injected/util/index.js @@ -31,7 +31,12 @@ export const bindEvents = (srcId, destId, bridge) => { } if (!incomingNodeEvent) { // CustomEvent is the main message - e = e::getDetail(); + // but if the previous message was non-cloneable we will throw if MouseEvent is next + try { e = e::getDetail(); } catch (err) { return; } + if (!e) { + e = createNullObj(); + e.data = `[${VIOLENTMONKEY}] Non-cloneable property e.g. a DOM node or function.`; + } if (cloneInto) e = cloneInto(e, window); if (e.node && (incomingNodeEvent = e)) return; } else { diff --git a/src/injected/web/bridge.js b/src/injected/web/bridge.js index a5518d8616..fdec75707d 100644 --- a/src/injected/web/bridge.js +++ b/src/injected/web/bridge.js @@ -1,11 +1,6 @@ const handlers = createNullObj(); export const addHandlers = obj => assign(handlers, obj); -const callbacks = { - __proto__: null, - Error(err) { - throw err; - }, -}; +export const callbacks = createNullObj(); /** * @property {VMScriptGMInfoPlatform} ua * @property {VMBridgePostFunc} post @@ -13,7 +8,6 @@ const callbacks = { */ const bridge = { __proto__: null, - callbacks, onHandle({ cmd, data, node }) { const fn = handlers[cmd]; if (fn) node::fn(data); diff --git a/src/injected/web/index.js b/src/injected/web/index.js index 8844108d8a..55298e8b85 100644 --- a/src/injected/web/index.js +++ b/src/injected/web/index.js @@ -1,4 +1,4 @@ -import bridge, { addHandlers } from './bridge'; +import bridge, { addHandlers, callbacks } from './bridge'; import { commands, storages } from './store'; import { GM_API } from './gm-api'; import { makeGmApiWrapper } from './gm-api-wrapper'; @@ -62,8 +62,9 @@ addHandlers({ }, /** @this {Node} */ Callback({ id, data }) { - const fn = bridge.callbacks[id]; - delete bridge.callbacks[id]; + if (id === 'Error') throw data; + const fn = callbacks[id]; + delete callbacks[id]; if (fn) this::fn(data); }, async Plant({ data: dataKey, win: winKey }) {