librelist archives

« back to archive

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

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

From:
Joren Van Onder
Date:
2013-12-22 @ 07:25
Allows users to change playback speed with 'C-c C-[' and 'C-c C-]'.
---
Improvements over v2:
  - Cache length in for loop condition
  - Give display_error_message a more specific name

 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..f47ae53 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 youtube_player_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, n = settings_control_rows.length; i < n && ! 
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) {
+            youtube_player_display_error_message(I, "No speed buttons found");
+            return;
+        }
+
+        var current_speed_found = false;
+        for (var i = 0, n = speed_buttons.length; i < n; ++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) {
+            youtube_player_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");
+            youtube_player_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 v3] youtube-player.js: add support for changing playback speed

From:
John J. Foerch
Date:
2013-12-22 @ 20:16
On Sun, Dec 22, 2013 at 08:25:29AM +0100, Joren Van Onder wrote:
> Allows users to change playback speed with 'C-c C-[' and 'C-c C-]'.
> ---
> Improvements over v2:
>   - Cache length in for loop condition
>   - Give display_error_message a more specific name
> 

Looking good; I'll merge it shortly.  I am curious, was there a reason for
the particular key bindings 'C-[' and 'C-]'?

-- 
John Foerch

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

From:
Joren Van Onder
Date:
2013-12-22 @ 22:14
On Sun, Dec 22, 2013 at 09:16, jjfoerch@earthlink.net wrote:
> I am curious, was there a reason for the particular key bindings 'C-['
> and 'C-]'?

I chose them mostly because of MPlayer, they use '[' and ']' to change
playback speed. VLC also has default bindings on those keys to alter
playback speed I believe.

And then I decided to use 'C-c C-[' instead of just 'C-c [' to keep it
consistent with the already existing bindings 'C-c C-m' and 'C-c C-f'.

'C-c return' kind of sticks out now though. It might be a good idea to
make all of them consistent. So we could either change 'C-c return' to
'C-c C-return' or we could change all of them to 'C-c [key]'. I don't
really have any strong opinion on which option is better. At the moment
I'm leaning towards 'C-c C-[key]' because this has the advantage of
being easily repeatable by the user. He can hold down the control
modifier while pressing 'c [key]' multiple times. This could be useful
when changing the playback speed of a video.

--
        Joren

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

From:
John J. Foerch
Date:
2013-12-22 @ 22:36
On Sun, Dec 22, 2013 at 11:14:24PM +0100, Joren Van Onder wrote:
> 
> On Sun, Dec 22, 2013 at 09:16, jjfoerch@earthlink.net wrote:
> > I am curious, was there a reason for the particular key bindings 'C-['
> > and 'C-]'?
> 
> I chose them mostly because of MPlayer, they use '[' and ']' to change
> playback speed. VLC also has default bindings on those keys to alter
> playback speed I believe.
> 
> And then I decided to use 'C-c C-[' instead of just 'C-c [' to keep it
> consistent with the already existing bindings 'C-c C-m' and 'C-c C-f'.
> 
> 'C-c return' kind of sticks out now though. It might be a good idea to
> make all of them consistent. So we could either change 'C-c return' to
> 'C-c C-return' or we could change all of them to 'C-c [key]'. I don't
> really have any strong opinion on which option is better. At the moment
> I'm leaning towards 'C-c C-[key]' because this has the advantage of
> being easily repeatable by the user. He can hold down the control
> modifier while pressing 'c [key]' multiple times. This could be useful
> when changing the playback speed of a video.
> 

Thanks.  I like the keys you've chosen.  'C-c [key]' is unavailable for
any alphanumeric key because those bindings are reserved for users.  Using
the control modifier is good because it's consistent with various Emacs
packages that have a lot of special keys — slime comes to mind.  I have no
personal problem with 'C-c return' being the single exception to the
pattern.  Return is such a prominent key, I very much doubt that it would
a problem for anybody.

-- 
John Foerch

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

From:
John J. Foerch
Date:
2013-12-23 @ 02:23
On Sun, Dec 22, 2013 at 08:25:29AM +0100, Joren Van Onder wrote:
> Allows users to change playback speed with 'C-c C-[' and 'C-c C-]'.
> ---

Pushed.

-- 
John Foerch