Adds the ability to play youtube videos.
It still needs a bit of work, but playing and pausing are operational.
This commit is contained in:
82
add-on/foreground.js
Normal file
82
add-on/foreground.js
Normal file
@@ -0,0 +1,82 @@
|
||||
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");
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user