TheRealJoelmatic / RemoveAdblockThing

The intrusive "Ad blocker are not allowed on YouTube" message is annoying. This open-source project aims to address this issue by providing a solution to bypass YouTube's ad blocker detection
http://joelmatic.com/
MIT License
5.91k stars 319 forks source link

fixed potential infinite loop in `popupRemover` function.. #542

Open qw87rt opened 3 weeks ago

qw87rt commented 3 weeks ago

The popupRemover function runs every second & can cause performance issues if the process is slow and if therr are frequent popups. //can also be applied to removeAds function cuz not clearing them can lead to performance issues if many intervals are running simultaneously.

  1. added flag variable isPopupBeingProcessed
  2. At the start of the interval function, we check if isPopupBeingProcessed is true
  3. set isPopupBeingProcessed to true to indicate that a popup is being processed.
  4. After the popup removal process is complete, we reset isPopupBeingProcessed to false.

this should help mitigate potential performance issues.

qw87rt commented 3 weeks ago

//can also be applied to removeAds function cuz not clearing them can lead to performance issues if many intervals are running simultaneously.

function removeAds() { log("removeAds()");

// Clear any existing interval
clearInterval(removeAdsInterval);

removeAdsInterval = setInterval(() => {

        if (window.location.href !== currentUrl) {
            currentUrl = window.location.href;
            isVideoPlayerModified = false;
            clearAllPlayers();
            removePageAds();
        }

        if (isVideoPlayerModified){
            return;
        }

        log("Video replacement started!");

        //
        // remove ad audio
        //

        var video = document.querySelector('video');
        if (video) video.volume = 0;
        if (video) video.pause();
        if (video) video.remove();

        //
        // Remove the current player
        //

        if(!clearAllPlayers()){
            return;
        }

        //
        // Get the url
        //

        let videoID = '';
        const baseURL = 'https://www.youtube.com/watch?v=';
        const startIndex = currentUrl.indexOf(baseURL);

        if (startIndex !== -1) {
            // Extract the part of the URL after the base URL
            const videoIDStart = startIndex + baseURL.length;
            videoID = currentUrl.substring(videoIDStart);

            const ampersandIndex = videoID.indexOf('&');
            if (ampersandIndex !== -1) {
                videoID = videoID.substring(0, ampersandIndex);
            }

        } else {
            log("YouTube video URL not found.", "e")
            return null;
        }

        log("Video ID: " + videoID);

        //
        // Create new frame for the video
        //

        const startOfUrl = "https://www.youtube-nocookie.com/embed/";
        const endOfUrl = "?autoplay=1&modestbranding=1";
        const finalUrl = startOfUrl + videoID + endOfUrl;

        const iframe = document.createElement('iframe');

        iframe.setAttribute('src', finalUrl);
        iframe.setAttribute('frameborder', '0');
        iframe.setAttribute('allow', 'accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share');
        iframe.setAttribute('allowfullscreen', true);
        iframe.setAttribute('mozallowfullscreen', "mozallowfullscreen");
        iframe.setAttribute('msallowfullscreen', "msallowfullscreen");
        iframe.setAttribute('oallowfullscreen', "oallowfullscreen");
        iframe.setAttribute('webkitallowfullscreen', "webkitallowfullscreen");

        iframe.style.width = '100%';
        iframe.style.height = '100%';
        iframe.style.position = 'absolute';
        iframe.style.top = '0';
        iframe.style.left = '0';
        iframe.style.zIndex = '9999';
        iframe.style.pointerEvents = 'all'; 

        const videoPlayerElement = document.querySelector('.html5-video-player');
        videoPlayerElement.appendChild(iframe);
        log("Finished");

        isVideoPlayerModified = true;

}, 500);

removePageAds(); }