Safari YouTube timestamp extension

This script adds timestamp functionality when using YouTube in Safari.

IMPORTANT: To use this you will have to turn on Allow JavaScript from Apple Events in the Developer menu in Safari. This allows any Apple Events (e.g. AppleScripts) running on your computer to inject arbitrary scripts in Safari, which is a potential security vulnerability. For example, software you run could access and control your web-browsing — do you trust the software you have installed?

Get Address

tell application "Safari"
	set d to first item of documents
	set u to URL of d
	
	if text 1 thru 32 of u is "https://www.youtube.com/watch?v=" then
		-- Remove any timestamp in URL
		set AppleScript's text item delimiters to "&t="
		set theItems to every text item of u
		set AppleScript's text item delimiters to ""
		set u to the first item of theItems
		
		set thePage to the front document
		set theScript to "ytplayer = document.getElementById('movie_player'); ytplayer.getCurrentTime();"
		set theTime to (do JavaScript theScript in thePage) as integer
		return u & "&t=" & theTime
	else
		return u
	end if
	
end tell
1 Like

Bug fix

Replace:

if text 1 thru 32 of u is "https://www.youtube.com/watch?v=" then

With:

if u starts with "https://www.youtube.com/watch?v=" then