Skip to content

Commit

Permalink
refactor(utils): move subtitle, danmaku functions
Browse files Browse the repository at this point in the history
  • Loading branch information
WakelessSloth56 committed Mar 13, 2024
1 parent e963247 commit 491c510
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 43 deletions.
42 changes: 41 additions & 1 deletion src/player.player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,46 @@ const __player_metadata__ = (function () {

// ====================================================================== //

function initDanmaku(stage: HTMLElement, url: string, onload: () => void) {
const provider = new CommentProvider();
provider.addStaticSource(CommentProvider.XMLProvider('GET', url, null, null), CommentProvider.SOURCE_XML);
provider.addParser(new BilibiliFormat.XMLParser(), CommentProvider.SOURCE_XML);
const commentManager = new CommentManager(stage);
// @ts-expect-error
provider.addTarget(commentManager);
commentManager.init('css');
provider
.load()
.then(() => {
commentManager.start();
onload();
})
.catch((e) => alert('DanmakuError: ' + e));
return commentManager;
}

function hasDanmaku(p: Player) {
return p.danmakuUrl ? true : false;
}

function initSubtitle(stage: HTMLElement, video: HTMLVideoElement, url: string) {
const req = new XMLHttpRequest();
req.open('GET', url, false);
req.send();
if (req.status === 200) {
// @ts-expect-error
const ass = new ASS(req.responseText, video, { container: stage, resampling: 'video_height' });
return ass as SubtitleManager;
}
return null;
}

function hasSubtitle(p: Player) {
return p.subtitleUrl ? true : false;
}

// ====================================================================== //

const toastBox = new EDC('div') //
.class('toast box visibility-transition invisible')
.playerEvents({
Expand Down Expand Up @@ -215,7 +255,7 @@ const __player_metadata__ = (function () {
click: (P) => toggleDisplay(P.elements.danmakuList),
})
.playerEvents({
danmakuload: (P, E) => (E.innerHTML = `(${danmakuCount(P)})`),
danmakuload: (P, E) => (E.innerHTML = `(${P.commentManager.timeline.length})`),
});

const danmakuTimeOffset = new EDC('input')
Expand Down
42 changes: 0 additions & 42 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,45 +138,3 @@ function bindEvents<F extends AnyFunction>(target: HTMLElement, listeners: Event
target.addEventListener(type, (event) => listener(...params, event));
}
}

function initDanmaku(stage: HTMLElement, url: string, onload: () => void) {
const provider = new CommentProvider();
provider.addStaticSource(CommentProvider.XMLProvider('GET', url, null, null), CommentProvider.SOURCE_XML);
provider.addParser(new BilibiliFormat.XMLParser(), CommentProvider.SOURCE_XML);
const commentManager = new CommentManager(stage);
// @ts-expect-error
provider.addTarget(commentManager);
commentManager.init('css');
provider
.load()
.then(() => {
commentManager.start();
onload();
})
.catch((e) => alert('DanmakuError: ' + e));
return commentManager;
}

function hasDanmaku(p: Player) {
return p.danmakuUrl ? true : false;
}

function initSubtitle(stage: HTMLElement, video: HTMLVideoElement, url: string) {
const req = new XMLHttpRequest();
req.open('GET', url, false);
req.send();
if (req.status === 200) {
// @ts-expect-error
const ass = new ASS(req.responseText, video, { container: stage, resampling: 'video_height' });
return ass as SubtitleManager;
}
return null;
}

function hasSubtitle(p: Player) {
return p.subtitleUrl ? true : false;
}

function danmakuCount(p: Player) {
return p.commentManager.timeline.length;
}

0 comments on commit 491c510

Please sign in to comment.