improve readable and fix YT overwrite bug

This commit is contained in:
bonjinnorenka 2023-09-03 02:27:05 +09:00
parent 1d67e13d9c
commit cb27c6a33a
2 changed files with 51 additions and 96 deletions

View File

@ -11,15 +11,12 @@ class invidious_embed {
if (typeof func === 'function') { if (typeof func === 'function') {
if (eventname in invidious_embed.eventname_table) { if (eventname in invidious_embed.eventname_table) {
this.eventobject[invidious_embed.eventname_table[eventname]].push(func); this.eventobject[invidious_embed.eventname_table[eventname]].push(func);
} } else if (invidious_embed.available_event_name.includes(eventname)) {
else if (invidious_embed.available_event_name.includes(eventname)) {
this.eventobject[eventname].push(func); this.eventobject[eventname].push(func);
} } else {
else {
console.warn('addEventListener cannot find such eventname : ' + eventname); console.warn('addEventListener cannot find such eventname : ' + eventname);
} }
} } else {
else {
console.warn("addEventListner secound args must be function"); console.warn("addEventListner secound args must be function");
} }
} }
@ -29,11 +26,9 @@ class invidious_embed {
let internal_eventname; let internal_eventname;
if (eventname in invidious_embed.eventname_table) { if (eventname in invidious_embed.eventname_table) {
internal_eventname = invidious_embed.eventname_table[eventname]; internal_eventname = invidious_embed.eventname_table[eventname];
} } else if (invidious_embed.available_event_name.includes(eventname)) {
else if (invidious_embed.available_event_name.includes(eventname)) {
internal_eventname = eventname; internal_eventname = eventname;
} } else {
else {
console.warn('removeEventListner cannot find such eventname : ' + eventname); console.warn('removeEventListner cannot find such eventname : ' + eventname);
return; return;
} }
@ -41,13 +36,11 @@ class invidious_embed {
const arrowFunctionDetected = x.toString()[0] === '('; const arrowFunctionDetected = x.toString()[0] === '(';
if (arrowFunctionDetected) { if (arrowFunctionDetected) {
x.toString() !== func.toString(); x.toString() !== func.toString();
} } else {
else{
x !== func; x !== func;
} }
}); });
} } else {
else {
console.warn("removeEventListener secound args must be function"); console.warn("removeEventListener secound args must be function");
} }
} }
@ -62,22 +55,18 @@ class invidious_embed {
const instance_stats_json = await instance_stats.json(); const instance_stats_json = await instance_stats.json();
if (instance_stats_json.software.name === 'invidious') { if (instance_stats_json.software.name === 'invidious') {
return_status = true; return_status = true;
} } else {
else {
return_status = false; return_status = false;
} }
} } else {
else {
return_status = false; return_status = false;
} }
} } catch {
catch{
return_status = false; return_status = false;
} }
invidious_embed.instance_status_list[instance_origin] = return_status; invidious_embed.instance_status_list[instance_origin] = return_status;
return return_status; return return_status;
} } else {
else {
return invidious_embed.instance_status_list[instance_origin]; return invidious_embed.instance_status_list[instance_origin];
} }
} }
@ -101,8 +90,7 @@ class invidious_embed {
async auto_instance_select() { async auto_instance_select() {
if (await this.instance_access_check(invidious_embed.invidious_instance)) { if (await this.instance_access_check(invidious_embed.invidious_instance)) {
return; return;
} } else {
else{
if (invidious_embed.api_instance_list.length === 0) { if (invidious_embed.api_instance_list.length === 0) {
await this.get_instance_list(); await this.get_instance_list();
} }
@ -112,14 +100,6 @@ class invidious_embed {
break; break;
} }
} }
/*
invidious_embed.api_instance_list.forEach(async (instance_origin) => {
if(await this.instance_access_check(instance_origin)){
invidious_embed.invidious_instance = instance_origin;
return;
}
});
*/
} }
} }
@ -135,8 +115,7 @@ class invidious_embed {
let tmp_videoid_list = []; let tmp_videoid_list = [];
playlist_api_json.videos.forEach(videodata => tmp_videoid_list.push(videodata.videoId)); playlist_api_json.videos.forEach(videodata => tmp_videoid_list.push(videodata.videoId));
return tmp_videoid_list; return tmp_videoid_list;
} } else {
else{
return []; return [];
} }
} }
@ -149,18 +128,15 @@ class invidious_embed {
let replace_elemnt; let replace_elemnt;
if (element === undefined || element === null) { if (element === undefined || element === null) {
throw 'Please, pass element id or HTMLElement as first argument'; throw 'Please, pass element id or HTMLElement as first argument';
} } else if (typeof element === 'string') {
else if (typeof element === 'string') {
replace_elemnt = document.getElementById(element); replace_elemnt = document.getElementById(element);
} } else {
else {
replace_elemnt = element; replace_elemnt = element;
} }
let iframe_src = ''; let iframe_src = '';
if (options.host !== undefined && options.host !== "") { if (options.host !== undefined && options.host !== "") {
iframe_src = new URL(options.host).origin; iframe_src = new URL(options.host).origin;
} } else if (invidious_embed.invidious_instance !== '') {
else if (invidious_embed.invidious_instance !== '') {
iframe_src = invidious_embed.invidious_instance; iframe_src = invidious_embed.invidious_instance;
} }
if (!await this.instance_access_check(iframe_src)) { if (!await this.instance_access_check(iframe_src)) {
@ -178,8 +154,7 @@ class invidious_embed {
this.event_executor('error'); this.event_executor('error');
return; return;
} }
} } else {
else {
this.error_code = 2; this.error_code = 2;
this.event_executor('error'); this.event_executor('error');
return; return;
@ -215,19 +190,16 @@ class invidious_embed {
this.player_iframe.src = iframe_src; this.player_iframe.src = iframe_src;
if (typeof options.width === 'number') { if (typeof options.width === 'number') {
this.player_iframe.width = options.width; this.player_iframe.width = options.width;
} } else {
else {
if (document.body.clientWidth < 640) { if (document.body.clientWidth < 640) {
this.player_iframe.width = document.body.clientWidth; this.player_iframe.width = document.body.clientWidth;
} } else {
else {
this.player_iframe.width = 640; this.player_iframe.width = 640;
} }
} }
if (typeof options.width === 'number') { if (typeof options.width === 'number') {
this.player_iframe.width = options.width; this.player_iframe.width = options.width;
} } else {
else {
this.player_iframe.height = this.player_iframe.width * (9 / 16); this.player_iframe.height = this.player_iframe.width * (9 / 16);
} }
this.player_iframe.style.border = "none"; this.player_iframe.style.border = "none";
@ -251,8 +223,7 @@ class invidious_embed {
execute_functions.forEach(func => { execute_functions.forEach(func => {
try { try {
func(return_data); func(return_data);
} } catch (e) {
catch(e){
console.error(e); console.error(e);
} }
}); });
@ -266,8 +237,7 @@ class invidious_embed {
promise_array.forEach(element => { promise_array.forEach(element => {
if (message.data.command === 'getvolume') { if (message.data.command === 'getvolume') {
element(message.data.value * 100); element(message.data.value * 100);
} } else {
else {
element(message.data.value); element(message.data.value);
} }
}); });
@ -311,8 +281,7 @@ class invidious_embed {
const promise_object = new Promise((resolve, reject) => { this.message_wait[event_name].push(resolve) }); const promise_object = new Promise((resolve, reject) => { this.message_wait[event_name].push(resolve) });
this.postMessage({ eventname: event_name }); this.postMessage({ eventname: event_name });
return promise_object; return promise_object;
} } else {
else {
return this.eventdata[event_name]; return this.eventdata[event_name];
} }
} }
@ -339,8 +308,7 @@ class invidious_embed {
if (volume !== NaN && volume != undefined && volume >= 0 && volume <= 100) { if (volume !== NaN && volume != undefined && volume >= 0 && volume <= 100) {
this.postMessage({ eventname: 'setvolume', value: volume / 100 }); this.postMessage({ eventname: 'setvolume', value: volume / 100 });
} }
} } else {
else {
console.warn("setVolume first argument must be number"); console.warn("setVolume first argument must be number");
} }
} }
@ -370,8 +338,7 @@ class invidious_embed {
if (seconds !== NaN && seconds !== undefined) { if (seconds !== NaN && seconds !== undefined) {
this.postMessage({ eventname: 'seek', value: seconds }); this.postMessage({ eventname: 'seek', value: seconds });
} }
} } else {
else {
console.warn('seekTo first argument type must be number') console.warn('seekTo first argument type must be number')
} }
} }
@ -380,8 +347,7 @@ class invidious_embed {
if (typeof width === 'number' && typeof height === 'number') { if (typeof width === 'number' && typeof height === 'number') {
this.player_iframe.width = width; this.player_iframe.width = width;
this.player_iframe.height = height; this.player_iframe.height = height;
} } else {
else {
console.warn('setSize first and secound argument type must be number'); console.warn('setSize first and secound argument type must be number');
} }
} }
@ -394,12 +360,10 @@ class invidious_embed {
if (typeof suggestedRate === 'number') { if (typeof suggestedRate === 'number') {
if (suggestedRate !== NaN) { if (suggestedRate !== NaN) {
this.postMessage({ eventname: 'setplaybackrate', value: suggestedRate }); this.postMessage({ eventname: 'setplaybackrate', value: suggestedRate });
} } else {
else {
console.warn('setPlaybackRate first argument NaN is no valid'); console.warn('setPlaybackRate first argument NaN is no valid');
} }
} } else {
else{
console.warn('setPlaybackRate first argument type must be number'); console.warn('setPlaybackRate first argument type must be number');
} }
} }
@ -416,29 +380,24 @@ class invidious_embed {
if (typeof option === 'string') { if (typeof option === 'string') {
if (option.length === 11) { if (option.length === 11) {
videoId = option videoId = option
} } else {
else {
mediaContetUrl = option; mediaContetUrl = option;
} }
if (typeof startSeconds_arg === 'number') { if (typeof startSeconds_arg === 'number') {
startSeconds = startSeconds_arg; startSeconds = startSeconds_arg;
} }
} } else if (typeof option === 'object') {
else if (typeof option === 'object') {
if (typeof option.videoId === 'string') { if (typeof option.videoId === 'string') {
if (option.videoId.length == 11) { if (option.videoId.length == 11) {
videoId = option.videoId; videoId = option.videoId;
} } else {
else {
this.error_code = 2; this.error_code = 2;
this.event_executor('error'); this.event_executor('error');
return; return;
} }
} } else if (typeof option.mediaContentUrl === 'string') {
else if (typeof option.mediaContentUrl === 'string') {
mediaContetUrl = option.mediaContentUrl; mediaContetUrl = option.mediaContentUrl;
} } else {
else {
this.error_code = 2; this.error_code = 2;
this.event_executor('error'); this.event_executor('error');
return; return;
@ -454,8 +413,7 @@ class invidious_embed {
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) { if (match_result !== null && match_result.length === 2) {
videoId = match_result[1]; videoId = match_result[1];
} } else {
else{
this.error_code = 2; this.error_code = 2;
this.event_executor('error'); this.event_executor('error');
return; return;
@ -489,8 +447,7 @@ class invidious_embed {
if (endSeconds !== -1 && endSeconds >= 0) { if (endSeconds !== -1 && endSeconds >= 0) {
if (endSeconds > startSeconds) { if (endSeconds > startSeconds) {
search_params.append('end', endSeconds); search_params.append('end', endSeconds);
} } else {
else {
throw 'Invalid end seconds because end seconds before start seconds'; throw 'Invalid end seconds because end seconds before start seconds';
} }
} }
@ -566,5 +523,3 @@ const invidious = { Player: invidious_embed, PlayerState: { ENDED: 0, PLAYING: 1
if (typeof onInvidiousIframeAPIReady === 'function') { if (typeof onInvidiousIframeAPIReady === 'function') {
onInvidiousIframeAPIReady(); onInvidiousIframeAPIReady();
} }
const YT = invidious;