add ads.js

This commit is contained in:
Harvey committed 2021-12-23 13:45:50 +08:00
1 parent 0048991f7e
commit 6c32895d34
7 files changed
+124 -4

No files matched your search

+45
View File
@@ -0,0 +1,45 @@
export default class Ads {
constructor(art) {
this.art = art;
const { option } = art;
this.index = 0;
this.playing = false;
this.isEnd = false;
this.urlCache = option.url;
if (option.ads.length) {
this.playing = true;
this.play(option.ads[this.index]);
}
}
get current() {
return this.art.option.ads[this.index];
}
play(item) {
const { option, player } = this.art;
player.url = item.url;
this.art.emit('ads:start', item);
this.art.once('video:ended', () => {
const nextIndex = this.index + 1;
const nextItem = option.ads[nextIndex];
if (nextItem) {
this.index = nextIndex;
this.play(nextItem);
} else {
this.end();
}
});
}
end() {
this.playing = false;
this.isEnd = true;
this.art.option.url = this.urlCache;
this.art.emit('ads:end');
}
}
+2
View File
@@ -22,6 +22,7 @@ import Icons from './icons';
import Setting from './setting';
import Storage from './storage';
import Plugins from './plugins';
import Ads from './ads';
import Mobile from './mobile';
let id = 0;
@@ -52,6 +53,7 @@ export default class Artplayer extends Emitter {
this.controls = new Control(this);
this.contextmenu = new Contextmenu(this);
this.subtitle = new Subtitle(this);
this.ads = new Ads(this);
this.info = new Info(this);
this.loading = new Loading(this);
this.hotkey = new Hotkey(this);
+8 -1
View File
@@ -34,5 +34,12 @@ export default function attrInit(art, player) {
}
$video.controls = false;
player.url = option.url;
if (option.ads.length) {
art.on('ads:end', () => {
player.url = option.url;
});
} else {
player.url = option.url;
}
}