From 3a41732c7db0f78c0c69ce54474007e672bbc56a Mon Sep 17 00:00:00 2001 From: tophf Date: Mon, 29 Apr 2024 21:03:11 +0300 Subject: [PATCH] fix: retain only necessary script props --- src/injected/web/gm-api-wrapper.js | 17 ++++++++++------- src/injected/web/index.js | 2 +- src/types.d.ts | 2 +- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/injected/web/gm-api-wrapper.js b/src/injected/web/gm-api-wrapper.js index e71da9b003..12f251c307 100644 --- a/src/injected/web/gm-api-wrapper.js +++ b/src/injected/web/gm-api-wrapper.js @@ -18,6 +18,11 @@ const GM4_ASYNC = { setValue: 1, listValues: 1, }; +/** @type {(keyof VMInjection.Script)[]} */ +const COPY_SCRIPT_PROPS = [ + 'displayName', + 'id', +]; const componentUtils = makeComponentUtils(); const sendTabClose = () => bridge.post('TabClose'); const sendTabFocus = () => bridge.post('TabFocus'); @@ -29,17 +34,15 @@ const sendTabFocus = () => bridge.post('TabFocus'); export function makeGmApiWrapper(script) { // Add GM functions // Reference: http://wiki.greasespot.net/Greasemonkey_Manual:API - const { id, meta } = script; + const { meta } = script; const { grant } = meta; const resources = setPrototypeOf(meta.resources, null); /** @type {GMContext} */ - const context = { - __proto__: null, // necessary for optional props like `async` - id, - script, + const context = safePickInto({ resources, resCache: createNullObj(), - }; + async: false, + }, script, COPY_SCRIPT_PROPS); const gmInfo = makeGmInfo(script.gmi, meta, resources); const gm4 = { __proto__: null, @@ -64,7 +67,7 @@ export function makeGmApiWrapper(script) { || (fn = GM_API.bound[gmName = gm4name ? `GM_${gm4name}` : name])) { fn = safeBind(fnGm4 || fn, fnGm4 || gm4name in GM4_ASYNC - ? contextAsync || (contextAsync = assign(createNullObj(), { async: true }, context)) + ? contextAsync || (contextAsync = assign(createNullObj(), context, { async: true })) : context); } else if (!(fn = GM_API.free[gmName]) && ( fn = name === 'window.close' && sendTabClose diff --git a/src/injected/web/index.js b/src/injected/web/index.js index 55298e8b85..3b53b1c718 100644 --- a/src/injected/web/index.js +++ b/src/injected/web/index.js @@ -34,7 +34,7 @@ export default function initialize(invokeHost, console) { } /** @this {GMContext} */ GM_API.bound.GM_log = function (...args) { - bridge.post('Log', ['log', safeConcat([`[${this.script.displayName}]`], args)]); + bridge.post('Log', ['log', safeConcat([`[${this.displayName}]`], args)]); }; } } else { diff --git a/src/types.d.ts b/src/types.d.ts index 38da3fd97b..db5cd88021 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -18,7 +18,7 @@ declare interface GMContext { id: number; resCache: StringMap; resources: StringMap; - script: VMInjection.Script; + displayName: string; } /**