Skip to content

Commit

Permalink
✨ feat: fetch api
Browse files Browse the repository at this point in the history
Signed-off-by: SimonShiki <[email protected]>
  • Loading branch information
SimonShiki committed Nov 12, 2023
1 parent 8801186 commit c80d56a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 21 additions & 3 deletions src/loader/make-ctx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,28 @@ export interface Context {
TargetType: typeof TargetType;
ReporterScope: typeof ReporterScope;
Cast: Cast;
translate: ReturnType<typeof createTranslate>;
extensions: {
register: (extensionObj: ExtensionClass) => void;
unsandboxed: boolean;
chibi: true;
eureka: true;
};
// Non-standard API
vm?: VM;
translate: ReturnType<typeof createTranslate>;
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.
Expand Down Expand Up @@ -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.
*/
Expand All @@ -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;
Expand Down

0 comments on commit c80d56a

Please sign in to comment.