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):
- Right-click on the video player, select "Copy debug info"
- Alt+Tab to a text editor
- Ctrl+V the debug info into text editor
- Ctrl+F for "addocid"
- Ctrl+C the advertisement video id
- Alt+Tab back to browser
- Ctrl+N to open new browser window
- Type "
youtu.be/
" - Ctrl+V the video id
- Wait a fraction of a second for the URL to redirect from
youtu.be/[video_id]
tohttps://www.youtube.com/watch?v=[video_id]&feature=youtu.be
tohttps://www.youtube.com/watch?v=[video_id]
- Ctrl+D to bookmark the video
- Ctrl+W to close the browser window
My new workflow (4 steps):
- Right-click on the video player, select "Copy debug info"
- Press F9 to run the script
- Check that the bookmark is saved to the correct folder
- 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
- 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.
- 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!
Wow! This is pretty intense. Thanks for sharing and welcome to the community
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!