I’m working on a Chrome extension that automatically clicks the “Skip Ad” button on YouTube videos to save us from watching those pesky ads.
I’ve got a content script that tries to find the skip button using several selectors and then simulates a user click by firing mouse events and calling .click(). It also mutes the video while ads play. The script runs with a MutationObserver on the player and a fallback interval every few seconds.
Here’s a snippet of my main click function:
function tryClickSkip() {
if (!cachedSkipButton || !document.contains(cachedSkipButton)) {
cachedSkipButton = findSkipButton();
}
if (cachedSkipButton && typeof cachedSkipButton.click === 'function') {
cachedSkipButton.dispatchEvent(new MouseEvent('mouseover', { bubbles: true }));
cachedSkipButton.dispatchEvent(new MouseEvent('mousedown', { bubbles: true }));
cachedSkipButton.dispatchEvent(new MouseEvent('mouseup', { bubbles: true }));
cachedSkipButton.click();
console.log('Clicked skip ad button!');
}
}
The problem: the console logs confirm the click happened, but the ad doesn’t actually skip. It seems like YouTube might require some kind of user gesture or additional interaction that I’m missing.
Has anyone successfully automated clicking YouTube’s skip ad button? Am I missing a trick to simulate the user click better, or is there a better approach altogether?
Thanks in advance for any tips or pointers!