添加弹幕验证

This commit is contained in:
Harvey committed 2020-04-26 09:36:12 +08:00
1 parent 7f3b116851
commit ae7d87e08b
6 files changed
+52 -25

No files matched your search

@@ -26,12 +26,12 @@ export default class Danmuku {
art.on('resize', this.resize.bind(this));
art.on('destroy', this.stop.bind(this));
if (typeof this.option.danmuku === 'function') {
this.option.danmuku().then(danmus => {
this.option.danmuku().then((danmus) => {
danmus.forEach(this.emit.bind(this));
art.emit('artplayerPluginDanmuku:loaded');
});
} else if (typeof this.option.danmuku === 'string') {
bilibiliDanmuParseFromUrl(this.option.danmuku).then(danmus => {
bilibiliDanmuParseFromUrl(this.option.danmuku).then((danmus) => {
danmus.forEach(this.emit.bind(this));
art.emit('artplayerPluginDanmuku:loaded');
});
@@ -82,7 +82,7 @@ export default class Danmuku {
}
continue() {
filter(this.queue, 'stop', danmu => {
filter(this.queue, 'stop', (danmu) => {
danmu.$state = 'emit';
danmu.$lastStartTime = Date.now();
switch (danmu.mode) {
@@ -98,7 +98,7 @@ export default class Danmuku {
suspend() {
const { $player } = this.art.template;
filter(this.queue, 'emit', danmu => {
filter(this.queue, 'emit', (danmu) => {
danmu.$state = 'stop';
switch (danmu.mode) {
case 0: {
@@ -118,7 +118,7 @@ export default class Danmuku {
resize() {
const { $player } = this.art.template;
const danmuLeft = getRect($player, 'width');
filter(this.queue, 'wait', danmu => {
filter(this.queue, 'wait', (danmu) => {
if (danmu.$ref) {
danmu.$ref.style.border = 'none';
danmu.$ref.style.left = `${danmuLeft}px`;
@@ -138,7 +138,7 @@ export default class Danmuku {
if (player.playing && !this.isHide) {
const danmuLeft = getRect($player, 'width');
filter(this.queue, 'emit', danmu => {
filter(this.queue, 'emit', (danmu) => {
danmu.$restTime -= (Date.now() - danmu.$lastStartTime) / 1000;
danmu.$lastStartTime = Date.now();
if (danmu.$restTime <= 0) {
@@ -153,19 +153,19 @@ export default class Danmuku {
this.queue
.filter(
danmu =>
(danmu) =>
player.currentTime + 0.1 >= danmu.time &&
danmu.time >= player.currentTime - 0.1 &&
danmu.$state === 'wait',
)
.forEach(danmu => {
.forEach((danmu) => {
danmu.$ref = getDanmuRef(this.queue);
this.$danmuku.appendChild(danmu.$ref);
danmu.$ref.style.opacity = this.option.opacity;
danmu.$ref.style.fontSize = `${this.option.fontSize}px`;
danmu.$ref.innerText = danmu.text;
danmu.$ref.style.color = danmu.color;
danmu.$ref.style.border = danmu.border ? `1px solid ${danmu.color}` : 'none';
danmu.$ref.style.color = danmu.color || '#fff';
danmu.$ref.style.border = danmu.border ? `1px solid ${danmu.color || '#fff'}` : 'none';
danmu.$restTime =
this.option.synchronousPlayback && player.playbackRate
? this.option.speed / Number(player.playbackRate)
@@ -228,6 +228,19 @@ export default class Danmuku {
emit(danmu) {
const { notice, player, i18n } = this.art;
const {
utils: { clamp },
validator,
} = this.art.constructor;
validator(danmu, {
text: 'string',
mode: 'number|undefined',
color: 'string|undefined',
time: 'number|undefined',
border: 'boolean|undefined',
});
if (!danmu.text.trim()) {
notice.show = i18n.get('Danmu text cannot be empty');
return;
@@ -238,8 +251,10 @@ export default class Danmuku {
return;
}
if (typeof danmu.time !== 'number') {
danmu.time = player.currentTime;
if (danmu.time) {
danmu.time = clamp(danmu.time, 0, Infinity);
} else {
danmu.time = player.currentTime + 0.5;
}
this.queue.push({
+6 -5
View File
@@ -25,6 +25,7 @@ import Plugins from './plugins';
import Mobile from './mobile';
let id = 0;
const instances = [];
class Artplayer extends Emitter {
constructor(option) {
super();
@@ -56,7 +57,11 @@ class Artplayer extends Emitter {
id += 1;
this.id = id;
Artplayer.instances.push(this);
instances.push(this);
}
static get instances() {
return instances;
}
static get version() {
@@ -158,10 +163,6 @@ class Artplayer extends Emitter {
}
}
utils.def(Artplayer, 'instances', {
value: [],
});
// eslint-disable-next-line no-console
console.log(
'%c ArtPlayer %c __VERSION__ %c https://artplayer.org',