The goal

Bookmark a YouTube preroll ad with as few mouseclicks and keystrokes as possible.

But why?

I like to have the option of rewatching or referencing an interesting or funny ad at a later date. Most ads are unlisted videos, which makes them nigh impossible to look up.

My previous workflow (12 steps):

  1. Right-click on the video player, select “Copy debug info”
  2. Alt+Tab to a text editor
  3. Ctrl+V the debug info into text editor
  4. Ctrl+F for “addocid”
  5. Ctrl+C the advertisement video id
  6. Alt+Tab back to browser
  7. Ctrl+N to open new browser window
  8. Type “youtu.be/
  9. Ctrl+V the video id
  10. Wait a fraction of a second for the URL to redirect from youtu.be/[video_id] to https://www.youtube.com/watch?v=[video_id]&feature=youtu.be to https://www.youtube.com/watch?v=[video_id]
  11. Ctrl+D to bookmark the video
  12. Ctrl+W to close the browser window

My new workflow (4 steps):

  1. Right-click on the video player, select “Copy debug info”
  2. Press F9 to run the script
  3. Check that the bookmark is saved to the correct folder
  4. Ctrl+W to close the browser tab

The AHK script:

#Requires AutoHotkey v2.0

F9:: ; Press F9, then a thing happens
{
	ActiveHwnd := WinExist("A")			; Save active window ID to variable
	Haystack := A_Clipboard				; Save clipboard to variable
	NeedleRegEx := 's).*"addocid": "(.*?)",.*'	; Regex witchcraft
	
	; Regex breakdown:
		; Example line: "addocid": "kU5Y-LRSteA",
		; We need to use "s)" otherwise it will treat each line in the clipboard as a separate string.
		; We need to flank the line we're searching for with ".*", otherwise it will search each line
		; We need to use the non-greedy "?" option when capturing the video ID "(.*?)" otherwise it will capture the rest of the file
	
	VidID := RegExReplace(Haystack, NeedleRegEx, "$1")		; Do the actual regex replacement
	URL := "https://www.youtube.com/watch?v=" . VidID		; Concatenate the video ID with the rest of a YouTube URL
	
	Run URL			; Open the video in the new tab
	Sleep 1000		; Wait for the tab to open
	WinActivate ActiveHwnd	; Reactivate the window
	Sleep 1000		; Wait again?
	Send "^d"		; Open bookmark dialogue and create bookmark
}

Potential improvements

  1. Automate clicking on “Copy debug info”: I figured out how to use AHK to open the right-click menu in the YouTube player, but I can’t figure out a reliable way to locate and click on “Copy debug info”. It seems like it is in a different place depending on whether it is the 1st or 2nd preroll ad.
  2. Save bookmark in specific folder, rather than just the default bookmark dialogue box. No idea how to do this elegantly.

This is my first AHK project, so I’ve probably done several things incorrectly or inefficiently. Any feedback would be welcome :)

I’m so happy to have found an AHK community on Lemmy, however small!

  • threelonmusketeers@sh.itjust.worksOP
    link
    fedilink
    English
    arrow-up
    1
    ·
    5 months ago

    Thanks! I’m still very new to AHK, so I probably did things wrong without knowing it. It’s been an interesting learning experience so far though, and I’d love to get some feedback and learn more. Thanks for the warm welcome!