Stop video 12 seconds before the end for Secular Talk videos to avoid the ad at the end

Stop video 12 seconds before the end for Secular Talk videos on YouTube

// ==UserScript==
// @name         Stop video 12 seconds before the end for Secular Talk videos to avoid the ad at the end
// @namespace    http://tampermonkey.net/
// @version      0.3
// @description  Stop video 12 seconds before the end for Secular Talk videos on YouTube
// @author       Your name
// @match        *://www.youtube.com/*
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // define the username of the channel you want to target
    const targetChannel = 'Secular Talk';
    const targetVideoDuration = 12; // Stop the video 12 seconds before the end

    console.log('Script loaded');

    // check if the video is from the target channel
    function isTargetVideo() {
        const channelNameElement = document.querySelector('.ytd-channel-name a');
        if (channelNameElement) {
            const channelName = channelNameElement.textContent.trim();
            return channelName === targetChannel;
        }
        return false;
    }

    let video = null;
    let videoDuration = 0;

    function init() {
        setInterval(checkVideo, 500);
    }

    function checkVideo() {
        video = document.querySelector('video.html5-main-video');
        if (video) {
            if (!video.paused && isTargetVideo()) {
                videoDuration = video.duration;
                if (videoDuration - video.currentTime <= targetVideoDuration) {
                    console.log('Stopping video');
                    video.pause();
                }
            }
        }
    }

    init();
})();