Skip to content

Commit d1bd53c

Browse files
committed
Support for Web Page Media Control API v0.4
1 parent 05c3342 commit d1bd53c

1 file changed

Lines changed: 23 additions & 18 deletions

File tree

extension/media-control-api.js

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
// Tab registration by MediaControlled event
2-
document.addEventListener("MediaControlled", function () {
3-
chrome.runtime.sendMessage({command: "registerTab"});
4-
});
5-
6-
// Tab unregistration by MediaUncontrolled event
7-
document.addEventListener("MediaUncontrolled", function () {
8-
chrome.runtime.sendMessage({command: "unregisterTab"});
9-
});
10-
111
// Media Events emmiter
122
chrome.runtime.onMessage.addListener(function(request) {
133
switch (request.command) {
@@ -29,16 +19,31 @@ chrome.runtime.onMessage.addListener(function(request) {
2919
}
3020
});
3121

32-
// Tell document that we are ready
33-
console.log('Keysocket Media Control API initialized');
34-
document.dispatchEvent(new Event("MediaControlApiInit"));
35-
36-
// Tab registration by meta tag
37-
if (document.getElementsByName("media-controlled").length > 0) {
38-
chrome.runtime.sendMessage({command: "registerTab"});
39-
}
22+
// Tab registration/unregistration by MediaControlStateChanged event
23+
document.addEventListener("MediaControlStateChanged", function () {
24+
registerOrUnregisterPage();
25+
});
4026

4127
// Unregister tab before move to another URI
4228
window.onunload = function() {
4329
chrome.runtime.sendMessage({command: "unregisterTab"});
30+
}
31+
32+
// Initial tab registration by meta tag
33+
registerOrUnregisterPage();
34+
35+
function isPageMediaControllable() {
36+
var tags = document.getElementsByName("media-controllable");
37+
if (tags.length > 0) {
38+
for (var i = 0; i < tags.length; i++) {
39+
if (tags[i].getAttribute("content") !== 'no') {
40+
return true;
41+
}
42+
}
43+
}
44+
return false;
45+
}
46+
47+
function registerOrUnregisterPage() {
48+
chrome.runtime.sendMessage({command: isPageMediaControllable() ? "registerTab" : "unregisterTab"});
4449
}

0 commit comments

Comments
 (0)