修改匹配 XML 弹幕的正则表达式

This commit is contained in:
Genteure authored and Harvey Zack committed 2022-06-08 16:21:22 +08:00
1 parent edfc95e4c3
commit 19061122b2
1 file changed
+25 -20
@@ -14,26 +14,31 @@ export function getMode(key) {
export function bilibiliDanmuParseFromXml(xmlString) { export function bilibiliDanmuParseFromXml(xmlString) {
if (typeof xmlString !== 'string') return []; if (typeof xmlString !== 'string') return [];
const srtList = xmlString.match(/<d([\S ]*?>[\S ]*?)<\/d>/gi); const matches = xmlString.matchAll(/<d (?:.*? )??p="(?<p>.+?)"(?: .*?)?>(?<text>.+?)<\/d>/gs);
return srtList && srtList.length return Array.from(matches).map(match => {
? srtList.map((item) => { const attr = match.groups.p.split(',');
const [, attrStr, text] = item.match(/<d p="(.+)">(.+)<\/d>/); if (attr.length === 8) {
const attr = attrStr.split(','); const text = match.groups.text.trim()
return attr.length === 8 && text.trim() .replaceAll('&quot;', '\"')
? { .replaceAll('&apos;', '\'')
text, .replaceAll('&lt;', '<')
time: Number(attr[0]), .replaceAll('&gt;', '>')
mode: getMode(Number(attr[1])), .replaceAll('&amp;', '&');
fontSize: Number(attr[2]), return {
color: `#${Number(attr[3]).toString(16)}`, text,
timestamp: Number(attr[4]), time: Number(attr[0]),
pool: Number(attr[5]), mode: getMode(Number(attr[1])),
userID: attr[6], fontSize: Number(attr[2]),
rowID: Number(attr[7]), color: `#${Number(attr[3]).toString(16)}`,
} timestamp: Number(attr[4]),
: null; pool: Number(attr[5]),
}) userID: attr[6],
: []; rowID: Number(attr[7]),
}
} else {
return null;
}
}).filter(d => d);
} }
export function bilibiliDanmuParseFromUrl(url) { export function bilibiliDanmuParseFromUrl(url) {