Youtube SpaceBar Fix? (Stop & Start videos)

I use Safari to watch YouTube videos, and for the longest while now, spacebar has become complete dysfunctional. In the past, it worked flawlessly to start and pause YouTube videos, but now it mostly doesn't work at all because YouTube changed something. Now it requires the "focus" to be on the right place in order for the spacebar to work, and it is a pain! Google are a band of idiots for having made whatever change they made to cause this.

I've been a user of Keyboard Maestro since version 8 and have upgraded to every new version since.

Can someone please tell me how to create a Macro that knows I have Youtube open in a Safari window and then uses spacebar to stop and start videos, regardless of whether I am viewing full screen or not? I cannot allow the spacebar to NEVER be used because I often post comments under videos. So at those times, the spacebar needs to work normally.

Could be what I ask is impossible, but I sure hope it is not. I really need spacebar to work properly again!

Thanks.

Did you know the "k" button also stop and starts videos?

Yes I am. Well aware. It's no substitute for the easier to press and muscle-memory powered SPACEBAR.

Are you aware that the K key sometimes doesn't work to stop/start the videos either? That's right. It too sometimes fails to do its job. And while this is a HUGE GOOGLE FAIL, the fact is I own a licensed copy of Keyboard Maestro, and I'd love to hear how to put it to good use restoring the SPACEBAR to its rightful place again when it comes to reliably and consistently stopping and starting YouTube videos in Safari on my Mac. If you have thoughts on that, I would love to hear them.

Thanks.

When I tried the "k" key, it paused the video regardless of what item (that I tested) was in focus. Maybe if you get more specific and tell us exactly which focus items to try, we might be able to replicate your problem. When I change the focus, the "k" key always works (pauses/plays the video) even though the space key does not (when the wrong item is in focus.) Your description was not specific enough for me to replicate your problem.

In any case, what you want is for keys to mean different things (eg, you want a "space" key to either pause a video or enter text in a box) depending upon which elements of a complex page are "selected." KM can probably solve this, but only by understanding the elements of the page. That probably means Javascript inquiries, and I am not your person to work with javascript. There are KM actions that can be used to interact with the Javascript on a page. but I don't know much about them.

However there may be another (different) solution. KM v11 has a new Button action feature which allows you to click on "buttons" in a page using a powerful new syntax. I tried it, and it seems to work for me, but remember, I couldn't replicate your problem so maybe it won't solve your problem. Here's what it looks like.

image

This action seems to work for me. It pauses/plays YouTube videos regardless of which button (that I tested) is in focus.

Google and Youtube change things all the time. It's a cat and mouse fight for automators. Google has no obligation to keep things consistent. Google might be making changes to fight the existence of ad blockers. If that's what they are doing, it's a moral thing for them to be doing.

1 Like

Thank you. I recreated the macro you made. It is an improvement in that it works a bit more reliably than the spacebar now. Of course, it doesn't work perfectly all the time because, like I said, the 'k" key doesn't reliably work all the time. It's hard to get the "k" key problem to be consistent, but try this:

  1. Open a YouTube video full screen.
  2. Play the video, not an Ad, by any means you like.
  3. Click the CC button to show Closed Captions.
  4. Press the "k" key (or spacebar via your Macro) once, and it might work, but press it a second time, and it won't work.

I tried to repeat the problem with the above steps, but it fails as many times as it succeeds. It's very frustrating.

Regardless of any moral reasons behind Google's move, it is frustrating more people than just me. I Googled the problem and found many people trying to solve the problem.

The problem is not exclusive to the Mac platform, but I am using an M1 Max 16" MacBook Pro running MacOS Sonoma (latest version) and Safari 17.1.

Anyway, thank you for the Macro tip. It's perhaps the best Keyboard Maestro can do, and I am happy about the improvement.

I tried these 4 steps several times with the space bar, the k key, and my macro using the "end button," and my macro using the "k" key, and never once did it fail to pause or restart the video.

So, that suggests that maybe there's something else at play here. Maybe you have some installed utility (like a plug in for Safari, or a keyboard utility) that is interfering. If you want to test this theory, you could uninstall any possible interfering programs.

I wonder if you have any ad blockers or similar extensions installed. I do and I constantly have problems with Youtube that only occur sometimes, including the same problem with the space bar and "K" key that you have.

I don't think that's something KM can fix.

I have a macro, which is actually an Apple Script shared by a member of this community. The macro allow me to play/pause YouTube video even if the window is not in focus. This is useful for me when I am learning Xcode. I will have Xcode in focus to type codes while another window, in another monitor, is playing YouTube of the instructor giving his lecture. I can press the macro to pause the video while I continue to key in the codes into Xcode. Very useful for learning.

I even have a Stream Deck button dedicated to play/pause the video.

The only caveat - it works in Chrome, not Safari. But, for my purpose, it works very well.

to clickClassName(theClassName, elementnum)
	tell application "Google Chrome" to (tabs of window 1 whose URL contains "youtube")
	set youtubeTabs to item 1 of the result
	tell application "Google Chrome"
		execute youtubeTabs javascript "document.getElementsByClassName('" & theClassName & "')[" & elementnum & "].click();"
	end tell
end clickClassName

clickClassName("ytp-play-button ytp-button", 0)

image

Very interesting. Is there a reason it won't work in Safari even if you swap "Google Chrome" for "Safari" in your script?

I don't know the answer to that, sorry.

Sadly, I changed it from Google Chrome to Safari, but in Safari, pressing spacebar fails with a "script error" I cannot fully read because the error notification cannot be expanded...

:cry:

If you open the Engine.log file, you can read the full error. Use Help > Open Logs Folder to get there.

-rob.

1 Like

Thanks. Here's what it says...

2023-11-27 10:18:54 Execute macro “Stop/Start YouTube” from trigger The Hot Key Space is pressed
2023-11-27 10:18:54 Action 1014 failed: Execute an AppleScript failed with script error: text-script:200:211: script error: Expected end of line but found identifier. (-2741)
2023-11-27 10:18:54 Execute an AppleScript failed with script error: text-script:200:211: script error: Expected end of line but found identifier. (-2741). Macro “Stop/Start YouTube” cancelled (while executing Execute AppleScript).

In Chrome, I had to turn on developer mode to allow Apple Script to control Chrome. I believe Safari had similar parameter. You can try that although I think I’ve tried it before and failed. I ended up just using Chrome to save time and pain.