diff --git a/scripts/bgplay.js b/scripts/bgplay.js new file mode 100644 index 0000000..d687ecb --- /dev/null +++ b/scripts/bgplay.js @@ -0,0 +1,588 @@ +/*Again a few inits */ +window.queue=[]; +window.isPlayingAud=false; +window.songIndex=0; + + + + +/*This one links to the video*/ +function createGTVideo(){ +if (document.querySelector("#GTVideo") != null) return; +var a=document.createElement("div"); +a.setAttribute("style",`height:40px;width:150px;border-radius:25px;background:rgba(255,255,255,.1);backdrop-filter:blur(10px);z-index:999999999; +position:fixed;top:150px;left:calc(50% - 75px);border:1px solid silver; +text-align:center;line-height:40px;`); +a.setAttribute("id","GTVideo") + + +a.innerHTML="Go to the Video"; +a.addEventListener("click",()=>{ +window.location.href="https://m.youtube.com/watch?v="+queue[songIndex]; +}) + +document.body.appendChild(a); + +} + + +/*The main BG Play fun*/ +async function ytproAudPlayer(id){ + + + + + +if (!queue.includes(id)) { +queue.push(id); +} + + + + +if(document.getElementById("ytproAudDiv") == null ){ +createAudElements(); +} + +var ytproURL=""; + +if(id != new URLSearchParams(window.location.search).get("v")){ +createGTVideo(); +}else{ +if (document.querySelector("#GTVideo") != null) document.querySelector("#GTVideo").remove(); +} + + + + + +var g=`https://m.youtube.com/watch?v=${id}`; +var f=await fetch(g).then(r => r.text()) + + + + +try{ +var sD=JSON.parse("{"+(f.substr(f.indexOf("streamingData")-1,((f.indexOf("playbackTracking")-1)-f.indexOf("streamingData"))))+"}"); +var vD=JSON.parse("{"+f.substr(f.indexOf("\"videoDetails"),((f.indexOf("\"trackingParams")-1)-f.indexOf("\"videoDetails")))+"}"); +}catch(e){ +history.back(); +return Android.showToast("Playback Error , Please open and issue on Github if the error persists.\n\n"+e); +} + + +var af=sD?.streamingData?.adaptiveFormats; + +var author=vD?.videoDetails?.author; +var thumb =[...vD?.videoDetails?.thumbnail?.thumbnails].pop().url; +var title=vD?.videoDetails?.title; + + + +for(var x in af){ + +if(af[x]?.itag == "140"){ +if("signatureCipher" in af[x]){ +ytproURL=ytproGetURL(af[x].signatureCipher,"sig"); +}else{ +ytproURL=ytproGetURL(af[x].url,"n"); +} + +break; + +} +} + + + +/*UI is important as well*/ +document.querySelector("#imgThumb").src=thumb; +document.querySelector("#imgThumb").style.background="url('"+thumb+"')"; +document.querySelector("#imgThumb").style.backgroundSize="cover"; +document.querySelector("#imgThumb").style.backgroundRepeat="no-repeat"; +document.querySelector("#ib").style.background="url('"+thumb+"')"; +document.querySelector("#ytProtitle").innerHTML=title; + +var ytproAud=document.querySelector("#ytproAud"); + + +ytproAud.src=ytproURL; + + +/*FallBack*/ +/*ytproAud.onerror = async function() { +console.log("error",ytproURL); +var id=new URLSearchParams(window.location.search).get("v"); +var info=await fetch("https://youtube-downloader.deno.dev/audio?id="+id); +var info=await info.json(); +for(var x in info.audio){ +if(info.audio[x].itag == "140"){ +ytproAud.src=info.audio[x].url; +ytproURL=info.audio[x].url; +console.log("NEW URL",ytproURL); +} +} +}*/ +//ytproAud.onerror = ytproAudPlayer(id); + + + + +/*Onload*/ +ytproAud.onloadeddata = async function() { + +isAP=true; +document.querySelector("#playpause").innerHTML=``; + + +var img = new Image(); +img.crossOrigin="anonymous"; +img.src=thumb; + +var iconBase64="iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII="; + + + +var canvas = document.createElement('canvas'); +var context = canvas.getContext('2d'); + +canvas.width = 160; +canvas.height = 90; + +//var z=performance.now(); + + +await new Promise((res,rej)=>{ +img.onload=()=>res(); +}); + + +//console.log("Loaded In",performance.now()-z) + + +try{ +context.drawImage(img, 0,0 ,160,90); +iconBase64 = canvas.toDataURL('image/png',1.0); +}catch{} + +if(isPlayingAud){ +setTimeout(()=>{Android.bgUpdate(iconBase64.replace("data:image/png;base64,", ""),title,author,ytproAud.duration*1000);},50); +} +else{ +isPlayingAud=true; +//console.log("NEW SERVICE"); +setTimeout(()=>{Android.bgStart(iconBase64.replace("data:image/png;base64,", ""),title,author,ytproAud.duration*1000);},50); +} + + + +ytproAud.play(); + + + +if(songIndex >= (queue.length - 1)){ +getQueue(id); +} + + + +}; + + + + + + + +/*Pause event , updates the android notification*/ +ytproAud.addEventListener('pause', async (event) => { +if(!isPlayingAud) return; +document.querySelector("#playpause").innerHTML=``; +setTimeout(()=>{Android.bgPause(ytproAud.currentTime*1000);},100); +}); +/*Play event , updates the android notification*/ +ytproAud.addEventListener('play', async (event) => { +if(!isPlayingAud) return; +document.querySelector("#playpause").innerHTML=``; +setTimeout(()=>{Android.bgPlay(ytproAud.currentTime*1000);},100); +}); + + +/*Timupdate event , updates the android notification*/ +ytproAud.addEventListener("timeupdate", async function(){ + + + + +if(!isPlayingAud) return; + +var tim=document.querySelector("#timeUpdate"); +document.querySelector("#inSeek").max=ytproAud.duration; + + +var duration=ytproAud.duration; +var currentTime=ytproAud.currentTime; + +/*To prevent the numbers from being NaN*/ +if(isNaN(duration)) return; + + +var totalTimehrs=Math.floor(duration / 3600); +var totalTimemin= Math.floor(duration % 3600 / 60); +var totalTimesec=Math.floor(duration % 3600 % 60); + +var timehrs=Math.floor(currentTime / 3600); +var timemin= Math.floor(currentTime % 3600 / 60); +var timesec= Math.floor(currentTime % 3600 % 60); +if(timehrs < 10){ timehrs = "0"+timehrs; } +if(totalTimehrs < 10){ totalTimehrs = "0"+totalTimehrs; } +if(timesec < 10){ timesec = "0"+timesec; } +if(totalTimesec < 10){ totalTimesec = "0"+totalTimesec; } +if(timemin < 10){ timemin = "0"+timemin; } +if(totalTimemin < 10){ totalTimemin = "0"+totalTimemin; } + +tim.innerHTML=`${(totalTimehrs >= 1 ) ? timehrs+":" : "" }${timemin}:${timesec}${(totalTimehrs >= 1 ) ? totalTimehrs+":" : "" }${totalTimemin}:${totalTimesec}`; + + +document.querySelector("#inSeek").value=currentTime; +document.querySelector("#prog").style.width=(currentTime/duration * 100)+"%"; +document.getElementsByClassName('video-stream')[0].pause(); + + + +if(ytproAud.currentTime==ytproAud.duration){ + +/*Nexto Songs*/ +playNext(); + +} +}); + + +/*Waits for the song , js like Mr Bean*/ +ytproAud.addEventListener('waiting', () => { +//console.log("waiting"); +document.querySelector("#playpause").innerHTML=``; +setTimeout(()=>{Android.bgBuffer(ytproAud.currentTime*1000);},50); +}); + +/*Playing event , updates da bla bla bla*/ +ytproAud.addEventListener('playing', () => { +//console.log("playing"); +document.querySelector("#playpause").innerHTML=``; +setTimeout(()=>{Android.bgPlay(ytproAud.currentTime*1000);},50); +}); + + + + + + +//console.log(id) +//console.log(queue,songIndex); + +} + + + +/*Creates da DOM for the audio player*/ +function createAudElements(){ + + +var ytADivOuter=document.createElement("div"); +var ytADiv=document.createElement("div"); +ytADivOuter.setAttribute("id","ytproAudDiv"); +ytADivOuter.setAttribute("style",`height:100%;width:100%;position:fixed;top:0;left:0;background:rgba(0,0,0,.8);z-index:999;`); + +ytADiv.setAttribute("style",`height:50%;width:98%; +background:rgba(0,0,0,.5);border:1px solid #222;position:fixed;border-radius:10px;backdrop-filter:blur(10px);bottom:15px;left:calc(1%);z-index:999;`); + +ytADiv.innerHTML=` + +