Compare commits

..

No commits in common. "6a1beeb78968c0d8a19b9a2dd31f2351dc03e9cf" and "4bb9a618be8a8cce01d9172d23164491aca29ae3" have entirely different histories.

2 changed files with 252 additions and 285 deletions

View File

@ -63,117 +63,116 @@ addEventListener('load', function (e) {
} }
}); });
function return_message(message, target_window) { function return_message(message,target_window){
if (target_window === undefined) { if(target_window===undefined){
target_window = window.parent; target_window = window.parent;
} }
let url_params = new URLSearchParams(location.search); let url_params = new URLSearchParams(location.search);
let widgetid = url_params.get('widgetid'); let widgetid = url_params.get('widgetid');
let additional_info = { from: 'invidious_control' }; let additional_info = {from:'invidious_control'};
if (widgetid !== null) { if(widgetid!==null){
additional_info.widgetid = widgetid; additional_info.widgetid = widgetid;
} }
if (message.message_kind === 'event') { if(message.message_kind==='event'&&message.eventname==='timeupdate'||message.eventname==='loadedmetadata'){
if (message.eventname === 'timeupdate' || message.eventname === 'loadedmetadata') { let add_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};
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 }; additional_info['value'] = add_value;
} }
if(message.eventname==='error'){
additional_info['value'] = {geterrorcode:player.error().code};
} }
if (message.eventname === 'error') { message = Object.assign(additional_info,message);
additional_info['value'] = { geterrorcode: player.error().code };
}
message = Object.assign(additional_info, message);
let target_origin = url_params.get('origin') || '*'; let target_origin = url_params.get('origin') || '*';
target_window.postMessage(message, target_origin); target_window.postMessage(message,target_origin);
} }
function control_embed_iframe(message) { function control_embed_iframe(message){
const url_params = new URLSearchParams(location.search); let url_params = new URLSearchParams(location.search);
const origin = url_params.get('origin'); let origin = url_params.get('origin');
const origin_equal = origin === null || origin === message.origin; if(origin===null||origin===message.origin){
if (origin_equal) { let widgetid = url_params.get('widgetid');
const widgetid = url_params.get('widgetid'); if((widgetid===null&&message.data.widgetid===null)||widgetid===message.data.widgetid&&message.data.target==='invidious_control'){
const widgetid_equal = (widgetid === null && message.data.widgetid === null) || widgetid === message.data.widgetid; switch(message.data.eventname){
const target_name_equal = message.data.target === 'invidious_control';
const eventname_string_check = typeof message.data.eventname === 'string';
if (widgetid_equal && target_name_equal && eventname_string_check) {
let message_return_value;
switch (message.data.eventname) {
case 'play': case 'play':
player.play(); player.play();
break; break;
case 'pause': case 'pause':
player.pause(); player.pause();
break; break;
case 'getvolume':
return_message({command:'getvolume',value:player.volume(),message_kind:'info_return'},message.source);
break;
case 'setvolume': case 'setvolume':
player.volume(message.data.value); player.volume(message.data.value);
break; break;
case 'getduration':
return_message({command:'getduration',value:player.duration(),message_kind:'info_return'},message.source);
break;
case 'getcurrenttime':
return_message({command:'getcurrenttime',value:player.currentTime(),message_kind:'info_return'},message.source);
break;
case 'seek': case 'seek':
const duration = player.duration(); const duration = player.duration();
let newTime = helpers.clamp(message.data.value, 0, duration); let newTime = helpers.clamp(message.data.value, 0, duration);
player.currentTime(newTime); player.currentTime(newTime);
break; break;
case 'getplaystatus':
return_message({command:'getplaystatus',value:player.paused(),message_kind:'info_return'},message.source);
break;
case 'getplaybackrate':
return_message({command:'getplaybackrate',value:player.playbackRate(),message_kind:'info_return'},message.source);
break;
case 'setplaybackrate': case 'setplaybackrate':
player.playbackRate(message.data.value); player.playbackRate(message.data.value);
break; break;
case 'getavailableplaybackrates':
return_message({command:'getavailableplaybackrates',value:options.playbackRates,message_kind:'info_return'},message.source);
break;
case 'getloopstatus':
return_message({command:'getloopstatus',value:player.loop(),message_kind:'info_return'},message.source);
break;
case 'setloopstatus': case 'setloopstatus':
player.loop(message.data.value); player.loop(message.data.value);
break; break;
case 'getmutestatus':
return_message({command:'getmutestatus',value:player.muted(),message_kind:'info_return'},message.source);
break;
case 'setmutestatus':
player.muted(message.data.value);
break;
case 'gettitle':
return_message({command:'gettitle',value:player_data.title,message_kind:'info_return'},message.source);
break;
case 'getfullscreenstatus':
return_message({command:'getfullscreenstatus',value:player.isFullscreen(),message_kind:'info_return'},message.source);
break;
case 'requestfullscreen': case 'requestfullscreen':
player.requestFullscreen(); player.requestFullscreen();
break; break;
case 'exitfullscreen': case 'exitfullscreen':
player.exitFullscreen(); player.exitFullscreen();
break; break;
case 'getvolume':
message_return_value = player.volume();
break;
case 'getduration':
message_return_value = player.duration();
break;
case 'getcurrenttime':
message_return_value = player.currentTime();
break;
case 'getplaystatus':
message_return_value = player.paused();
break;
case 'getplaybackrate':
message_return_value = player.playbackRate();
break;
case 'getavailableplaybackrates':
message_return_value = options.playbackRates;
break;
case 'getloopstatus':
message_return_value = player.loop();
break;
case 'getmutestatus':
message_return_value = player.muted();
break;
case 'gettitle':
message_return_value = player_data.title;
break;
case 'getfullscreenstatus':
message_return_value = player.isFullscreen();
break;
case 'geterrorcode': case 'geterrorcode':
message_return_value = player.error().code; return_message({command:'geterrorcode',value:player.error().code,message_kind:'info_return'},message.source);
break; break;
default: case 'getplaylist':
console.info("Unhandled event name: " + message.data.eventname);
break;
}
if (message_return_value !== undefined) {
return_message({ command: message.data.eventname, value: message_return_value, message_kind: 'info_return' }, message.source);
} }
} }
} }
} }
if (new URLSearchParams(location.search).get('enablejsapi') === '1') { if(new URLSearchParams(location.search).get('enablejsapi')==='1'){
window.addEventListener('message', control_embed_iframe); window.addEventListener('message',control_embed_iframe);
const event_list = ['ended', 'error', 'ratechange', 'volumechange', 'waiting', 'timeupdate', 'loadedmetadata', 'play', 'seeking', 'seeked', 'playerresize', 'pause']; player.on('ended',function(){return_message({message_kind:'event',eventname:'ended'})});
event_list.forEach(event_name => { player.on('error',function(){return_message({message_kind:'event',eventname:'error'})});
player.on(event_name, function () { return_message({ message_kind: 'event', eventname: event_name }) }); player.on('ratechange',function(){return_message({message_kind:'event',eventname:'ratechange'})});
}); player.on('volumechange',function(){return_message({message_kind:'event',eventname:'volumechange'})});
player.on('waiting',function(){return_message({message_kind:'event',eventname:'waiting'})});
player.on('timeupdate',function(){return_message({message_kind:'event',eventname:'timeupdate'})});
player.on('loadedmetadata',function(){return_message({message_kind:'event',eventname:'loadedmetadata'})});
player.on('play',function(){return_message({message_kind:'event',eventname:'play'})});
player.on('seeking',function(){return_message({message_kind:'event',eventname:'seeking'})});
player.on('seeked',function(){return_message({message_kind:'event',eventname:'seeked'})});
player.on('playerresize',function(){return_message({message_kind:'event',eventname:'playerresize'})});
player.on('pause',function(){return_message({message_kind:'event',eventname:'pause'})});
} }

View File

@ -1,156 +1,148 @@
class invidious_embed { class invidious_embed{
static widgetid = 0; static widgetid = 0;
static eventname_table = { onPlaybackRateChange: 'ratechange', onStateChange: 'statechange', onerror: 'error' }; static eventname_table = {onPlaybackRateChange:'ratechange',onStateChange:'statechange',onerror:'error'};
static available_event_name = ['ended', 'error', 'ratechange', 'volumechange', 'waiting', 'timeupdate', 'loadedmetadata', 'play', 'seeking', 'seeked', 'playerresize', 'pause'];
static api_promise = false; static api_promise = false;
static invidious_instance = ''; addEventListener(eventname,func){
addEventListener(eventname, func) { 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 { else{
try{
this.eventobject[eventname].push(func); this.eventobject[eventname].push(func);
} }
catch{}
} }
}
removeEventListner(eventname, func) { removeEventListner(eventname,func){
let internal_eventname; var 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{
internal_eventname = eventname; internal_eventname = eventname;
} }
this.eventobject[internal_eventname] = this.eventobject[internal_eventname].fillter(x => x !== func); this.eventobject[internal_eventname] = this.eventobject[internal_eventname].fillter(x=>x!==func);
} }
Player(element,options){
Player(element, options) {
this.player_status = -1; this.player_status = -1;
this.error_code = 0; this.error_code = 0;
this.volume = 100; this.volume = 100;
this.eventobject = { ready: [], ended: [], error: [], ratechange: [], volumechange: [], waiting: [], timeupdate: [], loadedmetadata: [], play: [], seeking: [], seeked: [], playerresize: [], pause: [], statechange: [] }; this.eventobject = {ready:[],ended:[],error:[],ratechange:[],volumechange:[],waiting:[],timeupdate:[],loadedmetadata:[],play:[],seeking:[],seeked:[],playerresize:[],pause:[],statechange:[]};
let replace_elemnt; var 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 = ''; var 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{
iframe_src = invidious_embed.invidious_instance;
}
else {
iframe_src = 'https://vid.puffyan.us';//I set most hot instanse but this may need discuss or change ay to default instanse iframe_src = 'https://vid.puffyan.us';//I set most hot instanse but this may need discuss or change ay to default instanse
} }
this.target_origin = iframe_src.slice(); this.target_origin = iframe_src.slice();
iframe_src += '/embed/'; iframe_src += '/embed/';
if (typeof options.videoId === 'string' && options.videoId.length === 11) { if(typeof options.videoId==='string'&&options.videoId.length===11){
iframe_src += options.videoId; iframe_src += options.videoId;
this.videoId = options.videoId; this.videoId = options.videoId;
} }
else { else{
this.error_code = 2; this.error_code = 2;
this.event_executor('error'); this.event_executor('error');
return;
} }
let search_params = new URLSearchParams(''); var search_params = new URLSearchParams('');
search_params.append('widgetid', invidious_embed.widgetid); search_params.append('widgetid',invidious_embed.widgetid);
this.widgetid = invidious_embed.widgetid; this.widgetid = invidious_embed.widgetid;
invidious_embed.widgetid++; invidious_embed.widgetid++;
search_params.append('origin', location.origin); search_params.append('origin',location.origin);
search_params.append('enablejsapi', '1'); search_params.append('enablejsapi','1');
if (typeof options.playerVars === 'object') { if(typeof options.playerVars==='object'){
this.option_playerVars = options.playerVars; this.option_playerVars = options.playerVars;
for (let x in options.playerVars) { for(var x in options.playerVars){
if (typeof x === 'string' && typeof options.playerVars[x] === 'string') { if(typeof x==='string'&&typeof options.playerVars[x]==='string'){
search_params.append(x, options.playerVars[x]); search_params.append(x,options.playerVars[x]);
} }
} }
if (options.playerVars.autoplay === undefined) { if(options.playerVars.autoplay===undefined){
search_params.append('autoplay', '0'); search_params.append('autoplay','0');
} }
} }
iframe_src += "?" + search_params.toString(); iframe_src += "?" + search_params.toString();
if (typeof options.events === 'object') { if(options.events!==undefined&&typeof options.events==='object'){
for (let x in options.events) { for(let x in options.events){
this.addEventListener(x, options.events[x]); this.addEventListener(x,options.events[x]);
} }
} }
this.player_iframe = document.createElement("iframe"); this.player_iframe = document.createElement("iframe");
this.loaded = false; this.loaded = false;
this.addEventListener('loadedmetadata', () => { this.event_executor('ready'); this.loaded = true }); this.addEventListener('loadedmetadata',()=>{this.event_executor('ready');this.loaded=true});
this.addEventListener('loadedmetadata', () => { this.setVolume(this.volume) }); this.addEventListener('loadedmetadata',()=>{this.setVolume(this.volume)});
this.player_iframe.src = iframe_src; this.player_iframe.src = iframe_src;
if (typeof options.width === 'number') { if(options.width!==undefined&&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(options.width!==undefined&&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";
replace_elemnt.replaceWith(this.player_iframe); replace_elemnt.replaceWith(this.player_iframe);
this.eventdata = {}; this.eventdata = {};
return this; return this;
} }
postMessage(data){
postMessage(data) { var additionalInfo = {'origin':location.origin,'widgetid':String(this.widgetid),'target':'invidious_control'};
const additionalInfo = { 'origin': location.origin, 'widgetid': this.widgetid.toString(), 'target': 'invidious_control' }; data = Object.assign(additionalInfo,data);
data = Object.assign(additionalInfo, data); this.player_iframe.contentWindow.postMessage(data,this.target_origin);
this.player_iframe.contentWindow.postMessage(data, this.target_origin);
} }
event_executor(eventname){
event_executor(eventname) { var execute_functions = this.eventobject[eventname];
const execute_functions = this.eventobject[eventname]; var return_data = {data:undefined,target:this};
let return_data = { data: undefined, target: this }; if(eventname==='statechange'){
if (eventname === 'statechange') {
return_data.data = this.getPlayerState(); return_data.data = this.getPlayerState();
} }
for (let x = 0; x < execute_functions.length; x++) { for(var x=0;x<execute_functions.length;x++){
try { try{
execute_functions[x](return_data); execute_functions[x](return_data);
} }
catch { } catch{}
} }
} }
receiveMessage(message){
receiveMessage(message) { if(message.data.from==='invidious_control'&&message.data.widgetid===String(this.widgetid)){
if (message.data.from === 'invidious_control' && message.data.widgetid === String(this.widgetid)) { switch(message.data.message_kind){
switch (message.data.message_kind) {
case 'info_return': case 'info_return':
const promise_array = this.message_wait[message.data.command]; var promise_array = this.message_wait[message.data.command];
promise_array.forEach(element => { for(var x=0;x<promise_array.length;x++){
if (message.data.command === 'getvolume') { if(message.data.command==='getvolume'){
element(message.data.value * 100); promise_array[x](message.data.value*100);
}
else{
promise_array[x](message.data.value);
} }
else {
element(message.data.value);
} }
});
this.message_wait[message.data.command] = []; this.message_wait[message.data.command] = [];
break; break;
case 'event': case 'event':
if (typeof message.data.eventname === 'string') { if(message.data.eventname!==undefined&&typeof message.data.eventname==='string'){
this.event_executor(message.data.eventname); this.event_executor(message.data.eventname);
const previous_status = this.player_status; var previous_status = this.player_status;
switch (message.data.eventname) { switch(message.data.eventname){
case 'ended': case 'ended':
this.player_status = 0; this.player_status = 0;
break; break;
@ -159,7 +151,7 @@ class invidious_embed {
break; break;
case 'timeupdate': case 'timeupdate':
this.player_status = 1; this.player_status = 1;
this.eventdata = Object.assign({}, this.eventdata, message.data.value); this.eventdata = Object.assign({},this.eventdata,message.data.value);
break; break;
case 'pause': case 'pause':
this.player_status = 2; this.player_status = 2;
@ -168,251 +160,227 @@ class invidious_embed {
this.player_status = 3; this.player_status = 3;
break; break;
case 'loadedmetadata': case 'loadedmetadata':
this.eventdata = Object.assign({}, this.eventdata, message.data.value); this.eventdata = Object.assign({},this.eventdata,message.data.value);
break; break;
} }
if (previous_status !== this.player_status) { if(previous_status!==this.player_status){
this.event_executor('statechange'); this.event_executor('statechange');
} }
} }
} }
} }
} }
promise_resolve(){
promise_send_event(event_name) { var res_outer;
if (invidious_embed.api_promise) { var pro = new Promise((res,rej)=>{res_outer=res});
const promise_object = new Promise((resolve, reject) => { this.message_wait[event_name].push(resolve) }); return {'promise':pro,'resolve':res_outer};
this.postMessage({ eventname: event_name });
return promise_object;
} }
else { promise_send_event(event_name){
if(invidious_embed.api_promise){
var pro_object = this.promise_resolve();
this.message_wait[event_name].push(pro_object.resolve);
this.postMessage({eventname:event_name});
return pro_object.promise;
}
else{
return this.eventdata[event_name]; return this.eventdata[event_name];
} }
} }
getPlayerState(){
getPlayerState() {
return this.player_status; return this.player_status;
} }
playVideo(){
playVideo() { this.postMessage({eventname:'play'});
this.postMessage({ eventname: 'play' });
} }
pauseVideo(){
pauseVideo() { this.postMessage({eventname:'pause'});
this.postMessage({ eventname: 'pause' });
} }
getVolume(){
getVolume() {
return this.promise_send_event('getvolume'); return this.promise_send_event('getvolume');
} }
setVolume(volume){
setVolume(volume) { volume = Number(volume);
if (typeof volume === 'number') {
this.volume = volume; this.volume = volume;
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 { getIframe(){
console.warn("setVolume first argument must be number");
}
}
getIframe() {
return this.player_iframe; return this.player_iframe;
} }
destroy(){
destroy() {
this.player_iframe.remove(); this.player_iframe.remove();
} }
mute(){
mute() { this.postMessage({eventname:'setmutestatus',value:true});
this.postMessage({ eventname: 'setmutestatus', value: true });
} }
unMute(){
unMute() { this.postMessage({eventname:'setmutestatus',value:false});
this.postMessage({ eventname: 'setmutestatus', value: false });
} }
isMuted(){
isMuted() {
return this.promise_send_event('getmutestatus'); return this.promise_send_event('getmutestatus');
} }
seekTo(seconds,allowSeekAhead){//seconds must be a number and allowSeekAhead is ignore
seekTo(seconds, allowSeekAhead) {//seconds must be a number and allowSeekAhead is ignore
seconds = Number(seconds); seconds = Number(seconds);
if (seconds !== NaN && seconds !== undefined) { if(seconds!==NaN&&seconds!==undefined){
this.postMessage({ eventname: 'seek', value: seconds }); this.postMessage({eventname:'seek',value:seconds});
} }
} }
setSize(width,height){//width and height must be Number
setSize(width, height) {//width and height must be Number
this.player_iframe.width = Number(width); this.player_iframe.width = Number(width);
this.player_iframe.height = Number(height); this.player_iframe.height = Number(height);
} }
getPlaybackRate(){
getPlaybackRate() {
return this.promise_send_event('getplaybackrate'); return this.promise_send_event('getplaybackrate');
} }
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
suggestedRate = Number(suggestedRate); suggestedRate = Number(suggestedRate);
if (suggestedRate !== NaN && suggestedRate !== undefined) { if(suggestedRate!==NaN&&suggestedRate!==undefined){
this.postMessage({ eventname: 'setplaybackrate', value: suggestedRate }); this.postMessage({eventname:'setplaybackrate',value:suggestedRate});
} }
} }
getAvailablePlaybackRates(){
getAvailablePlaybackRates() {
return this.promise_send_event('getavailableplaybackrates'); return this.promise_send_event('getavailableplaybackrates');
} }
playOtherVideoById(option,autoplay,startSeconds_arg){//internal fuction
playOtherVideoById(option, autoplay, startSeconds_arg) {//internal fuction
let videoId = ''; let videoId = '';
let startSeconds = 0; let startSeconds = -1;
let endSeconds = -1; let endSeconds = -1;
let mediaContetUrl = ''; let mediaContetUrl = '';
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(startSeconds_arg!==undefined&&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(option.videoId!==undefined&&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;
} }
} }
else if (typeof option.mediaContentUrl === 'string') { else if(option.mediaContentUrl!==undefined&&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;
} }
if (typeof option.startSeconds === 'number' && option.startSeconds >= 0) { if(option.startSeconds!==undefined&&typeof option.startSeconds==='number'&&option.startSeconds>=0){
startSeconds = option.startSeconds; startSeconds = option.startSeconds;
} }
if (typeof option.endSeconds === 'number' && option.endSeconds >= 0) { if(option.endSeconds!==undefined&&typeof option.endSeconds==='number'&&option.endSeconds>=0){
endSeconds = option.endSeconds; endSeconds = option.endSeconds;
} }
} }
if (mediaContetUrl.length > 0) { if(mediaContetUrl.length>0){
var tmp_videoId = ''; var tmp_videoId = '';
if (mediaContetUrl.indexOf('/v/') !== -1) { if(mediaContetUrl.indexOf('/v/')!==-1){
var end_pos = mediaContetUrl.length - 1; var end_pos = mediaContetUrl.length-1;
if (mediaContetUrl.indexOf('?') !== -1) { if(mediaContetUrl.indexOf('?')!==-1){
end_pos = mediaContetUrl.indexOf('?'); end_pos = mediaContetUrl.indexOf('?');
} }
tmp_videoId = mediaContetUrl.substring(mediaContetUrl.indexOf('/v/'), end_pos); tmp_videoId = mediaContetUrl.substring(mediaContetUrl.indexOf('/v/'),end_pos);
} }
else { else{
tmp_videoId = new URL(mediaContetUrl).searchParams.get('v'); tmp_videoId = new URL(mediaContetUrl).searchParams.get('v');
} }
if (tmp_videoId === null || tmp_videoId.length !== 11) { if(tmp_videoId===null||tmp_videoId.length!==11){
this.error_code = 2; this.error_code = 2;
this.event_executor('error'); this.event_executor('error');
return;
} }
videoId = tmp_videoId; videoId = tmp_videoId;
} }
let iframe_sorce = this.target_origin.slice(); var iframe_sorce = this.target_origin.slice();
iframe_sorce += "/embed/" + videoId; iframe_sorce += "/embed/" + videoId;
this.videoId = videoId; this.videoId = videoId;
let search_params = new URLSearchParams(''); var search_params = new URLSearchParams('');
search_params.append('origin', location.origin); search_params.append('origin',location.origin);
search_params.append('enablejsapi', '1'); search_params.append('enablejsapi','1');
search_params.append('widgetid', invidious_embed.widgetid); search_params.append('widgetid',invidious_embed.widgetid);
this.widgetid = invidious_embed.widgetid; this.widgetid = invidious_embed.widgetid;
invidious_embed.widgetid++; invidious_embed.widgetid++;
search_params.append('autoplay', Number(autoplay)); if(autoplay){
if (this.option_playerVars !== undefined) { search_params.append('autoplay',1);
Object.keys(this.option_playerVars).forEach(key => {
if (key !== 'autoplay' && key !== 'start' && key !== 'end') {
search_params.append(key, this.option_playerVars[key]);
} }
}) else{
search_params.append('autoplay',0);
} }
if (startSeconds > 0) { if(this.option_playerVars!==undefined){
search_params.append('start', startSeconds); for(var x in this.option_playerVars){
if(x!=='autoplay'&&x!=='start'&&x!=='end'){
search_params.append(x,this.option_playerVars[x]);
} }
if (endSeconds !== -1 && endSeconds >= 0) {
if (endSeconds > startSeconds) {
search_params.append('end', endSeconds);
} }
else { }
throw 'Invalid end seconds because end seconds before start seconds'; if(startSeconds!==-1&&startSeconds>=0){
search_params.append('start',startSeconds);
}
if(endSeconds!==-1&&endSeconds>=0){
if(endSeconds>startSeconds){
search_params.append('end',endSeconds);
}
else{
throw 'invalid end seconds';
} }
} }
iframe_sorce += "?" + search_params.toString(); iframe_sorce += "?" + search_params.toString();
this.player_iframe.src = iframe_sorce; this.player_iframe.src = iframe_sorce;
if (autoplay) { if(autoplay){
this.player_status = 5; this.player_status = 5;
} }
this.eventdata = {}; this.eventdata = {};
} }
loadVideoById(option,startSeconds){
loadVideoById(option, startSeconds) { this.playOtherVideoById(option,true,startSeconds);
this.playOtherVideoById(option, true, startSeconds);
} }
cueVideoById(option,startSeconds){
cueVideoById(option, startSeconds) { this.playOtherVideoById(option,false,startSeconds);
this.playOtherVideoById(option, false, startSeconds);
} }
cueVideoByUrl(option,startSeconds){
cueVideoByUrl(option, startSeconds) { this.playOtherVideoById(option,false,startSeconds);
this.playOtherVideoById(option, false, startSeconds);
} }
loadVideoByUrl(option,startSeconds){
loadVideoByUrl(option, startSeconds) { this.playOtherVideoById(option,true,startSeconds);
this.playOtherVideoById(option, true, startSeconds);
} }
getDuration(){
getDuration() {
return this.promise_send_event('getduration'); return this.promise_send_event('getduration');
} }
getVideoUrl(){
getVideoUrl() {
return this.target_origin + "/watch?v=" + this.videoId; return this.target_origin + "/watch?v=" + this.videoId;
} }
async getVideoEmbedCode(){
async getVideoEmbedCode() {
var title = await this.getVideoTitle(); var title = await this.getVideoTitle();
return '<iframe width="560" height="315" src="' + this.target_origin + '/embed/' + this.videoId + '" title="' + title.replace('"', "'") + '" frameborder="0" allow="autoplay;encrypted-media;picture-in-picture;web-share" allowfullscreen></iframe>'; return '<iframe width="560" height="315" src="' + this.target_origin + "/embed/" + this.videoId + '" title="' + title.replace('"','') + '" frameborder="0" allow="autoplay;encrypted-media;picture-in-picture; web-share" allowfullscreen></iframe>';
} }
getCurrentTime(){
getCurrentTime() {
return this.promise_send_event('getcurrenttime'); return this.promise_send_event('getcurrenttime');
} }
constructor(element,options){
constructor(element, options) { this.Player(element,options);
this.Player(element, options); window.addEventListener('message',(ms)=>{this.receiveMessage(ms)});
window.addEventListener('message', (ms) => { this.receiveMessage(ms) }); this.message_wait = {getvolume:[],getmutestatus:[],getduration:[],getcurrenttime:[],getplaybackrate:[],getavailableplaybackrates:[],gettitle:[]};
this.message_wait = { getvolume: [], getmutestatus: [], getduration: [], getcurrenttime: [], getplaybackrate: [], getavailableplaybackrates: [], gettitle: [] };
} }
async getVideoData(){
async getVideoData() { return {video_id:this.videoId,title:await this.promise_send_event('gettitle')};
return { video_id: this.videoId, title: await this.promise_send_event('gettitle') };
} }
} }
function invidious_ready(func) { function invidious_ready(func){
if (typeof func === 'function') { if(typeof func==='function'){
func(); func();
} }
} }
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 }; try{
try {
onInvidiousIframeAPIReady(); onInvidiousIframeAPIReady();
} }
catch { } catch{}