Skip to content

Commit

Permalink
Fix unext video name detection
Browse files Browse the repository at this point in the history
  • Loading branch information
killergerbah committed Jun 29, 2024
1 parent f14bd91 commit 01cf6d0
Showing 1 changed file with 56 additions and 43 deletions.
99 changes: 56 additions & 43 deletions extension/src/pages/unext-page.ts
Original file line number Diff line number Diff line change
@@ -1,55 +1,68 @@
import { poll } from './util';
setTimeout(() => {
let baseName: string | undefined;

document.addEventListener('asbplayer-get-synced-data', async () => {
let basename = '';
const originalParse = JSON.parse;
JSON.parse = function () {
// @ts-ignore
const value = originalParse.apply(this, arguments);

await poll(() => {
const titleNodes = document.querySelectorAll('div[class^="Header__TitleContainer"]');
const segments: string[] = [];
const nodes: Node[] = [];
if (typeof value?.data?.webfront_title_stage?.titleName === 'string') {
baseName = value.data.webfront_title_stage.titleName;

for (let i = 0; i < titleNodes.length; ++i) {
nodes.push(titleNodes[i]);
}

while (nodes.length > 0) {
const node = nodes.shift();
if (typeof value.data.webfront_title_stage.episode?.id === 'string') {
const episodeId = value.data.webfront_title_stage.episode.id;

if (node === undefined) {
break;
}
if (
typeof value.data.webfront_title_titleEpisodes?.episodes === 'object' &&
Array.isArray(value.data.webfront_title_titleEpisodes.episodes)
) {
for (const obj of value.data.webfront_title_titleEpisodes.episodes) {
if (obj.id === episodeId) {
if (typeof obj.displayNo === 'string') {
baseName = `${baseName} ${obj.displayNo}`;
}

if (node.childNodes.length === 0) {
const textContent = node.textContent;
if (typeof obj.episodeName === 'string') {
baseName = `${baseName} ${obj.episodeName}`;
}

if (textContent !== null) {
segments.push(textContent);
break;
}
}
}

continue;
}

for (let i = 0; i < node.childNodes.length; ++i) {
const childNode = node.childNodes[i];
nodes.push(childNode);
}
}

if (segments.length > 0) {
basename = segments.join(' ');
return true;
}
return value;
};

document.addEventListener(
'asbplayer-get-synced-data',
() => {
if (!baseName) {
document.dispatchEvent(
new CustomEvent('asbplayer-synced-data', {
detail: {
error: '',
basename: '',
subtitles: [],
},
})
);
return;
}

return false;
});

document.dispatchEvent(
new CustomEvent('asbplayer-synced-data', {
detail: {
error: '',
basename: basename,
subtitles: [],
},
})
document.dispatchEvent(
new CustomEvent('asbplayer-synced-data', {
detail: {
error: '',
basename: baseName,
subtitles: [],
},
})
);
baseName = undefined;
},
false
);
});
}, 0);

0 comments on commit 01cf6d0

Please sign in to comment.