Improved support for Linux

This commit is contained in:
Markil3
2021-09-04 15:11:36 -06:00
parent 5e4ae23978
commit c24d6c2367
11 changed files with 297 additions and 174 deletions

View File

@@ -0,0 +1,92 @@
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 onStatusUpdate(status, time, songData)
{
sendUpdate({
type: "edu.regis.universeplayer.PlaybackInfo",
currentSong: songData,
status: status,
playTime: 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");
});
}