diff --git a/.gitignore b/.gitignore index faa2c1c..ffbb077 100644 --- a/.gitignore +++ b/.gitignore @@ -10,5 +10,5 @@ yarn-error.log # generated build files /dist -/extension/scripts/chibi.user.js +/extension/scripts/eureka-loader.user.js /extension/manifest.json diff --git a/src/loader/make-ctx.ts b/src/loader/make-ctx.ts index 0242782..7d59d49 100644 --- a/src/loader/make-ctx.ts +++ b/src/loader/make-ctx.ts @@ -16,16 +16,28 @@ export interface Context { TargetType: typeof TargetType; ReporterScope: typeof ReporterScope; Cast: Cast; - translate: ReturnType; extensions: { register: (extensionObj: ExtensionClass) => void; unsandboxed: boolean; chibi: true; eureka: true; }; + // Non-standard API vm?: VM; + translate: ReturnType; renderer?: Renderer; + fetch: typeof fetch; + canFetch (url: string): boolean; +} + +function parseURL (url: string) { + try { + return new URL(url, location.href); + } catch (e) { + return null; + } } + /** * I10n support for Eureka extensions. * @param vm Virtual machine instance. Optional. @@ -80,8 +92,9 @@ function createTranslate (vm?: VM) { return translate; } + /** - * Make a fake scratch context. + * Make a Scratch context. * @param vm Virtual machine instance. * @returns The context. */ @@ -100,7 +113,12 @@ export function makeCtx (vm?: VM) { chibi: true, eureka: true }, - translate: createTranslate(vm) + translate: createTranslate(vm), + fetch: globalThis.fetch, + canFetch: (url: string) => { + const parsed = parseURL(url); + return !!parsed; + } }; if (vm) { ctx.vm = vm;