Skip to content

Commit

Permalink
chore: better loading of crunchyroll's episode details (regular and b…
Browse files Browse the repository at this point in the history
…eta)
  • Loading branch information
aklinker1 committed Sep 26, 2021
1 parent b2597a4 commit 456f68c
Showing 1 changed file with 40 additions and 36 deletions.
76 changes: 40 additions & 36 deletions src/content-scripts/services/crunchyroll/parent.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { sleep } from '~/common/utils/GlobalUtils';
import { waitUntil } from '~/common/utils/EventLoop';
import { loadedLog } from '~/common/utils/loadedLog';
import setupParent from '~/common/utils/setupParent';

Expand All @@ -7,41 +7,45 @@ loadedLog('content-scripts/services/crunchyroll/parent.ts');
const GET_EPISODE_INFO_TIMEOUT = 5000; // ms

async function getEpisodeInfo() {
const start = Date.now();
while (Date.now() - start < GET_EPISODE_INFO_TIMEOUT) {
// X - 06/2021 (Crunchyroll Classic)
if (document.querySelector('#showmedia_about_media a') != null) {
const show = document.querySelector('#showmedia_about_media a')?.textContent?.trim();
const number = /([0-9]+)/.exec(
document.querySelectorAll('#showmedia_about_media h4')?.[1]?.textContent ?? ''
)?.[1];
const dirtyName = document.querySelector('#showmedia_about_name')?.textContent?.trim();
const name = !dirtyName ? undefined : dirtyName.substring(1, dirtyName.length - 1);

return {
show,
name,
number,
};
}

// 06/2021 - Current
if (document.querySelector('.erc-current-media-info .show-title-link') != null) {
const show = document.querySelector('.erc-current-media-info .show-title-link')?.textContent;
const episodeAndNumber =
document.querySelector('.erc-current-media-info .title')?.textContent ?? '';
const groups = /([0-9]+)\s*-\s*(.+)$/.exec(episodeAndNumber);
const episode = groups?.[2];
const number = groups?.[1];

return {
show: show || undefined,
name: episode || undefined,
number: number || undefined,
};
}

await sleep(500);
const pageHasLoaded = () =>
Promise.resolve(
document.querySelector('.erc-current-media-info') != null ||
document.querySelector('#showmedia_about_media') != null
);
console.log('Waiting for page to load...');
await waitUntil(pageHasLoaded, 10 * 1000, 1, 200);
console.log('Page has loaded!');

// X - 06/2021 (Crunchyroll Classic)
if (document.querySelector('#showmedia_about_media a') != null) {
const show = document.querySelector('#showmedia_about_media a')?.textContent?.trim();
const number = /([0-9]+)/.exec(
document.querySelectorAll('#showmedia_about_media h4')?.[1]?.textContent ?? ''
)?.[1];
const dirtyName = document.querySelector('#showmedia_about_name')?.textContent?.trim();
const name = !dirtyName ? undefined : dirtyName.substring(1, dirtyName.length - 1);

return {
show,
name,
number,
};
}

// 06/2021 - Current
if (document.querySelector('.erc-current-media-info .show-title-link') != null) {
const show = document.querySelector('.erc-current-media-info .show-title-link')?.textContent;
const episodeAndNumber =
document.querySelector('.erc-current-media-info .title')?.textContent ?? '';
const groups = /([0-9]+)\s*-\s*(.+)$/.exec(episodeAndNumber);
const episode = groups?.[2];
const number = groups?.[1];

return {
show: show || undefined,
name: episode || undefined,
number: number || undefined,
};
}

return {};
Expand Down

0 comments on commit 456f68c

Please sign in to comment.