Skip to content

Commit

Permalink
fix: uncached resource resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
Sv443 committed Nov 11, 2024
1 parent adc9505 commit e70cbd9
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/utils/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,16 +231,17 @@ export function formatNumber(num: number, notation?: NumberLengthFormat): string
export async function getResourceUrl(name: ResourceKey | "_", uncached = false) {
let url = !uncached && await GM.getResourceUrl(name);
if(!url || url.length === 0) {
const resources = GM.info.script?.resources;
const resUrl = Array.isArray(resources) ? resources.find(r => r.name === name)?.url : resources?.[name]?.url;
if(typeof resUrl === "string") {
const { pathname } = new URL(resUrl);

const resource = resourcesJson?.[name as keyof typeof resourcesJson];
const ref = typeof resource === "object" && "ref" in resource ? resource.ref : branch;
const resObjOrStr = resourcesJson?.[name as keyof typeof resourcesJson];
if(typeof resObjOrStr === "object" || typeof resObjOrStr === "string") {
const pathname = typeof resObjOrStr === "object" && "path" in resObjOrStr ? resObjOrStr.path : resObjOrStr;
const ref = typeof resObjOrStr === "object" && "ref" in resObjOrStr ? resObjOrStr.ref : branch;

if(pathname && pathname.startsWith("/") && pathname.length > 1)
return `https://raw.githubusercontent.com/${repo}/${ref}${pathname}`;
else if(pathname && pathname.startsWith("http"))
return pathname;
else if(pathname && pathname.length > 0)
return `https://raw.githubusercontent.com/${repo}/${ref}/assets/${pathname}`;
}
warn(`Couldn't get blob URL nor external URL for @resource '${name}', trying to use base64-encoded fallback`);
// @ts-ignore
Expand Down

0 comments on commit e70cbd9

Please sign in to comment.