AdvertSkipper for YouTube

CGM : This is a little Tcl script to make playing videos and music on YouTube less irritating by semi-automating the process of skipping the adverts. The old music albums I have been rediscovering lately keep getting interrupted by attempts to sell me things like software to correct my English grammar for me, which I find rather insulting. No true stingy Scotsman would pay for an ad-free subscription if he can devise a free alternative, so the following script is my solution.

When it's running and an advert starts which you are not interested in (probably most of them), you just press "+" on the PC's numeric pad. This will mute the sound for 5 seconds until it's possible to click the "Skip Ads" option. The script is not clever enough to find "Skip Ads" on the screen by itself, so for the first time you need to click this manually. It then records that location and will click in the same place automatically on subsequent runs.

The script is Windows-specific and relies on TWAPI to setup hotkeys and send simulated mouse and keyboard input to the web browser. If you don't already have a suitable Tcl & TWAPI installation, one of the all-in-one binaries at APN's https://sourceforge.net/projects/twapi/files/Tcl%20binaries/Tclkits%20with%20TWAPI/ will provide everything needed.


# AdvertSkipper - mute and skip unwanted YouTube adverts.

package require twapi

##### Define screen layout #####

wm title . AdvertSkipper
wm protocol . WM_DELETE_WINDOW quit

set message {\
When an advert starts that you are not interested in,\
press "+" on the numeric pad - this will mute\
the sound for 5 seconds until the advert can be skipped. Then for\
the first time you will need to click "Skip Ads" manually,\
but this location will be remembered and for later times\
the click will be done automatically.  If you need to adjust\
the click location, press "-" to select the click location again.

Other keys: "*" will mute/unmute the sound, "Break" will exit the program.\
}

message .msg -text $message
pack .msg -side top -fill both -expand 1

font create big -family Arial -size 20
ttk::label .clickmsg -font big -foreground red -text "Please click 'Skip Ads'"

##### Set up hotkeys #####

foreach {key action} {
        ADD skip
        SUBTRACT {unset -nocomplain ::mouse_xy; skip}
        MULTIPLY mute
        BREAK quit
} {
        lappend hotkey_ids [twapi::register_hotkey $key $action]
}

##### Define actions #####

proc quit {} {
        grab release .
        foreach id $::hotkey_ids {twapi::unregister_hotkey $id}
        exit
}

bind . <ButtonPress> {set ::mouse_xy "%X %Y"}

proc skip {} {
        twapi::send_keys "{VOLUME_MUTE}"

        # If we don't know where to click, ask the user
        if {! [info exists ::mouse_xy]} {
                pack .clickmsg -fill both -expand 1 -ipadx 20 -ipady 20
                wm deiconify .
                focus -force .
                grab -global .
        }

        wait 5500

        # Wait for the click if it's not already done
        if {! [info exists ::mouse_xy]} {
                vwait ::mouse_xy
        }
        grab release .
        pack forget .clickmsg

        twapi::move_mouse {*}$::mouse_xy
        twapi::click_mouse_button left
        wait 200
        twapi::send_keys "{VOLUME_MUTE}"
}

proc mute {} {
        twapi::send_keys "{VOLUME_MUTE}"
}

# delay while keeping event loop active
proc wait ms {
        after $ms {incr ::wait_done}
        vwait ::wait_done
}

vwait forever