From fc99d7fd9b32f0eeaec79a657268f6060bdb341b Mon Sep 17 00:00:00 2001 From: Peter Njeim Date: Thu, 1 Feb 2024 22:07:59 -0400 Subject: [PATCH] fix: userscript 404 error Fixes #45 --- src/file.ts | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/file.ts b/src/file.ts index 97bd087..8b70f6a 100644 --- a/src/file.ts +++ b/src/file.ts @@ -8,7 +8,7 @@ import { magics } from "./file-magics"; export type FileType = "img" | "mp3" | "midi"; const getApiUrl = (id: number, type: FileType, index: number): string => { - return `/api/jmuse?id=${id}&type=${type}&index=${index}&v2=1`; + return `/api/jmuse?id=${id}&type=${type}&index=${index}`; }; /** @@ -26,9 +26,6 @@ const useBuiltinAuth = (type: FileType): string => { }; const getApiAuth = async (type: FileType, index: number): Promise => { - // eslint-disable-next-line no-void - void index; // unused - if (isNodeJs) { // we cannot intercept API requests in Node.js (as no requests are sent), so go straightforward to the hard-coded tokens return useBuiltinAuth(type); @@ -43,6 +40,9 @@ const getApiAuth = async (type: FileType, index: number): Promise => { const fsBtn = document.querySelector( 'button[title="Toggle Fullscreen"]' ) as HTMLButtonElement; + if (!fsBtn) { + throw Error; + } const el = fsBtn.parentElement?.parentElement?.querySelector( "button" @@ -51,6 +51,10 @@ const getApiAuth = async (type: FileType, index: number): Promise => { break; } case "mp3": { + // Mobile doesn't support click() function, find another method + if (navigator.userAgentData.mobile) { + throw Error; + } const el = document.querySelector( 'button[title="Toggle Play"]' ) as HTMLButtonElement; @@ -58,9 +62,8 @@ const getApiAuth = async (type: FileType, index: number): Promise => { break; } case "img": { - const imgE = document.querySelector("img[src*=score_]"); - const nextE = imgE?.parentElement?.nextElementSibling; - if (nextE) nextE.scrollIntoView(); + // Use fallback until better method is found + throw Error; break; } } @@ -88,12 +91,20 @@ export const getFileUrl = async ( const url = getApiUrl(id, type, index); const auth = await getApiAuth(type, index); - const r = await _fetch(url, { + let r = await _fetch(url, { headers: { Authorization: auth, }, }); + if (!r.ok) { + r = await _fetch(url + "&v2=1", { + headers: { + Authorization: auth, + }, + }); + } + const { info } = await r.json(); return info.url as string; };