librelist archives

« back to archive

[PATCH v2] youtube-player.js: add support for changing playback speed

[PATCH v2] youtube-player.js: add support for changing playback speed

From:
Joren Van Onder
Date:
2013-12-21 @ 08:32
Allows users to change playback speed with 'C-c C-[' and 'C-c C-]'.
---
Improved version of the previous one [1]. Made it so it works on all
HTML5 Youtube pages. Also improved the error reporting.

[1]: 
http://librelist.com/browser//conkeror/2013/12/18/patch-1-2-youtube-player-js-update-play-pause-and-fullscreen/#cf2ed248acbeceb35d4545e677b7b7e4

 modules/page-modes/youtube-player.js | 60 +++++++++++++++++++++++++++++++++++-
 1 file changed, 59 insertions(+), 1 deletion(-)

diff --git a/modules/page-modes/youtube-player.js 
b/modules/page-modes/youtube-player.js
index 10ac68e..997d191 100644
--- a/modules/page-modes/youtube-player.js
+++ b/modules/page-modes/youtube-player.js
@@ -1,5 +1,6 @@
 /**
  * (C) Copyright 2012 Scott Jaderholm
+ * (C) Copyright 2013 Joren Van Onder
  *
  * Use, modification, and distribution are subject to the terms specified in the
  * COPYING file.
@@ -9,6 +10,53 @@ require("content-buffer.js");

 define_keymap("youtube_player_keymap", $display_name = "youtube-player");

+function display_error_message (I, error_message) {
+    I.minibuffer.message(error_message + ", ensure html5 is enabled for 
youtube by visiting http://youtube.com/html5");
+}
+
+function youtube_player_change_speed (slower) {
+    return function (I) {
+        var buf = I.buffer;
+
+        // get settings control rows
+        var settings_control_rows = 
buf.document.querySelectorAll(".ytp-menu-row");
+
+        var speed_buttons = null;
+        // find speed buttons
+        for (var i = 0; i < settings_control_rows.length && ! 
speed_buttons; ++i) {
+            if (settings_control_rows[i].innerHTML.indexOf("Speed") != -1) {
+                speed_buttons = settings_control_rows[i].querySelectorAll(
+                    ".ytp-drop-down-menu-content .ytp-drop-down-menu-button");
+            }
+        }
+
+        if (! speed_buttons) {
+            display_error_message(I, "No speed buttons found");
+            return;
+        }
+
+        var current_speed_found = false;
+        for (var i = 0; i < speed_buttons.length; ++i) {
+            if 
(speed_buttons[i].className.indexOf("ytp-drop-down-menu-button-checked") 
!= -1) {
+                // current speed
+                current_speed_found = true;
+
+                if (slower && i != 0) {
+                    dom_node_click(speed_buttons[i - 1], 1, 1);
+                } else if (! slower && i != speed_buttons.length - 1) {
+                    dom_node_click(speed_buttons[i + 1], 1, 1);
+                }
+
+                break;
+            }
+        }
+
+        if (! current_speed_found) {
+            display_error_message(I, "Current speed button not found");
+        }
+    }
+}
+
 function youtube_player_click_command (selector, error_message) {
     return function (I) {
         var buf = I.buffer;
@@ -16,7 +64,7 @@ function youtube_player_click_command (selector, 
error_message) {
         if (elem)
             dom_node_click(elem, 1, 1);
         else
-            I.minibuffer.message(error_message + ", ensure html5 is 
enabled for youtube by visiting http://youtube.com/html5");
+            display_error_message(I, error_message);
     };
 }

@@ -35,9 +83,19 @@ interactive("youtube-player-fullscreen",
     youtube_player_click_command(".ytp-button-fullscreen-enter, 
.ytp-button-fullscreen-exit",
                                  "No fullscreen button found"));

+interactive("youtube-player-speed-down",
+    "Slow down the Youtube html5 player.",
+    youtube_player_change_speed(true));
+
+interactive("youtube-player-speed-up",
+    "Speed up the Youtube html5 player.",
+    youtube_player_change_speed(false));
+
 define_key(youtube_player_keymap, "C-c return", "youtube-player-play-or-pause");
 define_key(youtube_player_keymap, "C-c C-m", "youtube-player-mute");
 define_key(youtube_player_keymap, "C-c C-f", "youtube-player-fullscreen");
+define_key(youtube_player_keymap, "C-c C-[", "youtube-player-speed-down");
+define_key(youtube_player_keymap, "C-c C-]", "youtube-player-speed-up");

 define_keymaps_page_mode("youtube-player-mode",
     build_url_regexp($domain = "youtube", $allow_www = true),
--
1.8.5.2

Re: [conkeror] [PATCH v2] youtube-player.js: add support for changing playback speed

From:
John J. Foerch
Date:
2013-12-21 @ 23:50
On Sat, Dec 21, 2013 at 09:32:13AM +0100, Joren Van Onder wrote:
> +function display_error_message (I, error_message) {
> +    I.minibuffer.message(error_message + ", ensure html5 is enabled for
youtube by visiting http://youtube.com/html5");
> +}

The procedure display_error_message should not have such a general name.

-- 
John Foerch

Re: [conkeror] [PATCH v2] youtube-player.js: add support for changing playback speed

From:
John J. Foerch
Date:
2013-12-21 @ 21:31
On Sat, Dec 21, 2013 at 09:32:13AM +0100, Joren Van Onder wrote:
> +        for (var i = 0; i < settings_control_rows.length && ! 
speed_buttons; ++i) {
...
> +        for (var i = 0; i < speed_buttons.length; ++i) {

Pro Tip:

  This looping construct runs slowly in javascript because ".length" must
be recomputed for every iteration.  This style is better:

  for (i = 0, n = some_array.length; i < n; ++i)

-- 
John Foerch