Skip to content

Commit

Permalink
don't fast-forwards ads because it trigger the anti-ad-block modal, i…
Browse files Browse the repository at this point in the history
…nstead mute them and click skip as soon as clickable
  • Loading branch information
tomsaleeba committed Jul 1, 2024
1 parent 9478dc5 commit 57a6d34
Showing 1 changed file with 43 additions and 8 deletions.
51 changes: 43 additions & 8 deletions tampermonkey-script.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// ==UserScript==
// @name YouTube custom speeds
// @namespace https://github.com/tomsaleeba
// @version 0.9
// @version 0.91
// @description Adds a div to the YouTube player page with custom speed controls
// @author Tom Saleeba
// @match https://www.youtube.com/*
Expand All @@ -11,10 +11,12 @@

const maxFastnessAnchorId = 'max-fastness'
const mainLoopInterval = 1000
// FIXME should I be using session storage? Is that per-tab?
const lsKeyPrefix = 'techotom.yt.'
const lsKeyUserSpeed = `${lsKeyPrefix}user-speed`
const lsKeyIsAdFF = `${lsKeyPrefix}is-ad-ff`
let panner = null
const isTrace = false

function resetBoldness(className) {
const speedSelectors = document.getElementsByClassName(className)
Expand All @@ -29,6 +31,13 @@ function log(msg) {
unsafeWindow.console.debug(`[${logPrefix}] ${msg}`)
}

function trace(msg) {
if (!isTrace) {
return
}
log(` [TRACE] ${msg}`)
}

function setPlayerSpeed(newSpeed) {
document.getElementsByClassName('html5-main-video')[0].playbackRate = newSpeed
}
Expand Down Expand Up @@ -252,22 +261,44 @@ function clickBtnIfVisibleQS(querySelector, niceName) {
}

function autoFastForwardAds() {
const speedAnchor = document.getElementById(maxFastnessAnchorId)
if (!speedAnchor) {
trace('no speed anchor')
// don't control the player when the human can't control us
return
}
const classForOnlyVideoAds = 'ad-showing'
const [adContainer] = document.getElementsByClassName(classForOnlyVideoAds)
const isAdHidden = !adContainer || adContainer.offsetParent === null
const speedAnchor = document.getElementById(maxFastnessAnchorId)
if (isAdHidden || !speedAnchor) {
if (isAdHidden) {
trace('ad *is* hidden')
useSavedPlaybackSpeed()
assertMuteState(false)
return
}
log('ad is playing, time to fast forward!')
localStorage.setItem(lsKeyIsAdFF, true)
speedAnchor.click()
assertMuteState(true)
// // disabled because YouTube is detecting ad-blockers
// log('ad is playing, time to fast forward!')
// speedAnchor.click()
log('ad is playing, waiting for clickable skip button!')
clickBtnIfVisible('ytp-ad-skip-button', 'old skip button')
clickBtnIfVisible('ytp-ad-skip-button-modern', 'new skip button')
clickBtnIfVisible('ytp-ad-skip-button-modern', '2024-feb skip button')
clickBtnIfVisible('ytp-skip-ad-button', '2024-jun skip button')
clickBtnIfVisibleQS('button[id="skip-button:x"]', '2024-jun skip button (by ID)')
// FIXME disable check for ads from now on?
}

function assertMuteState(isMute) {
const muteButton = document.querySelector('.ytp-mute-button')
const currMuteState = muteButton.title.startsWith('Unmute')
trace(JSON.stringify({currMuteState, isMute}))
if (currMuteState === isMute) {
return
}
muteButton.click()
}

function cancelStupidAutoplay() {
clickBtnIfVisible(
'ytp-autonav-endscreen-upnext-cancel-button',
Expand Down Expand Up @@ -313,8 +344,12 @@ function runMainLoop() {
premiumNoThanks()
fadeAdOverlay()
}
/* const intervalThingy = */ setInterval(worker, mainLoopInterval)
// FIXME do we need to clearInterval(intervalThingy) ?
// "Ad blockers are not allowed on YouTube" modal
// document.querySelector('.ytd-enforcement-message-view-model') - the modal
// document.querySelector('yt-button-view-model[icon="COUNTDOWN_TO_CLOSE"]').click() - close button (after timer has expired)
// need to click play on the ad again too
setInterval(worker, mainLoopInterval)
// FIXME do we need to clearInterval() ?
}

waitForTargetElement((targetElement) => {
Expand Down

0 comments on commit 57a6d34

Please sign in to comment.