Skip to content
This repository was archived by the owner on Dec 24, 2024. It is now read-only.

fixed potential infinite loop in popupRemover function..#542

Open
qw87rt wants to merge 1 commit intoTheRealJoelmatic:mainfrom
qw87rt:patch-3
Open

fixed potential infinite loop in popupRemover function..#542
qw87rt wants to merge 1 commit intoTheRealJoelmatic:mainfrom
qw87rt:patch-3

Conversation

@qw87rt
Copy link
Copy Markdown
Contributor

@qw87rt qw87rt commented Jun 8, 2024

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
Copy link
Copy Markdown
Contributor Author

qw87rt commented Jun 8, 2024

//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();
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant