Control Videos Without Switching To The Browser

TL;DR

I got tired of having to switch to my browser every time I want rewind a few seconds in a video tutorial. I wrote these scripts so I don't have to. They control videos via hotkeys regardless of what app I'm using.

Play/Pause

Application('Google Chrome').windows[0].activeTab.execute(
    {
        javascript:`
          if (document.getElementsByClassName('html5-main-video')[0]) {
            const thePlayer = document.getElementsByClassName('html5-main-video')[0]
            if(thePlayer.paused === true) {
              thePlayer.play()
            }
            else {
              thePlayer.pause()
            }
          }
        `
    }
)

Skip Forward

Application('Google Chrome').windows[0].activeTab.execute(
    {
        javascript:`
          if (document.getElementsByClassName('html5-main-video')[0]) {
            const thePlayer = document.getElementsByClassName('html5-main-video')[0]
            thePlayer.currentTime = thePlayer.currentTime + 12
            if(thePlayer.paused === true) {
              thePlayer.play()
            }
          }
        `
    }
)

Skip Backward

Application('Google Chrome').windows[0].activeTab.execute(
    {
        javascript:`
          if (document.getElementsByClassName('html5-main-video')[0]) {
            const thePlayer = document.getElementsByClassName('html5-main-video')[0]
            thePlayer.currentTime = thePlayer.currentTime - 12
            if(thePlayer.paused === true) {
              thePlayer.play()
            }
          }
        `
    }
)

Overview

Learning is hard. It doesn't take much to make it harder.

Having to switch back and forth between the browser and whatever app I'm using during video tutorials is tough for me. The constant micro-interruptions are unpleasant and draining. They make it harder to stay connected to the material. So much so that I generally avoid tutorials.

This problem is a good one though, because it's one we can fix. The scripts above can be used to play/pause, fast-forward, and rewind a video without having to switch to the browser. For example, I have hotkeys setup on my keyboard so I can skip backwards and forwards while staying in the app that I'm actually trying to learn.

Usage

  • These scripts are designed to be used as keyboard shortcuts. You'll need an app to set those up. I use Alfred, but any app that lets you assign global hotkeys to scripts will work. (I tried the Shortcuts and Automator apps the come pre-installed on macs but I couldn't get them to work consistently.)

  • The scripts control: a YouTube video : in the front tab : of Google Chrome : on a mac. Getting the functionality to work on different video platforms, tabs, browsers, or operating systems is completely possible but left as an exercise for the reader.

  • My Alfred setup is to create a "Workflow" for each one of the scripts. Inside each workflow I add a "Hotkey" to assign whatever keyboard shortcut I want. Then, I add a "Run Script" action. It gets set to Language: /use/bin/osascript (JavaScript) with the code is pasted in. After connecting the two actions everything is ready to go.

An Alfred app workflow showing hotkey and run script actions for a workflow named YouTube Play Pause In Chrome An Alfred app Run Script action dialog box showing the JavaScript For Automation code that runs the play/pause process

Security Settings

There are two security items necessary to get things working. Both things are directly available without having to do anything hacky. That said, they are security related. You'll have to make your own decision about them.

  1. In Chrome, click "View" in the top menubar. Then, select "Developer" and click on "Allow JavaScript From Apple Events".

  2. The first time you run one of the processes you'll get a security dialog asking you to give permission for the app your using (e.g. Alfred) to control Google Chrome. Clicking "OK" allows the process to continue, otherwise it stops without affecting the video.

Outro

It took the better part of two days to figure out those scripts. I condier it time well spent. If you'd like to see some behind the scenees stuff, I posted my in-progress notes, drafts, and links, here.

I suspect I'm more sensitive to the drain having to flip back and forth to the browser is than most folks because on the heavy cocktail of antipsychotics I'm on. But, I expect not having to do it will be a nice thing for everyone.

Enjoy,

-a