forked from Sv443/Userscript.ts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresources.ts
More file actions
20 lines (18 loc) · 1015 Bytes
/
Copy pathresources.ts
File metadata and controls
20 lines (18 loc) · 1015 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import type { LooseUnion } from "@sv443-network/coreutils";
import type resources from "@assets/resources.json";
/** Key of a resource in `assets/resources.json` and extra keys defined by `tools/post-build.ts` */
export type ResourceKey = keyof typeof resources;
/**
* Returns the URL of a resource by its name, as defined in `assets/resources.json`, from GM resource cache - [see GM.getResourceUrl docs](https://wiki.greasespot.net/GM.getResourceUrl)
* Falls back to a `raw.githubusercontent.com` URL or base64-encoded data URI if the resource is not available in the GM resource cache.
* ⚠️ Requires the directive `@grant GM.getResourceUrl`
*/
export async function getResourceUrl(name: LooseUnion<ResourceKey>) {
let url = await GM.getResourceUrl(name);
if(!url || url.length === 0) {
console.warn(`Couldn't get blob URL nor external URL for @resource '${name}', trying to use base64-encoded fallback`);
// @ts-ignore
url = await GM.getResourceUrl(name, false);
}
return url;
}