Add Dreambox web interface access token support
authorChristian Weiske <cweiske@cweiske.de>
Tue, 11 Aug 2015 19:34:27 +0000 (21:34 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Tue, 11 Aug 2015 19:34:27 +0000 (21:34 +0200)
ChangeLog [new file with mode: 0644]
README.rst
index.js

diff --git a/ChangeLog b/ChangeLog
new file mode 100644 (file)
index 0000000..f0694cc
--- /dev/null
+++ b/ChangeLog
@@ -0,0 +1,11 @@
+2015-08-11  Christian Weiske  <cweiske@cweiske.de>
+
+       * Add context menu entry to play the video on the linked page
+       * Add Dreambox web interface access token support
+       * Add private browsing mode support
+       * Version 0.2.0
+
+2015-08-10  Christian Weiske  <cweiske@cweiske.de>
+
+       * Initial version with a toolbar button to play the current tab's video
+       * Version 0.1.0
index e69953d216230a960a0a3b4e0b99932d4f7e5eac..2a4c32424cccf298d13e83ae34d0c8ab7a062bd1 100644 (file)
@@ -19,6 +19,14 @@ __ http://www.dream-multimedia-tv.de/dm7080-hd
 .. contents::
 
 
+Features
+========
+- Toolbar button to play the video on the current page
+- Context menu item to play the video on the linked page
+  (nice for video list; no need to access the detail page anymore)
+- Dreambox web interface access token support
+
+
 Configuration options
 =====================
 In the addons manager
index de9629905e6e49f0e3cb6073a2bf48c1be3c3a31..dd9695d507710d9cff3190fc359cc4fe691d6d06 100644 (file)
--- a/index.js
+++ b/index.js
@@ -2,8 +2,15 @@ var buttons = require('sdk/ui/button/action');
 var tabs = require("sdk/tabs");
 require("sdk/simple-prefs").on("", reloadPrefs);
 
+//IP or hostname of dreambox
 var dreamboxHost;
+//Full file path to "youtube-dl" binary
 var youtubedlPath;
+//dreambox web interface access token
+var dbToken;
+//number of failures fetching the access token
+var dbTokenFails = 0;
+
 reloadPrefs();
 
 function reloadPrefs()
@@ -35,7 +42,7 @@ var menuItem = contextMenu.Item({
 });
 
 function playCurrentTab(state)
-{    
+{
     console.log('active tab', tabs.activeTab.url);
     var pageUrl = tabs.activeTab.url;
     playPageUrl(pageUrl);
@@ -74,10 +81,57 @@ function playVideoOnDreambox(videoUrl)
     console.log('dreambox url: ' + dreamboxUrl);
 
     var Request = require("sdk/request").Request;
-    var latestTweetRequest = Request({
-        url: dreamboxUrl/*,
+    Request({
+        url: dreamboxUrl,
+        content: {
+            sessionid: dbToken
+        },
+        onComplete: function (response) {
+            if (response.status == 412) {
+                //web interface requires an access token
+                // fetch it and try to play the video again
+                fetchAccessToken(
+                    function() {
+                        playVideoOnDreambox(videoUrl);
+                    }
+                );
+            }
+        }
+    }).post();
+}
+
+/**
+ * Obtain an access token from the dreambox and store it
+ * in the global dbToken variable.
+ *
+ * @param function replayFunc Function to call when the token has
+ *                            been acquired successfully
+ */
+function fetchAccessToken(replayFunc)
+{
+    if (dbTokenFails > 0) {
+        return;
+    }
+
+    dbTokenFails++;
+    var Request = require("sdk/request").Request;
+    Request({
+        url: 'http://' + dreamboxHost + '/web/session',
         onComplete: function (response) {
-            //console.error(response);
-        }*/
-    }).get();
+            if (response.status != 200) {
+                return;
+            }
+            var nStart = response.text.indexOf('<e2sessionid>') + 13;
+            var nEnd   = response.text.indexOf('</e2sessionid>');
+            if (nStart == -1 || nEnd == -1) {
+                console.error('Could not parse token XML');
+                return;
+            }
+
+            dbTokenFails = 0;
+            dbToken = response.text.substring(nStart, nEnd);
+            console.error('Aquired access token: ' + dbToken);
+            replayFunc();
+        }
+    }).post();
 }