add bilibiliDanmuParseFromAv

This commit is contained in:
Harvey Zack committed 2019-04-26 10:01:54 +08:00
1 parent d735bb4de7
commit 9bd1452030
4 files changed
+121 -87

No files matched your search

+91 -74
View File
@@ -4,6 +4,95 @@
(global = global || self, global['artplayer-plugin-danmu'] = factory());
}(this, function () { 'use strict';
function _arrayWithHoles(arr) {
if (Array.isArray(arr)) return arr;
}
var arrayWithHoles = _arrayWithHoles;
function _iterableToArrayLimit(arr, i) {
var _arr = [];
var _n = true;
var _d = false;
var _e = undefined;
try {
for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
_arr.push(_s.value);
if (i && _arr.length === i) break;
}
} catch (err) {
_d = true;
_e = err;
} finally {
try {
if (!_n && _i["return"] != null) _i["return"]();
} finally {
if (_d) throw _e;
}
}
return _arr;
}
var iterableToArrayLimit = _iterableToArrayLimit;
function _nonIterableRest() {
throw new TypeError("Invalid attempt to destructure non-iterable instance");
}
var nonIterableRest = _nonIterableRest;
function _slicedToArray(arr, i) {
return arrayWithHoles(arr) || iterableToArrayLimit(arr, i) || nonIterableRest();
}
var slicedToArray = _slicedToArray;
function bilibiliDanmuParseFromXml(xmlString) {
if (typeof xmlString !== 'string') return [];
var regSrt = '<d p="(.+)">(.+)</d>';
var listReg = new RegExp(regSrt, 'gi');
var itemReg = new RegExp(regSrt, 'i');
var srtList = xmlString.match(listReg);
return srtList.length ? srtList.map(function (item) {
var _item$match = item.match(itemReg),
_item$match2 = slicedToArray(_item$match, 3),
attrStr = _item$match2[1],
text = _item$match2[2];
var attr = attrStr.split(',').map(Number);
return attr.length === 8 && text.trim() ? {
text: text,
time: attr[0],
mode: attr[1],
size: attr[2],
color: "#".concat(attr[3].toString(16)),
timestamp: attr[4],
pool: attr[5],
userID: attr[6],
rowID: attr[7]
} : null;
}) : [];
}
function bilibiliDanmuParseFromAv(av) {
var corsUrl = 'https://cors-anywhere.herokuapp.com/';
return fetch("".concat(corsUrl, "https://api.bilibili.com/x/web-interface/view?aid=").concat(av)).then(function (res) {
return res.json();
}).then(function (res) {
if (res.code === 0 && res.data && res.data.cid) {
return fetch("".concat(corsUrl, "https://api.bilibili.com/x/v1/dm/list.so?oid=").concat(res.data.cid)).then(function (res) {
return res.text();
}).then(function (xmlString) {
return bilibiliDanmuParseFromXml(xmlString);
});
}
throw new Error("Unable to get data: ".concat(JSON.stringify(res)));
});
}
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
@@ -234,79 +323,6 @@
return Danmuku;
}();
function _arrayWithHoles(arr) {
if (Array.isArray(arr)) return arr;
}
var arrayWithHoles = _arrayWithHoles;
function _iterableToArrayLimit(arr, i) {
var _arr = [];
var _n = true;
var _d = false;
var _e = undefined;
try {
for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
_arr.push(_s.value);
if (i && _arr.length === i) break;
}
} catch (err) {
_d = true;
_e = err;
} finally {
try {
if (!_n && _i["return"] != null) _i["return"]();
} finally {
if (_d) throw _e;
}
}
return _arr;
}
var iterableToArrayLimit = _iterableToArrayLimit;
function _nonIterableRest() {
throw new TypeError("Invalid attempt to destructure non-iterable instance");
}
var nonIterableRest = _nonIterableRest;
function _slicedToArray(arr, i) {
return arrayWithHoles(arr) || iterableToArrayLimit(arr, i) || nonIterableRest();
}
var slicedToArray = _slicedToArray;
function bilibiliDanmuParse(xmlString) {
if (typeof xmlString !== 'string') return [];
var regSrt = '<d p="(.+)">(.+)</d>';
var listReg = new RegExp(regSrt, 'gi');
var itemReg = new RegExp(regSrt, 'i');
var srtList = xmlString.match(listReg);
return srtList.length ? srtList.map(function (item) {
var _item$match = item.match(itemReg),
_item$match2 = slicedToArray(_item$match, 3),
attrStr = _item$match2[1],
text = _item$match2[2];
var attr = attrStr.split(',');
return attr.length === 8 && text.trim() ? {
text: text,
time: Number(attr[0]),
mode: Number(attr[1]),
size: Number(attr[2]),
color: "#".concat(Number(attr[3]).toString(16)),
timestamp: Number(attr[4]),
pool: Number(attr[5]),
userID: Number(attr[6]),
rowID: Number(attr[7])
} : null;
}) : [];
}
function artplayerPluginDanmu(option) {
return function (art) {
var danmuku = new Danmuku(art, option);
@@ -321,7 +337,8 @@
};
}
artplayerPluginDanmu.bilibiliDanmuParse = bilibiliDanmuParse;
artplayerPluginDanmu.bilibiliDanmuParseFromXml = bilibiliDanmuParseFromXml;
artplayerPluginDanmu.bilibiliDanmuParseFromAv = bilibiliDanmuParseFromAv;
window.artplayerPluginDanmu = artplayerPluginDanmu;
return artplayerPluginDanmu;
@@ -1 +1 @@
{"version":3,"file":"artplayer-plugin-danmu.js","sources":["../../node_modules/@babel/runtime/helpers/classCallCheck.js","../../node_modules/@babel/runtime/helpers/createClass.js","../../packages/artplayer-plugin-danmu/src/danmuku.js","../../node_modules/@babel/runtime/helpers/arrayWithHoles.js","../../node_modules/@babel/runtime/helpers/iterableToArrayLimit.js","../../node_modules/@babel/runtime/helpers/nonIterableRest.js","../../node_modules/@babel/runtime/helpers/slicedToArray.js","../../packages/artplayer-plugin-danmu/src/bilibiliDanmuParse.js","../../packages/artplayer-plugin-danmu/src/index.js"],"sourcesContent":["function _classCallCheck(instance, Constructor) {\n if (!(instance instanceof Constructor)) {\n throw new TypeError(\"Cannot call a class as a function\");\n }\n}\n\nmodule.exports = _classCallCheck;","function _defineProperties(target, props) {\n for (var i = 0; i < props.length; i++) {\n var descriptor = props[i];\n descriptor.enumerable = descriptor.enumerable || false;\n descriptor.configurable = true;\n if (\"value\" in descriptor) descriptor.writable = true;\n Object.defineProperty(target, descriptor.key, descriptor);\n }\n}\n\nfunction _createClass(Constructor, protoProps, staticProps) {\n if (protoProps) _defineProperties(Constructor.prototype, protoProps);\n if (staticProps) _defineProperties(Constructor, staticProps);\n return Constructor;\n}\n\nmodule.exports = _createClass;","export default class Danmuku {\n constructor(art, option = {}) {\n this.art = art;\n this.queue = {};\n this.option = Object.assign({}, Danmuku.option, option);\n art.constructor.validator(this.option, Danmuku.scheme);\n this.option.danmus.forEach(this.emit);\n this.current = [];\n this.layer = {};\n this.isStop = false;\n this.timer = null;\n this.init();\n }\n\n static get option() {\n return {\n danmus: [],\n speed: 5,\n opacity: 1,\n color: '#fff',\n size: 14,\n };\n }\n\n static get scheme() {\n return {\n danmus: {\n type: 'array',\n child: {\n text: 'string',\n size: 'number',\n time: 'number',\n color: 'string',\n mode: 'number',\n },\n },\n speed: 'number',\n opacity: 'number',\n color: 'string',\n size: 'number',\n };\n }\n\n init() {\n const { layers } = this.art;\n this.layer = layers.add({\n name: 'danmu',\n style: {\n position: 'absolute',\n left: 0,\n top: 0,\n right: 0,\n bottom: 0,\n width: '100%',\n height: '100%',\n overflow: 'hidden',\n },\n });\n\n this.update();\n this.art.on('video:play', this.start.bind(this));\n this.art.on('video:pause', this.stop.bind(this));\n this.art.on('video:ended', this.stop.bind(this));\n this.art.on('destroy', this.stop.bind(this));\n }\n\n emit(danmu = {}) {\n const { errorHandle } = this.art.constructor.utils;\n errorHandle(danmu.text, 'Danmu text cannot be empty');\n errorHandle(danmu.time, 'Danmu time cannot be empty');\n if (this.queue[danmu.time]) {\n this.queue[danmu.time].push(danmu);\n } else {\n this.queue[danmu.time] = [danmu];\n }\n // this.art.emit('artplayerPluginDanmu:emit', danmu);\n // if (!this.isStop) {\n // const { clientWidth: playerWidth, clientHeight: playerHeight } = this.art.template.$player;\n // const danmuItem = this.getDanmuItem();\n // danmuItem.$ref.innerText = danmu.text;\n // danmuItem.$ref.style.fontSize = danmu.size || this.option.size;\n // const { clientWidth: danmuWidth, clientHeight: danmuHeight } = danmuItem.$ref;\n // danmuItem.$ref.style.opacity = danmu.opacity || this.option.opacity;\n // danmuItem.$ref.style.color = danmu.color || this.option.color;\n // danmuItem.$ref.style.top = this.getDanmuTop(playerHeight, danmuHeight);\n // danmuItem.$ref.style.left = `${playerWidth}px`;\n // danmuItem.$ref.style.transform = `translateX(${-playerWidth -\n // danmuWidth}px) translateY(0px) translateZ(0px)`;\n // danmuItem.$ref.style.transition = `-webkit-transform ${danmu.speed || this.option.speed}s linear 0s`;\n // this.art.emit('artplayerPluginDanmu:emit', danmu);\n // }\n }\n\n getDanmuItem() {\n const { setStyles, append } = this.art.constructor.utils;\n const inactiveItem = this.current.find(item => item.state === 'inactive');\n if (inactiveItem) {\n Line truncated
{"version":3,"file":"artplayer-plugin-danmu.js","sources":["../../node_modules/@babel/runtime/helpers/arrayWithHoles.js","../../node_modules/@babel/runtime/helpers/iterableToArrayLimit.js","../../node_modules/@babel/runtime/helpers/nonIterableRest.js","../../node_modules/@babel/runtime/helpers/slicedToArray.js","../../packages/artplayer-plugin-danmu/src/bilibiliDanmuParse.js","../../node_modules/@babel/runtime/helpers/classCallCheck.js","../../node_modules/@babel/runtime/helpers/createClass.js","../../packages/artplayer-plugin-danmu/src/danmuku.js","../../packages/artplayer-plugin-danmu/src/index.js"],"sourcesContent":["function _arrayWithHoles(arr) {\n if (Array.isArray(arr)) return arr;\n}\n\nmodule.exports = _arrayWithHoles;","function _iterableToArrayLimit(arr, i) {\n var _arr = [];\n var _n = true;\n var _d = false;\n var _e = undefined;\n\n try {\n for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {\n _arr.push(_s.value);\n\n if (i && _arr.length === i) break;\n }\n } catch (err) {\n _d = true;\n _e = err;\n } finally {\n try {\n if (!_n && _i[\"return\"] != null) _i[\"return\"]();\n } finally {\n if (_d) throw _e;\n }\n }\n\n return _arr;\n}\n\nmodule.exports = _iterableToArrayLimit;","function _nonIterableRest() {\n throw new TypeError(\"Invalid attempt to destructure non-iterable instance\");\n}\n\nmodule.exports = _nonIterableRest;","var arrayWithHoles = require(\"./arrayWithHoles\");\n\nvar iterableToArrayLimit = require(\"./iterableToArrayLimit\");\n\nvar nonIterableRest = require(\"./nonIterableRest\");\n\nfunction _slicedToArray(arr, i) {\n return arrayWithHoles(arr) || iterableToArrayLimit(arr, i) || nonIterableRest();\n}\n\nmodule.exports = _slicedToArray;","export function bilibiliDanmuParseFromXml(xmlString) {\n if (typeof xmlString !== 'string') return [];\n const regSrt = '<d p=\"(.+)\">(.+)</d>';\n const listReg = new RegExp(regSrt, 'gi');\n const itemReg = new RegExp(regSrt, 'i');\n const srtList = xmlString.match(listReg);\n return srtList.length\n ? srtList.map(item => {\n const [, attrStr, text] = item.match(itemReg);\n const attr = attrStr.split(',').map(Number);\n return attr.length === 8 && text.trim()\n ? {\n text,\n time: attr[0],\n mode: attr[1],\n size: attr[2],\n color: `#${attr[3].toString(16)}`,\n timestamp: attr[4],\n pool: attr[5],\n userID: attr[6],\n rowID: attr[7],\n }\n : null;\n })\n : [];\n}\n\nexport function bilibiliDanmuParseFromAv(av) {\n const corsUrl = 'https://cors-anywhere.herokuapp.com/';\n return fetch(`${corsUrl}https://api.bilibili.com/x/web-interface/view?aid=${av}`)\n .then(res => res.json())\n .then(res => {\n if (res.code === 0 && res.data && res.data.cid) {\n return fetch(`${corsUrl}https://api.bilibili.com/x/v1/dm/list.so?oid=${res.data.cid}`)\n .then(res => res.text())\n .then(xmlString => {\n return bilibiliDanmuParseFromXml(xmlString);\n });\n }\n throw new Error(`Unable to get data: ${JSON.stringify(res)}`);\n });\n}","function _classCallCheck(instance, Constructor) {\n if (!(instance instanceof Constructor)) {\n throw new TypeError(\"Cannot call a class as a function\");\n }\n}\n\nmodule.exports = _classCallCheck;","function _defineProperties(target, props) {\n for (var i = 0; i < props.length; i++) {\n var descriptor = props[i];\n descriptor.enumerable = descriptor.enumerable || false;\n descriptor.configurable = true;\n if (\"value\" in descriptor) descriptor.writable = true;\n Object.defineProperty(target, descriptor.key, descriptor);\n }\n}\n\nfunction _createClass(Constructor, protoProps, staticProps) {\n if (protoProps) _defineProperties(Constructor.prototype, protoProps);\n if (staticProps) _defineProperties(Constructor, staticProps);\n return Constructor;\n}\n\nmodule.exports = _createClass;","export default class Danmuku {\n constructor(art, option = {}) {\n this.art = art;\n this.queue = {};\n this.option = Object.assign({}, Danmuku.option, option);\n art.constructor.validator(this.option, Danmuku.scheme);\n this.option.danmus.forEach(this.emit);\n this.current = [];\n this.layer = {};\n this.isStop = false;\n this.timer = null;\n this.init();\n }\n\n static get option() {\n return {\n danmus: [],\n speed: 5,\n opacity: 1,\n color: '#fff',\n size: 14,\n };\n }\n\n static get scheme() {\n return {\n danmus: {\n Line truncated
@@ -1,4 +1,4 @@
export default function bilibiliDanmuParse(xmlString) {
export function bilibiliDanmuParseFromXml(xmlString) {
if (typeof xmlString !== 'string') return [];
const regSrt = '<d p="(.+)">(.+)</d>';
const listReg = new RegExp(regSrt, 'gi');
@@ -7,20 +7,36 @@ export default function bilibiliDanmuParse(xmlString) {
return srtList.length
? srtList.map(item => {
const [, attrStr, text] = item.match(itemReg);
const attr = attrStr.split(',');
const attr = attrStr.split(',').map(Number);
return attr.length === 8 && text.trim()
? {
text,
time: Number(attr[0]),
mode: Number(attr[1]),
size: Number(attr[2]),
color: `#${Number(attr[3]).toString(16)}`,
timestamp: Number(attr[4]),
pool: Number(attr[5]),
userID: Number(attr[6]),
rowID: Number(attr[7]),
time: attr[0],
mode: attr[1],
size: attr[2],
color: `#${attr[3].toString(16)}`,
timestamp: attr[4],
pool: attr[5],
userID: attr[6],
rowID: attr[7],
}
: null;
})
: [];
}
export function bilibiliDanmuParseFromAv(av) {
const corsUrl = 'https://cors-anywhere.herokuapp.com/';
return fetch(`${corsUrl}https://api.bilibili.com/x/web-interface/view?aid=${av}`)
.then(res => res.json())
.then(res => {
if (res.code === 0 && res.data && res.data.cid) {
return fetch(`${corsUrl}https://api.bilibili.com/x/v1/dm/list.so?oid=${res.data.cid}`)
.then(res => res.text())
.then(xmlString => {
return bilibiliDanmuParseFromXml(xmlString);
});
}
throw new Error(`Unable to get data: ${JSON.stringify(res)}`);
});
}
+3 -2
View File
@@ -1,5 +1,5 @@
import { bilibiliDanmuParseFromXml, bilibiliDanmuParseFromAv } from './bilibiliDanmuParse';
import Danmuku from './danmuku';
import bilibiliDanmuParse from './bilibiliDanmuParse';
function artplayerPluginDanmu(option) {
return art => {
@@ -15,6 +15,7 @@ function artplayerPluginDanmu(option) {
};
}
artplayerPluginDanmu.bilibiliDanmuParse = bilibiliDanmuParse;
artplayerPluginDanmu.bilibiliDanmuParseFromXml = bilibiliDanmuParseFromXml;
artplayerPluginDanmu.bilibiliDanmuParseFromAv = bilibiliDanmuParseFromAv;
window.artplayerPluginDanmu = artplayerPluginDanmu;
export default artplayerPluginDanmu;