Skip to content

Commit

Permalink
fix: retain only necessary script props
Browse files Browse the repository at this point in the history
  • Loading branch information
tophf committed May 4, 2024
1 parent 7329b8f commit 3a41732
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
17 changes: 10 additions & 7 deletions src/injected/web/gm-api-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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,
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/injected/web/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ declare interface GMContext {
id: number;
resCache: StringMap;
resources: StringMap;
script: VMInjection.Script;
displayName: string;
}

/**
Expand Down

0 comments on commit 3a41732

Please sign in to comment.