Skip to content

Commit

Permalink
refactor: improved some typings
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyhalight committed Jan 10, 2025
1 parent 372fa7a commit fd3cf68
Show file tree
Hide file tree
Showing 14 changed files with 32 additions and 47 deletions.
8 changes: 3 additions & 5 deletions packages/ext/src/helpers/appledeveloper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ export default class AppleDeveloperHelper extends BaseHelper {
// eslint-disable-next-line @typescript-eslint/require-await
async getVideoData(videoId: string): Promise<MinimalVideoData | undefined> {
try {
const contentUrl = (
document.querySelector("meta[property='og:video']") as
| HTMLMetaElement
| undefined
const contentUrl = document.querySelector<HTMLMetaElement>(
"meta[property='og:video']",
)?.content;
if (!contentUrl) {
throw new VideoHelperError("Failed to find content url");
Expand All @@ -21,7 +19,7 @@ export default class AppleDeveloperHelper extends BaseHelper {
return {
url: contentUrl,
};
} catch (err: unknown) {
} catch (err) {
Logger.error(
`Failed to get apple developer video data by video ID: ${videoId}`,
(err as Error).message,
Expand Down
2 changes: 1 addition & 1 deletion packages/ext/src/helpers/bannedvideo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default class BannedVideoHelper extends BaseHelper {
});

return (await res.json()) as BannedVideo.GetVideoResponse;
} catch (err: unknown) {
} catch (err) {
console.error(
`Failed to get bannedvideo video info by videoId: ${videoId}.`,
(err as Error).message,
Expand Down
7 changes: 2 additions & 5 deletions packages/ext/src/helpers/coursehunterLike.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ export default class CoursehunterLikeHelper extends BaseHelper {
return String(courseId);
}

const inputEl = document.querySelector('input[name="course_id"]') as
| HTMLInputElement
| undefined;

return inputEl ? inputEl.value : undefined;
return document.querySelector<HTMLInputElement>('input[name="course_id"]')
?.value;
}

async getLessonsData(courseId: string) {
Expand Down
2 changes: 1 addition & 1 deletion packages/ext/src/helpers/loom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default class LoomHelper extends BaseHelper {

// @ts-expect-error var from page scripts
const release = SENTRY_RELEASE as Loom.SentryRelease;
return release?.id;
return release.id;
}

async getVideoData(videoId: string): Promise<MinimalVideoData | undefined> {
Expand Down
2 changes: 1 addition & 1 deletion packages/ext/src/helpers/mailru.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default class MailRuHelper extends BaseHelper {
}/+/video/meta/${videoId}?xemail=&ajax_call=1&func_name=&mna=&mnb=&ext=1&_=${new Date().getTime()}`,
);
return (await res.json()) as MailRu.VideoInfo;
} catch (err: unknown) {
} catch (err) {
Logger.error("Failed to get mail.ru video data", (err as Error).message);
return undefined;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/ext/src/helpers/nineanimetv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default class NineAnimeTVHelper extends BaseHelper {
}

return /data-id="(\d+)"/.exec(content.html)?.[1];
} catch (err: unknown) {
} catch (err) {
Logger.error(
`Failed to get 9animetv servers info by episodeId: ${episodeId}.`,
(err as Error).message,
Expand All @@ -42,7 +42,7 @@ export default class NineAnimeTVHelper extends BaseHelper {
}

return content.link;
} catch (err: unknown) {
} catch (err) {
Logger.error(
`Failed to get player link by sourceId: ${sourceId}.`,
(err as Error).message,
Expand All @@ -68,7 +68,7 @@ export default class NineAnimeTVHelper extends BaseHelper {
}

return content;
} catch (err: unknown) {
} catch (err) {
Logger.error(
`Failed to get rapid cloud data by rapidId: ${rapidId}.`,
(err as Error).message,
Expand Down
2 changes: 1 addition & 1 deletion packages/ext/src/helpers/odysee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default class OdyseeHelper extends BaseHelper {
}

return { url };
} catch (err: unknown) {
} catch (err) {
Logger.error(
`Failed to get odysee video data by video ID: ${videoId}`,
(err as Error).message,
Expand Down
2 changes: 1 addition & 1 deletion packages/ext/src/helpers/patreon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default class PatreonHelper extends BaseHelper {
);

return (await res.json()) as Patreon.PostsResponse;
} catch (err: unknown) {
} catch (err) {
Logger.error(
`Failed to get patreon posts by postId: ${postId}.`,
(err as Error).message,
Expand Down
12 changes: 3 additions & 9 deletions packages/ext/src/helpers/reddit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ export default class RedditHelper extends BaseHelper {

// eslint-disable-next-line @typescript-eslint/require-await
async getContentUrl(videoId: string) {
if (
!this.service!.additionalData ||
this.service!.additionalData !== "old"
) {
if (this.service?.additionalData !== "old") {
// this isn't a video element, but nevertheless let it be so
return (
document.querySelector("shreddit-player-2") as
Expand All @@ -20,10 +17,7 @@ export default class RedditHelper extends BaseHelper {
)?.src;
}

const playerEl = document.querySelector("[data-hls-url]") as
| HTMLElement
| undefined;

const playerEl = document.querySelector<HTMLElement>("[data-hls-url]");
return playerEl?.dataset.hlsUrl?.replaceAll("&amp;", "&");
}

Expand All @@ -37,7 +31,7 @@ export default class RedditHelper extends BaseHelper {
return {
url: decodeURIComponent(contentUrl),
};
} catch (err: unknown) {
} catch (err) {
Logger.error(
`Failed to get reddit video data by video ID: ${videoId}`,
(err as Error).message,
Expand Down
13 changes: 6 additions & 7 deletions packages/ext/src/helpers/sap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,17 @@ export default class SapHelper extends BaseHelper {
},
);
return (await res.json()) as Sap.Response;
} catch (err: unknown) {
} catch (err) {
Logger.error("Failed to request kaltura data", (err as Error).message);
return undefined;
}
}

async getKalturaData(videoId: string) {
try {
const scriptEl = document.querySelector(
const scriptEl = document.querySelector<HTMLScriptElement>(
'script[data-nscript="beforeInteractive"]',
) as HTMLScriptElement | undefined;
);
if (!scriptEl) {
throw new VideoHelperError("Failed to find script element");
}
Expand All @@ -91,9 +91,8 @@ export default class SapHelper extends BaseHelper {
)?.getAttribute("id");
if (!entryId) {
// for course demo
const nextDataEl = document.querySelector("#__NEXT_DATA__") as
| HTMLScriptElement
| undefined;
const nextDataEl =
document.querySelector<HTMLScriptElement>("#__NEXT_DATA__");
if (!nextDataEl) {
throw new VideoHelperError("Failed to find next data element");
}
Expand All @@ -107,7 +106,7 @@ export default class SapHelper extends BaseHelper {
}

return await this.requestKaltura(kalturaDomain, partnerId, entryId);
} catch (err: unknown) {
} catch (err) {
Logger.error("Failed to get kaltura data", (err as Error).message);
return undefined;
}
Expand Down
8 changes: 4 additions & 4 deletions packages/ext/src/helpers/twitch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ export default class TwitchHelper extends BaseHelper {

// eslint-disable-next-line @typescript-eslint/require-await
async getClipLink(pathname: string, clipId: string | null) {
const schema = document.querySelector(
const schema = document.querySelector<HTMLScriptElement>(
"script[type='application/ld+json']",
) as HTMLScriptElement | undefined;
);
const clearPathname = pathname.slice(1);
if (schema) {
const schemaJSON = JSON.parse(schema.innerText) as Sap.Schema;
Expand All @@ -24,11 +24,11 @@ export default class TwitchHelper extends BaseHelper {
return `${channelName}/clip/${clearPathname}`;
}
const isEmbed = clearPathname === "embed";
const channelLink = document.querySelector(
const channelLink = document.querySelector<HTMLLinkElement>(
isEmbed
? ".tw-link[data-test-selector='stream-info-card-component__stream-avatar-link']"
: ".clips-player a:not([class])",
) as HTMLLinkElement | undefined;
);
if (!channelLink) {
return undefined;
}
Expand Down
6 changes: 2 additions & 4 deletions packages/ext/src/helpers/twitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ export default class TwitterHelper extends BaseHelper {
}

const postEl = this.video?.closest('[data-testid="tweet"]');
const newLink = (
postEl?.querySelector('a[role="link"][aria-label]') as
| HTMLLinkElement
| undefined
const newLink = postEl?.querySelector<HTMLLinkElement>(
'a[role="link"][aria-label]',
)?.href;
return newLink ? /status\/([^/]+)/.exec(newLink)?.[1] : undefined;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/ext/src/helpers/udemy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ export default class UdemyHelper extends BaseHelper {
API_ORIGIN = "https://www.udemy.com/api-2.0";

getModuleData() {
const appLoaderEl = document.querySelector(
const appLoaderEl = document.querySelector<HTMLElement>(
".ud-app-loader[data-module-id='course-taking']",
) as HTMLElement | undefined;
);
const moduleData = appLoaderEl?.dataset?.moduleArgs;
if (!moduleData) {
return undefined;
Expand Down
5 changes: 2 additions & 3 deletions packages/ext/src/helpers/yandexdisk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ export default class YandexDiskHelper extends BaseHelper {
return undefined;
}

const preloadedScript = document.querySelector("#preloaded-data") as
| HTMLScriptElement
| undefined;
const preloadedScript =
document.querySelector<HTMLScriptElement>("#preloaded-data");
if (!preloadedScript) {
return undefined;
}
Expand Down

0 comments on commit fd3cf68

Please sign in to comment.