script for youtube video on anybrowser

hi, i have 2 screens and often watching tutorial on youtube with the second screen,
the good thing with the key f8 i can pause/play the video that run even when im on xcode for example
unfortunately i was hopping that f7 and f9 can bring me the back forward 5sec or move forward 5sec functionality of yt, but nothing.
is it possible to create a script to do that ?
this can save a lot of time avoiding going back to the browser each time i want to move backward in the video

hi @Andreas_Hegenberg do you think you can help me with that ?

im trying to learn apple script since yesterday, but im block, i tried this for chrome or brave to fastforward 10sec but its not working .

Blockquote

tell application "Google Chrome"
    repeat with t in tabs of windows
        tell t
            if URL starts with "http://www.youtube.com/watch" or URL starts with "https://www.youtube.com/watch" then
                execute javascript "
            var player = document.querySelector('video');
            player.seekTo(player.getCurrentPosition() + 10000);
            "
                exit repeat
            end if
        end tell
    end repeat
end tell

Your problem is not AppleScript, but JavaScript. The (YouTube-) player has no function "seekTo" and "getCurrentPosition".

Try this to fast forward by 10 seconds:

tell application "Google Chrome"
    repeat with t in tabs of windows
        tell t
            if URL starts with "http://www.youtube.com/watch" or URL starts with "https://www.youtube.com/watch" then
                execute javascript "
            var player = document.querySelector('video');
            player.currentTime = player.currentTime + 10;
            "
                exit repeat
            end if
        end tell
    end repeat
end tell

ohh god , its working, thanks you very much you are the best

if i want to run this code on different app. do you know if it possible with apple script to do a tell application on multiple app like for example :
tell application "Google Chrome", "brave", "safari" ?

No. You have to write a statement for each app. The JavaScript Code (e.g. the player object) may be also different.

ok thanks Dirk :relaxed:

What you can do is to use a loop:

set appList to {"Google Chrome", "Safari", "Brave"}
repeat with theApp in appList
	tell application theApp
		-- your code
	end tell
end repeat

But that doesn't make sense from my point of view, because the tab handling in the browsers is too different. But you can try.

no its not working :smiling_face_with_tear:,and previously i wrote a statement for each browser, but only chrome is working, i guess its because of what you said "the tab handling". and i dont know how to make the change...

Here is the script for Safari:

Note: "You must enable the 'Allow JavaScript from Apple Events' option in Safari's Develop menu to use 'do JavaScript'."

tell application "Safari"
    repeat with t in tabs of windows
        tell t
            if URL starts with "http://www.youtube.com/watch" or URL starts with "https://www.youtube.com/watch" then
                do JavaScript "
            var player = document.querySelector('video');
            player.currentTime = player.currentTime + 10;
            "
                exit repeat
            end if
        end tell
    end repeat
end tell

I can't help you with the Brave browser as I don't have it installed, but you can try to add it to the Script Editors Dictionary (under windows) to see if it is scriptable and if so, what the commands are. See: change browser in script to work with brave browser instead of safari - #2 by Caliguvara

thanks very much Dirk ill try today :relaxed:

Hi @Dirk and @Emm_Gr , I found this thread since I was looking for how to do basically the same thing (just would like to trigger it with a custom shortcut).

I'm wondering if there's a simpler approach though... I was initially trying to make this work with a chrome extension and stumbled upon the navigator.mediaSession API - it seems like you could just try to dispatch whichever event e.g. navigator.mediaSession.setActionHandler("seekforward" listens to no? I'm trying to test this out, but don't know what this event would be or how to trigger it... In Karabiner if I dispatch "consumer_key_code": "fast_forward" it triggers navigator.mediaSession.setActionHandler("nexttrack"

Any ideas?

Thanks!

Hi,
I have no idea what you have in mind. Also I don't know the navigator.mediaSession API, because I don't use Chrome.
If you just want to fast forward, then you can currently use the following script until Google changes the web page again.

tell application "Google Chrome"
	repeat with t in tabs of windows
		tell t
			if URL starts with "http://www.youtube.com/watch" or URL starts with "https://www.youtube.com/watch" then
				execute javascript "document.getElementsByClassName('ytp-next-button ytp-button')[0].click()"
				exit repeat
			end if
		end tell
	end repeat
end tell
1 Like

Hi, awesome thanks for the script

what I was thinking related to the other approach:
navigator.mediaSession.setActionHandler("seekforward" is meant to listen to some system-wide event - not sure what terminology to use since I'm not too experienced in this area, but I assume this is whatever kind of event that developers would emit if they wanted to control media playback system-wide, and also the same kind emitted when you click the buttons in the picture at the top of this thread?

it's just the 3 buttons in the pictures will emit something like "previoustrack", "play/pause", and "nexttrack", while what I really want to is "seekbackward" or "seekforward"

so basically, skip Applescript/js injection (or writing a chrome extension) and just emit this kind of system-wide event and let YouTube do what it's already set up for

@Dirk any thoughts on this? Do you know if there is an analogous event for the system-wide "previous_track" / "next_track" events, but for "seek_backward" and "seek_forward?, and if so can I send those with BTT?

Thanks in advance

Hello,

I'd love to find out how do I use this script by Emma_Gr?

Where do I put this script or how do I use it?

I know it is an old topic but thought to give it a chance and see if someone can help me out.

Thanks.