Adds full support for YouTube playback.

While a few optimizations could be made, YouTube playback is almost as seamless as local playback.
This commit is contained in:
Markil3
2021-08-17 01:23:23 -06:00
parent ede9132279
commit 9c2cea1662
23 changed files with 388 additions and 261 deletions

View File

@@ -4,13 +4,20 @@ var numTabMessages = 0;
/*
On startup, connect to the "ping_pong" app.
*/
var port = browser.runtime.connectNative("universalmusic");
var interfacePort = browser.runtime.connectNative("universalmusic");
var tabPort = null;
function setupTab(port)
{
console.info("Tab " + port.sender.tab.url + " loaded.");
if (port.sender)
{
console.info("Tab " + port.sender.tab.url + " loaded.");
}
else
{
console.info("Tab loaded");
}
tabPort = port;
tabPort.onDisconnect.addListener(e => {
tabPort = null;
@@ -25,9 +32,21 @@ function setupTab(port)
browser.tabs.remove(e.sender.tab.id);
});
tabPort.onMessage.addListener(message => {
if (typeof message == "object" && message.type == "response")
if (typeof message == "object")
{
tabMessages.set(message.num, message.data);
if (message.type == "response")
{
tabMessages.set(message.num, message.data);
}
else if (message.type == "update")
{
returnValue = {
"messageNum": -1,
"message": message.data
}
console.trace("Sending update ", returnValue)
interfacePort.postMessage(returnValue);
}
}
});
return true;
@@ -70,15 +89,20 @@ var listeners = [function (message, returnValue) {
}
switch (type)
{
case "CommandSong":
returnValue = () => browser.tabs.create({url: message.song}).then(setupTab);
case "CommandLoadSong":
returnValue = () => {
if (message.song)
{
browser.tabs.create({url: message.song});
}
}
if (tabPort)
{
console.log("Replacing tab");
/*
* Closes the existing tab first
*/
returnValue = browser.tabs.remove(e.sender.tab.id).then(returnValue);
returnValue = browser.tabs.remove(tabPort.sender.tab.id).then(returnValue);
}
else
{
@@ -89,7 +113,7 @@ var listeners = [function (message, returnValue) {
case "QueryStatus":
if (!tabPort)
{
returnValue = 4;
returnValue = "EMPTY";
break;
}
case "QueryTime":
@@ -139,6 +163,7 @@ function handleMessage(message)
{
returnValue = listeners[index](message, returnValue);
}
console.debug("Message returned ", returnValue);
if (returnValue instanceof Promise)
{
returnValue.then(resolve).catch(reject);
@@ -158,7 +183,7 @@ function handleMessage(message)
/*
* Listen for messages from the app.
*/
port.onMessage.addListener((message) => {
interfacePort.onMessage.addListener((message) => {
console.log("Received from interface: ", message);
handleMessage(message.message).then((response) => {
@@ -175,9 +200,9 @@ port.onMessage.addListener((message) => {
}
}
console.log("Sending ", returnValue)
port.postMessage(returnValue);
interfacePort.postMessage(returnValue);
}).catch(error => {
console.error("Error in evaluating message: ", error)
});
});

View File

@@ -34,6 +34,16 @@ function handleMessage(message)
}
}
function onStatusUpdate(status, time, songData)
{
sendUpdate({
type: "edu.regis.universeplayer.PlaybackInfo",
currentSong: songData,
status: status,
playTime: time
});
}
function sendUpdate(response)
{
post = data => background.postMessage({

View File

@@ -3,26 +3,51 @@ var video;
function onload()
{
video = document.getElementsByClassName('video-stream html5-main-video')[0]
video = document.getElementsByClassName('video-stream html5-main-video')[0];
statusUpdate = e => {
return onStatusUpdate(getState(), e.srcElement.currentTime, getSongData());
};
// video.addListener("timeupdate", statusUpdate);
// video.addListener("play", statusUpdate);
// video.addListener("pause", statusUpdate);
// video.addListener("ended", statusUpdate);
video.ontimeupdate = statusUpdate;
video.onplay = statusUpdate;
video.onpause = statusUpdate;
video.onended = statusUpdate;
}
function getSongData()
{
return {
type: "edu.regis.universeplayer.browser.InternetSong",
location: "window.location.href",
title: getTitle(),
artists: getArtists(),
trackNum: 0,
discNum: 0,
duration: parseInt(getLength() * 1000),
album: null
}
}
function getState()
{
if (!video)
{
return 4;
return "EMPTY";
}
else if (video.ended)
{
return 3;
return "FINISHED";
}
else if (video.paused)
{
return 1;
return "PAUSED";
}
else
{
return 0;
return "PLAYING";
}
}
@@ -36,6 +61,16 @@ function getLength()
return video.duration;
}
function getTitle()
{
return document.getElementsByTagName("meta").title.content;
}
function getArtists()
{
return [document.getElementById("channel-name").getElementsByTagName("a")[0].text];
}
function play()
{
if (video != null)