fix bugs, rollback event function and readable

This commit is contained in:
bonjinnorenka 2023-09-08 00:46:37 +09:00
parent b1a1e5230d
commit c3ea7fb72c
2 changed files with 119 additions and 54 deletions

View File

@ -22,6 +22,8 @@ function get_playlist(plid) {
player.on('ended', function () {
var url = new URL('https://example.com/embed/' + response.nextVideo);
const search_params = new URLSearchParams(location.search);
url.searchParams.set('list', plid);
if (!plid.startsWith('RD'))
url.searchParams.set('index', response.index);
@ -33,6 +35,12 @@ function get_playlist(plid) {
url.searchParams.set('speed', video_data.params.speed);
if (video_data.params.local !== video_data.preferences.local)
url.searchParams.set('local', video_data.params.local);
if (search_params.get('widgetid') !== null)
url.searchParams.set('widgetid', search_params.get('widgetid'));
if (search_params.get('origin') !== null)
url.searchParams.set('origin', search_params.get('origin'));
if (search_params.get('enablejsapi') !== null)
url.searchParams.set('enablejsapi', search_params.get('enablejsapi'));
location.assign(url.pathname + url.search);
});

View File

@ -9,17 +9,41 @@ class invidious_embed {
static videodata_cahce = {};
addEventListener(eventname, func) {
if (typeof func === 'function') {
if (eventname in invidious_embed.eventname_table) {
eventname = invidious_embed.eventname_table[eventname];
this.eventobject[invidious_embed.eventname_table[eventname]].push(func);
} else if (invidious_embed.available_event_name.includes(eventname)) {
this.eventobject[eventname].push(func);
} else {
console.warn('addEventListener cannot find such eventname : ' + eventname);
}
} else {
console.warn("addEventListner secound args must be function");
}
this.eventElement.addEventListener(eventname,func);
}
removeEventListener(eventname, func) {
if (typeof func === 'function') {
let internal_eventname;
if (eventname in invidious_embed.eventname_table) {
eventname = invidious_embed.eventname_table[eventname];
internal_eventname = invidious_embed.eventname_table[eventname];
} else if (invidious_embed.available_event_name.includes(eventname)) {
internal_eventname = eventname;
} else {
console.warn('removeEventListner cannot find such eventname : ' + eventname);
return;
}
this.eventobject[internal_eventname] = this.eventobject[internal_eventname].filter(x => {
const allowFunctionDetected = x.toString()[0] === '(';
if (allowFunctionDetected) {
x.toString() !== func.toString();
} else {
x !== func;
}
});
} else {
console.warn("removeEventListener secound args must be function");
}
this.eventElement.removeEventListener(eventname,func);
}
async instance_access_check(instance_origin) {
@ -110,7 +134,6 @@ class invidious_embed {
}
async Player(element, options) {
this.eventElement = document.createElement("span");
this.player_status = -1;
this.error_code = 0;
this.volume = 100;
@ -118,6 +141,7 @@ class invidious_embed {
this.playlistVideoIds = [];
this.eventobject = { ready: [], ended: [], error: [], ratechange: [], volumechange: [], waiting: [], timeupdate: [], loadedmetadata: [], play: [], seeking: [], seeked: [], playerresize: [], pause: [], statechange: [] };
let replace_elemnt;
this.isPlaylistVideoList = false;
if (element === undefined || element === null) {
throw 'Please, pass element id or HTMLElement as first argument';
} else if (typeof element === 'string') {
@ -176,7 +200,7 @@ class invidious_embed {
} else {
console.warn('player vars key must be string');
}
})
});
if (options.playerVars.start !== undefined) {
no_start_parameter = false;
}
@ -191,14 +215,19 @@ class invidious_embed {
}
iframe_src += "?" + search_params.toString();
if (typeof options.events === 'object') {
for (let x in options.events) {
this.addEventListener(x, options.events[x]);
Object.keys(options.events).forEach(key => {
if (typeof options.events[key] === 'function') {
this.addEventListener(key, options.events[key]);
} else {
console.warn('event function must be function');
}
});
}
this.player_iframe = document.createElement("iframe");
this.loaded = false;
this.addEventListener('loadedmetadata', () => { this.event_executor('ready'); this.loaded = true; });
this.addEventListener('loadedmetadata', () => { this.setVolume(this.volume); });
this.addEventListener('ended', () => { if (this.isPlaylistVideoList) { this.nextVideo() } })
this.player_iframe.src = iframe_src;
if (typeof options.width === 'number') {
this.player_iframe.width = options.width;
@ -206,8 +235,8 @@ class invidious_embed {
this.player_iframe.width = 640;
this.player_iframe.style.maxWidth = '100%';
}
if (typeof options.width === 'number') {
this.player_iframe.width = options.width;
if (typeof options.height === 'number') {
this.player_iframe.height = options.height;
} else {
this.player_iframe.height = this.player_iframe.width * (9 / 16);
}
@ -224,13 +253,22 @@ class invidious_embed {
}
event_executor(eventname) {
let event_parameter = {detail:undefined};
if (eventname === 'statechange') {
event_parameter.detail = this.getPlayerState();
} else if (eventname === 'error'){
event_parameter.detail = this.error_code;
const execute_functions = this.eventobject[eventname];
let return_data = { type: eventname, data: null, target: this };
switch (eventname) {
case 'statechange':
return_data.data = this.getPlayerState();
break;
case 'error':
return_data.data = this.error_code;
}
this.eventElement.dispatchEvent(new Event(eventname,event_parameter));
execute_functions.forEach(func => {
try {
func(return_data);
} catch (e) {
console.error(e);
}
});
}
receiveMessage(message) {
@ -415,7 +453,7 @@ class invidious_embed {
}
}
if (mediaContetUrl.length > 0) {
const match_result = mediaContetUrl.match(/\/([A-Za-z0-9]{11})\//);
const match_result = mediaContetUrl.match(/\/([A-Za-z0-9]{11})/);
if (match_result !== null && match_result.length === 2) {
videoId = match_result[1];
} else {
@ -472,29 +510,34 @@ class invidious_embed {
}
loadVideoById(option, startSeconds) {
this.isPlaylistVideoList = false;
this.playOtherVideoById(option, true, startSeconds, {});
}
cueVideoById(option, startSeconds) {
this.isPlaylistVideoList = false;
this.playOtherVideoById(option, false, startSeconds, {});
}
cueVideoByUrl(option, startSeconds) {
this.isPlaylistVideoList = false;
this.playOtherVideoById(option, false, startSeconds, {});
}
loadVideoByUrl(option, startSeconds) {
this.isPlaylistVideoList = false;
this.playOtherVideoById(option, true, startSeconds, {});
}
async playPlaylist(playlistData, autoplay, index, startSeconds) {
let playlistId;
if (typeof playlistData === 'string') {
this.playlistVideoIds = await this.getPlaylistVideoids(playlistData);
playlistId = playlistData;
this.playlistVideoIds = [playlistData];
this.isPlaylistVideoList = true;
} else if (typeof playlistData === 'object') {
if (Array.isArray(playlistData)) {
this.playlistVideoIds = playlistData;
this.isPlaylistVideoList = true;
} else {
index = playlistData['index'];
let listType = 'playlist';
@ -503,13 +546,11 @@ class invidious_embed {
}
switch (listType) {
case 'playlist':
if (Array.isArray(playlistData['list'])) {
this.playlistVideoIds = playlistData['list'];
} else if(typeof playlistData['list'] === 'string') {
if (typeof playlistData['list'] === 'string') {
this.playlistVideoIds = await this.getPlaylistVideoids(playlistData['list']);
playlistId = playlistData['list'];
} else {
console.error('playlist data list must be string or array of strings');
console.error('playlist data list must be string');
return;
}
break;
@ -521,6 +562,9 @@ class invidious_embed {
return;
}
}
if (typeof playlistData.startSeconds === 'number') {
startSeconds = playlistData.startSeconds;
}
} else {
console.error('playlist function first argument must be string or array of string');
return;
@ -541,6 +585,11 @@ class invidious_embed {
parameter['list'] = playlistId;
this.playlistId = playlistId;
}
this.sub_index = parameter.index;
if (index >= this.playlistVideoIds.length) {
index = 0;
parameter.index = 0;
}
this.playOtherVideoById(this.playlistVideoIds[index], autoplay, startSeconds, parameter);
}
@ -566,6 +615,9 @@ class invidious_embed {
async nextVideo() {
let now_index = this.promise_send_event('getplaylistindex');
if (now_index === null) {
now_index = this.sub_index;
}
if (now_index === this.playlistVideoIds.length - 1) {
if (this.loop) {
now_index = 0;
@ -576,6 +628,7 @@ class invidious_embed {
} else {
now_index++;
}
this.sub_index = now_index;
let parameter = { index: now_index };
if (this.playlistId !== undefined) {
parameter['list'] = this.playlistId;
@ -585,6 +638,9 @@ class invidious_embed {
async previousVideo() {
let now_index = this.promise_send_event('getplaylistindex');
if (now_index === null) {
now_index = this.sub_index;
}
if (now_index === 0) {
if (this.loop) {
now_index = this.playlistVideoIds.length - 1;
@ -595,6 +651,7 @@ class invidious_embed {
} else {
now_index--;
}
this.sub_index = now_index;
let parameter = { index: now_index };
if (this.playlistId !== undefined) {
parameter['list'] = this.playlistId;