mirror of
https://gh-proxy.org/https://github.com/tiajinsha/JKVideo
synced 2026-07-07 23:18:38 +08:00
feat: add danmaku XML parser and color util
This commit is contained in:
24
utils/danmaku.ts
Normal file
24
utils/danmaku.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import type { DanmakuItem } from '../services/types';
|
||||
|
||||
export function parseDanmakuXml(xml: string): DanmakuItem[] {
|
||||
const re = /<d p="([^"]+)">([^<]*)<\/d>/g;
|
||||
const items: DanmakuItem[] = [];
|
||||
let m: RegExpExecArray | null;
|
||||
while ((m = re.exec(xml)) !== null) {
|
||||
const p = m[1].split(',');
|
||||
if (p.length < 4) continue;
|
||||
const time = parseFloat(p[0]);
|
||||
const mode = parseInt(p[1], 10);
|
||||
const text = m[2]
|
||||
.replace(/&/g, '&').replace(/</g, '<')
|
||||
.replace(/>/g, '>').replace(/"/g, '"').trim();
|
||||
if (!text || isNaN(time)) continue;
|
||||
if (mode !== 1 && mode !== 4 && mode !== 5) continue;
|
||||
items.push({ time, mode: mode as 1|4|5, fontSize: parseInt(p[2],10), color: parseInt(p[3],10), text });
|
||||
}
|
||||
return items.sort((a, b) => a.time - b.time);
|
||||
}
|
||||
|
||||
export function danmakuColorToCss(color: number): string {
|
||||
return '#' + (color >>> 0 & 0xFFFFFF).toString(16).padStart(6, '0');
|
||||
}
|
||||
Reference in New Issue
Block a user