playVideoOnDreambox as WebExtension
[playVideoOnDreambox.git] / src / background-script.js
1 function handleToolbarPlay()
2 {
3     browser.tabs.query({currentWindow: true, active: true}).then(
4         function (tabs) {
5             playUrl(tabs.shift().url);
6         }
7     );
8 }
9
10 function handleMenuClick(event)
11 {
12     if ('mediaType' in event && event.mediaType == "video") {
13         playUrl(event.srcUrl);
14     } else if ('linkUrl' in event) {
15         playUrl(event.linkUrl);
16     } else {
17         console.error('No idea what to play here', event);
18     }
19 }
20
21 function playUrl(url)
22 {
23     browser.browserAction.setBadgeText({text: '⧗'});
24     browser.browserAction.setBadgeTextColor({color: 'white'});
25     browser.browserAction.setBadgeBackgroundColor({color: 'orange'});
26
27     browser.storage.sync.get('proxyUrl').then((res) => {
28         var proxyUrl = res.proxyUrl;
29         console.log(url, proxyUrl);
30
31         fetch(
32             proxyUrl,
33             {
34                 method: 'POST',
35                 headers: {
36                     'Content-Type': 'text/plain'
37                 },
38                 //mode: 'no-cors',
39                 body: url
40             }
41         ).then(function (response) {
42             console.log(response.ok);
43             if (response.ok) {
44                 videoPlayOk(response);
45             } else {
46                 videoPlayError(response);
47             }
48         }).catch(function (error) {
49             //e.g. Network error (when no network available)
50             showError(error.message);
51         });
52     });
53 }
54
55 function videoPlayOk(response)
56 {
57     browser.notifications.create(
58         'dreambox-playing',
59         {
60             type: 'basic',
61             title: 'Play video on Dreambox',
62             message: 'Video is playing now',
63             iconUrl: 'icon.png'
64         }
65     );
66
67     browser.browserAction.setBadgeText({text: '🗸'});
68     browser.browserAction.setBadgeTextColor({color: 'white'});
69     browser.browserAction.setBadgeBackgroundColor({color: 'green'});
70     setBadgeRemovalTimeout();
71 }
72
73 function videoPlayError(response)
74 {
75     response.text().then(function (text) {
76         showError(text);
77     });
78 }
79
80 function showError(message)
81 {
82     console.log("Play error: ", message);
83
84     browser.notifications.create(
85         'dreambox-error',
86         {
87             type: 'basic',
88             title: 'Error playing video',
89             message: message,
90             iconUrl: 'icon.png'
91         }
92     );
93
94     browser.browserAction.setBadgeText({text: 'x'});
95     browser.browserAction.setBadgeTextColor({color: 'white'});
96     browser.browserAction.setBadgeBackgroundColor({color: 'red'});
97     setBadgeRemovalTimeout();
98 }
99
100 function setBadgeRemovalTimeout()
101 {
102     setTimeout(
103         function () {
104             browser.browserAction.setBadgeText({text:""});
105         },
106         5000
107     );
108 }
109
110 //toolbar button
111 browser.browserAction.onClicked.addListener(handleToolbarPlay);
112
113 //context menu
114 browser.menus.create({
115     id: "play-video-on-dreambox-link",
116     title: "Play linked video on dreambox",
117     contexts: ["link"],
118     onclick: handleMenuClick,
119     icons: {
120         "16": "icon.png",
121         "32": "icon.png",
122     }
123 });
124 browser.menus.create({
125     id: "play-video-on-dreambox-video",
126     title: "Play this video on dreambox",
127     contexts: ["video"],
128     onclick: handleMenuClick,
129     icons: {
130         "16": "icon.png",
131         "32": "icon.png",
132     }
133 });