Skip to content
This repository has been archived by the owner on Jun 3, 2024. It is now read-only.

Commit

Permalink
Add option to show episode number relative to season
Browse files Browse the repository at this point in the history
  • Loading branch information
bytedream committed Oct 20, 2023
1 parent 1743012 commit ee7afaf
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 1 deletion.
82 changes: 81 additions & 1 deletion src/entries/contentScript/website/series/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ const DomElements = {
true
);
},
seasonWithNavigationContainer: async (): Promise<HTMLDivElement> => {
return await getElementMounted(
(p) => p.querySelector('.erc-season-with-navigation'),
document.getElementById('content'),
true
);
},
seasonSelectContainer: async (): Promise<HTMLDivElement> => {
return await getElementMounted(
(p) => p.querySelector('.seasons-select'),
Expand All @@ -33,6 +40,72 @@ async function nextEpisodeAirDateSetting(enable: boolean) {
MountComponent.mount(NextEpisodeAirDate, import.meta.PLUGIN_WEB_EXT_CHUNK_CSS_PATHS, container);
}

const relativeEpisodeNumbersEpisodeNumberRegex = /(E\d+)(?=\.|$)/;
let relativeEpisodeNumbersPairs = [];
let relativeEpisodeNumbersEpisodeListObserver: MutationObserver;
async function relativeEpisodeNumbersSetting(enable: boolean) {
if (relativeEpisodeNumbersEpisodeListObserver)
relativeEpisodeNumbersEpisodeListObserver.disconnect();

if (!enable) {
if (!relativeEpisodeNumbersPairs) return;

for (const [card, relativeIdentifier, identifier] of relativeEpisodeNumbersPairs) {
console.log(relativeIdentifier, identifier);
card.innerHTML = card.innerHTML.replace(new RegExp(relativeIdentifier, 'gm'), identifier);
}

return;
}
relativeEpisodeNumbersPairs = [];

let selfModify = false;
const mountFunction = () => {
const cards = document.querySelectorAll('.episode-list .card') as NodeListOf<HTMLDivElement>;

let skipped = 0;
for (const [i, card] of cards.entries()) {
let identifier = card.querySelector('h4 a').textContent;
identifier = identifier.split('-')[0].trim();

const episodeNumber = relativeEpisodeNumbersEpisodeNumberRegex.exec(identifier);
if (episodeNumber === null) {
skipped++;
continue;
} else if (identifier.indexOf('.') != -1 || episodeNumber[0] == '0') {
skipped++;
}

const relativeIdentifier = identifier.replace(
relativeEpisodeNumbersEpisodeNumberRegex,
`E${i + 1 - skipped}`
);
if (relativeIdentifier === identifier) continue;

selfModify = true;
relativeEpisodeNumbersPairs.push([card, relativeIdentifier, identifier]);
card.innerHTML = card.innerHTML.replace(new RegExp(identifier, 'gm'), relativeIdentifier);
}
};

const seasonWithNavigationContainer = await DomElements.seasonWithNavigationContainer();
relativeEpisodeNumbersEpisodeListObserver = new MutationObserver(() => {
// make sure that `mountFunction` is not re-triggered when the html is edited from inside it
if (selfModify) {
selfModify = false;
return;
}

mountFunction();
});
relativeEpisodeNumbersEpisodeListObserver.observe(seasonWithNavigationContainer, {
childList: true,
subtree: true
});

mountFunction();
}

let animeListLinkSeasonNameObserver: MutationObserver;
async function animeListLinkSetting(value: { anilist: boolean; mal: boolean }) {
MountComponent.unmount(AnimeListLinks);
Expand Down Expand Up @@ -81,14 +154,21 @@ async function animeListLinkSetting(value: { anilist: boolean; mal: boolean }) {
}

export async function settings() {
const subscriptionSettings = [SeriesSettings.NextEpisodeAirDate, SeriesSettings.AnimeListLinks];
const subscriptionSettings = [
SeriesSettings.NextEpisodeAirDate,
SeriesSettings.RelativeEpisodeNumber,
SeriesSettings.AnimeListLinks
];
subscribeSettings(
subscriptionSettings,
(key, value) => {
switch (key) {
case SeriesSettings.NextEpisodeAirDate.key:
nextEpisodeAirDateSetting(value);
break;
case SeriesSettings.RelativeEpisodeNumber.key:
relativeEpisodeNumbersSetting(value);
break;
case SeriesSettings.AnimeListLinks.key:
animeListLinkSetting(value);
break;
Expand Down
6 changes: 6 additions & 0 deletions src/entries/popup/pages/settings/Settings.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@
setting: SeriesSettings.NextEpisodeAirDate,
value: await settingStoreProxy(SeriesSettings.NextEpisodeAirDate)
},
{
name: 'Relative episode numbers',
details: 'Show episode numbers relative to the season',
setting: SeriesSettings.RelativeEpisodeNumber,
value: await settingStoreProxy(SeriesSettings.RelativeEpisodeNumber)
},
{
name: 'External anime list links',
details: 'Show links to external anime list/tracking websites',
Expand Down
4 changes: 4 additions & 0 deletions src/lib/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ export const SeriesSettings = {
key: 'nextEpisodeAirDate',
default: true
} satisfies Setting<boolean>,
RelativeEpisodeNumber: {
key: 'relativeEpisodeNumber',
default: false
} satisfies Setting<boolean>,
AnimeListLinks: {
key: 'animeListLinks',
default: { anilist: false, mal: false }
Expand Down

0 comments on commit ee7afaf

Please sign in to comment.