Files
UniversalMusicPlayer/add-on/foreground.js
Markil3 ede9132279 Adds the ability to play youtube videos.
It still needs a bit of work, but playing and pausing are operational.
2021-08-16 22:42:44 -06:00

82 lines
1.7 KiB
JavaScript

console.debug("Loading foreground.js")
let background;
function handleMessage(message)
{
if (typeof message == "object" && "type" in message)
{
let type = message.type;
lastPeriod = type.lastIndexOf(".");
if (lastPeriod != -1)
{
type = type.substring(lastPeriod + 1);
}
switch (type)
{
case "QueryStatus":
return getState();
case "QueryTime":
return getTime();
case "QueryLength":
return getLength();
case "CommandSetPlayback":
switch (message.status)
{
case "PLAY":
return play();
case "PAUSE":
return pause();
}
return false;
case "CommandSeek":
return seek(message.time);
}
}
}
function sendUpdate(response)
{
post = data => background.postMessage({
type: "update",
data: data
});
if (response instanceof Promise)
{
response.then(post);
}
else
{
post(response);
}
}
background = browser.runtime.connect({name:"universalMusic"});
background.onMessage.addListener(message => {
let num = message.num;
response = handleMessage(message.data);
post = data => background.postMessage({
type: "response",
num: num,
data: data
});
if (response instanceof Promise)
{
response.then(post);
}
else
{
post(response);
}
});
if (document.readyState === "complete")
{
sendUpdate("loaded");
}
else
{
sendUpdate("loaded");
window.addEventListener("load", () => {
sendUpdate("loaded");
});
}