mirror of
https://github.com/iv-org/invidious.git
synced 2025-01-08 14:07:56 -05:00
improve readable and fix YT overwrite bug
This commit is contained in:
parent
1d67e13d9c
commit
cb27c6a33a
@ -75,7 +75,7 @@ function return_message(message, target_window) {
|
|||||||
}
|
}
|
||||||
if (message.message_kind === 'event') {
|
if (message.message_kind === 'event') {
|
||||||
if (message.eventname === 'timeupdate' || message.eventname === 'loadedmetadata') {
|
if (message.eventname === 'timeupdate' || message.eventname === 'loadedmetadata') {
|
||||||
additional_info['value'] = { getvolume: player.volume(), getduration: player.duration(), getcurrenttime: player.currentTime(), getplaystatus: player.paused(), getplaybackrate: player.playbackRate(), getloopstatus: player.loop(), getmutestatus: player.muted(), getfullscreenstatus: player.isFullscreen(), getavailableplaybackrates: options.playbackRates, gettitle: player_data.title, getplaylistindex: video_data.index, getplaylistid: video_data.plid};
|
additional_info['value'] = { getvolume: player.volume(), getduration: player.duration(), getcurrenttime: player.currentTime(), getplaystatus: player.paused(), getplaybackrate: player.playbackRate(), getloopstatus: player.loop(), getmutestatus: player.muted(), getfullscreenstatus: player.isFullscreen(), getavailableplaybackrates: options.playbackRates, gettitle: player_data.title, getplaylistindex: video_data.index, getplaylistid: video_data.plid };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (message.eventname === 'error') {
|
if (message.eventname === 'error') {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
class invidious_embed {
|
class invidious_embed {
|
||||||
static widgetid = 0;
|
static widgetid = 0;
|
||||||
static eventname_table = { onPlaybackRateChange: 'ratechange', onStateChange: 'statechange', onError: 'error' ,onReady: 'ready'};
|
static eventname_table = { onPlaybackRateChange: 'ratechange', onStateChange: 'statechange', onError: 'error', onReady: 'ready' };
|
||||||
static available_event_name = ['ready', 'ended', 'error', 'ratechange', 'volumechange', 'waiting', 'timeupdate', 'loadedmetadata', 'play', 'seeking', 'seeked', 'playerresize', 'pause'];
|
static available_event_name = ['ready', 'ended', 'error', 'ratechange', 'volumechange', 'waiting', 'timeupdate', 'loadedmetadata', 'play', 'seeking', 'seeked', 'playerresize', 'pause'];
|
||||||
static api_promise = false;
|
static api_promise = false;
|
||||||
static invidious_instance = '';
|
static invidious_instance = '';
|
||||||
@ -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");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -56,28 +49,24 @@ class invidious_embed {
|
|||||||
let return_status;
|
let return_status;
|
||||||
const status_cahce_exist = instance_origin in invidious_embed.instance_status_list;
|
const status_cahce_exist = instance_origin in invidious_embed.instance_status_list;
|
||||||
if (!status_cahce_exist) {
|
if (!status_cahce_exist) {
|
||||||
try{
|
try {
|
||||||
const instance_stats = await fetch(instance_origin + '/api/v1/stats');
|
const instance_stats = await fetch(instance_origin + '/api/v1/stats');
|
||||||
if (instance_stats.ok) {
|
if (instance_stats.ok) {
|
||||||
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];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -98,45 +87,35 @@ 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();
|
||||||
}
|
}
|
||||||
for (let x=0;x<invidious_embed.api_instance_list.length;x++) {
|
for (let x = 0; x < invidious_embed.api_instance_list.length; x++) {
|
||||||
if(await this.instance_access_check(invidious_embed.api_instance_list[x])){
|
if (await this.instance_access_check(invidious_embed.api_instance_list[x])) {
|
||||||
invidious_embed.invidious_instance = invidious_embed.api_instance_list[x];
|
invidious_embed.invidious_instance = invidious_embed.api_instance_list[x];
|
||||||
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;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async videoid_accessable_check(videoid){
|
async videoid_accessable_check(videoid) {
|
||||||
const video_api_response = await fetch(invidious_embed.invidious_instance + "/api/v1/videos/" + videoid);
|
const video_api_response = await fetch(invidious_embed.invidious_instance + "/api/v1/videos/" + videoid);
|
||||||
return video_api_response.ok;
|
return video_api_response.ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
async getPlaylistVideoids(playlistid) {
|
async getPlaylistVideoids(playlistid) {
|
||||||
const playlist_api_response = await fetch(invidious_embed.invidious_instance + "/api/v1/playlists/" + playlistid);
|
const playlist_api_response = await fetch(invidious_embed.invidious_instance + "/api/v1/playlists/" + playlistid);
|
||||||
if (playlist_api_response.ok){
|
if (playlist_api_response.ok) {
|
||||||
const playlist_api_json = await playlist_api_response.json();
|
const playlist_api_json = await playlist_api_response.json();
|
||||||
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";
|
||||||
@ -249,10 +221,9 @@ class invidious_embed {
|
|||||||
return_data.data = this.getPlayerState();
|
return_data.data = this.getPlayerState();
|
||||||
}
|
}
|
||||||
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,18 +338,16 @@ 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')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
setSize(width, height) {//width and height must be Number
|
setSize(width, height) {//width and height must be Number
|
||||||
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');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -391,15 +357,13 @@ class invidious_embed {
|
|||||||
}
|
}
|
||||||
|
|
||||||
setPlaybackRate(suggestedRate) {//suggestedRate must be number.this player allow not available playback rate such as 1.4
|
setPlaybackRate(suggestedRate) {//suggestedRate must be number.this player allow not available playback rate such as 1.4
|
||||||
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;
|
||||||
@ -452,10 +411,9 @@ class invidious_embed {
|
|||||||
}
|
}
|
||||||
if (mediaContetUrl.length > 0) {
|
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){
|
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';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -536,7 +493,7 @@ class invidious_embed {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async getVideoData() {
|
async getVideoData() {
|
||||||
return { video_id: this.videoId, title: await this.promise_send_event('gettitle'), list:this.promise_send_event('getplaylistid') };
|
return { video_id: this.videoId, title: await this.promise_send_event('gettitle'), list: this.promise_send_event('getplaylistid') };
|
||||||
}
|
}
|
||||||
|
|
||||||
getPlaylistIndex() {
|
getPlaylistIndex() {
|
||||||
@ -563,8 +520,6 @@ function invidious_ready(func) {
|
|||||||
}
|
}
|
||||||
invidious_embed.invidious_instance = new URL(document.currentScript.src).origin;
|
invidious_embed.invidious_instance = new URL(document.currentScript.src).origin;
|
||||||
const invidious = { Player: invidious_embed, PlayerState: { ENDED: 0, PLAYING: 1, PAUSED: 2, BUFFERING: 3, CUED: 5 }, ready: invidious_ready };
|
const invidious = { Player: invidious_embed, PlayerState: { ENDED: 0, PLAYING: 1, PAUSED: 2, BUFFERING: 3, CUED: 5 }, ready: invidious_ready };
|
||||||
if (typeof onInvidiousIframeAPIReady === 'function'){
|
if (typeof onInvidiousIframeAPIReady === 'function') {
|
||||||
onInvidiousIframeAPIReady();
|
onInvidiousIframeAPIReady();
|
||||||
}
|
}
|
||||||
|
|
||||||
const YT = invidious;
|
|
||||||
|
Loading…
Reference in New Issue
Block a user