Skip to content

Commit

Permalink
fix: warn about non-cloneable objects
Browse files Browse the repository at this point in the history
+ persist _Error callback
+ import `callbacks` directly
  • Loading branch information
tophf committed May 4, 2024
1 parent 8c7be64 commit 7329b8f
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/injected/content/bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const bridge = {
data = data.data;
}
try {
if (!handle) throw data;
if (handle === REIFY) {
handle = true;
res = bridge[REIFY];
Expand Down
7 changes: 6 additions & 1 deletion src/injected/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 1 addition & 7 deletions src/injected/web/bridge.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
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
* @property {VMBridgeMode} mode
*/
const bridge = {
__proto__: null,
callbacks,
onHandle({ cmd, data, node }) {
const fn = handlers[cmd];
if (fn) node::fn(data);
Expand Down
7 changes: 4 additions & 3 deletions src/injected/web/index.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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 }) {
Expand Down

0 comments on commit 7329b8f

Please sign in to comment.