mirror of
https://github.com/GetOpenScript/OpenScript.git
synced 2026-09-18 09:45:43 +00:00
Expose OpenScript and env to globalThis for reliable secret access
This commit is contained in:
@@ -14,12 +14,19 @@ export const isUserScriptsAvailable = async () => {
|
|||||||
export const wrapScriptCode = (code, secrets = {}) => {
|
export const wrapScriptCode = (code, secrets = {}) => {
|
||||||
const envInjection = `
|
const envInjection = `
|
||||||
// [OpenScript Injected Environment]
|
// [OpenScript Injected Environment]
|
||||||
const OpenScript = Object.freeze({
|
(function() {
|
||||||
|
const secretsObj = Object.freeze(${JSON.stringify(secrets)});
|
||||||
|
const openScriptObj = Object.freeze({
|
||||||
version: "1.0.0",
|
version: "1.0.0",
|
||||||
env: Object.freeze(${JSON.stringify(secrets)})
|
env: secretsObj
|
||||||
});
|
});
|
||||||
const env = OpenScript.env;
|
globalThis.OpenScript = openScriptObj;
|
||||||
const GM_getValue = (k, def) => (OpenScript.env[k] ?? def);
|
globalThis.env = secretsObj;
|
||||||
|
globalThis.GM_getValue = (k, def) => (secretsObj[k] ?? def);
|
||||||
|
})();
|
||||||
|
var OpenScript = globalThis.OpenScript;
|
||||||
|
var env = globalThis.env;
|
||||||
|
var GM_getValue = globalThis.GM_getValue;
|
||||||
`;
|
`;
|
||||||
return `${envInjection}\n${code}`;
|
return `${envInjection}\n${code}`;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -50,10 +50,10 @@ test('wrapScriptCode injects OpenScript.env and GM_getValue polyfill', () => {
|
|||||||
const code = 'console.log(env.API_KEY, GM_getValue("API_KEY"));';
|
const code = 'console.log(env.API_KEY, GM_getValue("API_KEY"));';
|
||||||
const wrapped = wrapScriptCode(code, { API_KEY: 'secret123' });
|
const wrapped = wrapScriptCode(code, { API_KEY: 'secret123' });
|
||||||
|
|
||||||
assert.ok(wrapped.includes('const OpenScript = Object.freeze('));
|
assert.ok(wrapped.includes('OpenScript'));
|
||||||
assert.ok(wrapped.includes('"API_KEY":"secret123"'));
|
assert.ok(wrapped.includes('"API_KEY":"secret123"'));
|
||||||
assert.ok(wrapped.includes('const env = OpenScript.env;'));
|
assert.ok(wrapped.includes('globalThis.OpenScript'));
|
||||||
assert.ok(wrapped.includes('const GM_getValue ='));
|
assert.ok(wrapped.includes('GM_getValue'));
|
||||||
assert.ok(wrapped.includes(code));
|
assert.ok(wrapped.includes(code));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user