弹幕池支持异步返回

This commit is contained in:
Harvey Zack committed 2019-04-26 12:05:59 +08:00
1 parent 9bd1452030
commit 9d730146c0
5 files changed
+64 -64

No files matched your search

+8 -2
View File
@@ -3,5 +3,11 @@ var art = new Artplayer({
container: '.artplayer-app',
url: url + '/video/you-name.mp4',
autoSize: true,
plugins: [artplayerPluginDanmu()],
});
plugins: [artplayerPluginDanmu({
danmus: () => artplayerPluginDanmu.bilibiliDanmuParseFromAv(7185185),
speed: 5,
opacity: 1,
color: '#fff',
size: 14,
})],
});
+33 -34
View File
@@ -52,27 +52,24 @@
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);
var srtList = xmlString.match(/<d([\S ]*?>[\S ]*?)<\/d>/gi);
return srtList.length ? srtList.map(function (item) {
var _item$match = item.match(itemReg),
var _item$match = item.match(/<d p="(.+)">(.+)<\/d>/),
_item$match2 = slicedToArray(_item$match, 3),
attrStr = _item$match2[1],
text = _item$match2[2];
var attr = attrStr.split(',').map(Number);
var attr = attrStr.split(',');
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],
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: attr[6],
rowID: attr[7]
rowID: Number(attr[7])
} : null;
}) : [];
}
@@ -123,20 +120,31 @@
/*#__PURE__*/
function () {
function Danmuku(art) {
var _this = this;
var option = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
classCallCheck(this, Danmuku);
this.art = art;
this.queue = {};
this.option = Object.assign({}, Danmuku.option, option);
art.constructor.validator(this.option, Danmuku.scheme);
this.option.danmus.forEach(this.emit);
this.current = [];
this.layer = {};
this.isStop = false;
this.timer = null;
this.init();
this.option = Object.assign({}, Danmuku.option, option);
art.constructor.validator(this.option, Danmuku.scheme);
if (typeof this.option.danmus === 'function') {
this.option.danmus().then(function (danmus) {
danmus.forEach(_this.emit);
_this.init();
});
} else {
this.option.danmus.forEach(this.emit);
this.init();
}
}
createClass(Danmuku, [{
@@ -237,28 +245,28 @@
}, {
key: "update",
value: function update() {
var _this = this;
var _this2 = this;
var _this$art = this.art,
$player = _this$art.template.$player,
player = _this$art.player;
this.timer = window.requestAnimationFrame(function () {
_this.current.forEach(function (item) {
if (Date.now() - item.time >= _this.option.speed) {
_this2.current.forEach(function (item) {
if (Date.now() - item.time >= _this2.option.speed) {
item.state = 'inactive';
}
});
Object.keys(_this.queue).filter(function (time) {
Object.keys(_this2.queue).filter(function (time) {
return player.currentTime + 0.5 >= time && time >= player.currentTime - 0.5;
}).reduce(function (result, key) {
return result.concat(_this.queue[key]);
return result.concat(_this2.queue[key]);
}, []).forEach(function (item) {
console.log(item);
});
if (!_this.isStop && player.playing) {
_this.update();
if (!_this2.isStop && player.playing) {
_this2.update();
}
});
}
@@ -302,16 +310,7 @@
key: "scheme",
get: function get() {
return {
danmus: {
type: 'array',
child: {
text: 'string',
size: 'number',
time: 'number',
color: 'string',
mode: 'number'
}
},
danmus: 'array|function',
speed: 'number',
opacity: 'number',
color: 'string',
@@ -1 +1 @@
{"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
{"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 srtList = xmlString.match(/<d([\\S ]*?>[\\S ]*?)<\\/d>/gi);\n return srtList.length\n ? srtList.map(item => {\n const [, attrStr, text] = item.match(/<d p=\"(.+)\">(.+)<\\/d>/);\n const attr = attrStr.split(',');\n return attr.length === 8 && text.trim()\n ? {\n text,\n time: Number(attr[0]),\n mode: Number(attr[1]),\n size: Number(attr[2]),\n color: `#${Number(attr[3]).toString(16)}`,\n timestamp: Number(attr[4]),\n pool: Number(attr[5]),\n userID: attr[6],\n rowID: Number(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.current = [];\n this.layer = {};\n this.isStop = false;\n this.timer = null;\n this.option = Object.assign({}, Danmuku.option, option);\n art.constructor.validator(this.option, Danmuku.scheme);\n if (typeof this.option.danmus === 'function') {\n this.option.danmus().then(danmus => {\n danmus.forEach(this.emit);\n this.init();\n });\n } else {\n this.option.danmus.forEach(this.emit);\n this.init();\n }\n }\n\n static get option() {\n return {\n danmus: [],\n sLine truncated
@@ -1,24 +1,21 @@
export function bilibiliDanmuParseFromXml(xmlString) {
if (typeof xmlString !== 'string') return [];
const regSrt = '<d p="(.+)">(.+)</d>';
const listReg = new RegExp(regSrt, 'gi');
const itemReg = new RegExp(regSrt, 'i');
const srtList = xmlString.match(listReg);
const srtList = xmlString.match(/<d([\S ]*?>[\S ]*?)<\/d>/gi);
return srtList.length
? srtList.map(item => {
const [, attrStr, text] = item.match(itemReg);
const attr = attrStr.split(',').map(Number);
const [, attrStr, text] = item.match(/<d p="(.+)">(.+)<\/d>/);
const attr = attrStr.split(',');
return attr.length === 8 && text.trim()
? {
text,
time: attr[0],
mode: attr[1],
size: attr[2],
color: `#${attr[3].toString(16)}`,
timestamp: attr[4],
pool: attr[5],
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: attr[6],
rowID: attr[7],
rowID: Number(attr[7]),
}
: null;
})
+12 -14
View File
@@ -2,14 +2,21 @@ export default class Danmuku {
constructor(art, option = {}) {
this.art = art;
this.queue = {};
this.option = Object.assign({}, Danmuku.option, option);
art.constructor.validator(this.option, Danmuku.scheme);
this.option.danmus.forEach(this.emit);
this.current = [];
this.layer = {};
this.isStop = false;
this.timer = null;
this.init();
this.option = Object.assign({}, Danmuku.option, option);
art.constructor.validator(this.option, Danmuku.scheme);
if (typeof this.option.danmus === 'function') {
this.option.danmus().then(danmus => {
danmus.forEach(this.emit);
this.init();
});
} else {
this.option.danmus.forEach(this.emit);
this.init();
}
}
static get option() {
@@ -24,16 +31,7 @@ export default class Danmuku {
static get scheme() {
return {
danmus: {
type: 'array',
child: {
text: 'string',
size: 'number',
time: 'number',
color: 'string',
mode: 'number',
},
},
danmus: 'array|function',
speed: 'number',
opacity: 'number',
color: 'string',