Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Youtube Anti Translate is slowing down Firefox" message and Firefox freezes when viewing a playlist #1

Closed
artisticfox8 opened this issue Aug 1, 2024 · 7 comments

Comments

@artisticfox8
Copy link
Owner

Steps to reproduce:

  1. Install version 1.5.14 from addons.mozilla.org

  2. Go to https://www.youtube.com/playlist?list=PLiNeEis1npnCBYNHB36-hZJ4dDLZ3dIo2
    Alternatively, to end up on the same page:

    1. Go to a channel page (such as https://www.youtube.com/@ichika_nito/featured,
    2. Choose "Playlists" (so you're on https://www.youtube.com/@ichika_nito/playlists)
    3. Click "View full playlist"
  3. The message is displayed:
    image

    Firefox uses a lot of RAM, and eventually freezes.

@artisticfox8
Copy link
Owner Author

The reason is the

const realTitle = statusObject["realTitle"];
replaceAll(realTitle);

pair of lines unconditionally in untranslateCurrentVideo

The call to replaceAll results in a call to setFakeTitleNode == a node which is never removed, so it's innerText can be set (querySelector finds it).
When the innerText is set, the MutationObserver gets this change and fires its callback, which eventually calls replaceAll and setFakeTitleNode, forming a loop.

This loop is the reason for the bug.

Removing the two lines fixes the bug.

@artisticfox8
Copy link
Owner Author

I will also add a check in untranslateCurrentVideo which URL it's being called from. It should only be called from a /watch page

@artisticfox8
Copy link
Owner Author

Testing some more, it seems the

const realTitle = statusObject["realTitle"];
replaceAll(realTitle);

have their use, as the title sometimes doesn't get updated without them, when replaceAll is called exactly once in the get call in untranslateCurrentVideo before the title element is constructed on the page.

In that case, we get:

replaceAll called
TypeError: can't access property "textContent", translatedTitleElement is null

which means const translatedTitleElement = document.querySelector(selector); didn't match.

So these lines will be put back, and

if (document.location.pathname != "/watch") {
    return;
}

will be added to untranslateCurrentVideo

@artisticfox8
Copy link
Owner Author

It is interesting, that on a /watch page all these functions get called a lot of times (while the page is loading), but they don't make an infinite loop (else the bug would not be on playlists only, the browser would hang on other pages too).

It is possible to see it after a while when everything has loaded, when I hover a mouse over a video, the only change MutationObserver shows are:

Array [ MutationRecord ]
​0: MutationRecord { type: "childList", target: span#yt-anti-translate-fake-node.style-scope.ytd-video-primary-info-renderer
, addedNodes: NodeList(1), … }

​​addedNodes: NodeList [ #text ]

​​​    0: #text "How Bubblegum Is Made!"
    ​​​length: 1

removedNodes: NodeList [ #text ]

​​​    0: #text "How Bubblegum Is Made!"
​​​    length: 1

​​target: <span id="yt-anti-translate-fake-node" class="style-scope ytd-video-primary-info-renderer" style="">
​​type: "childList"
​​<prototype>: MutationRecordPrototype { type: Getter, target: Getter, addedNodes: Getter, … }
​length: 1
​<prototype>: Array []

which does not start the infinite loop.

@artisticfox8
Copy link
Owner Author

The reason why it doesn't start an infinite loop is the mutationIdx % MUTATION_UPDATE_STEP == 0 check in untranslate
Interestingly, when hovering over videos, mutationIdx rarely stops at an even number. That is a fun implementation detail (I suppose) on YT side.

(it goes up by 10 when I move the mouse over a suggested video. If I end up at an even number moving the mouse erratically, the moment I move the mouse again, it goes up by 1 to an odd number. )

@artisticfox8
Copy link
Owner Author

If I remove the check, it makes a similar infinite loop.

Eg:

function untranslate(statusObject) {
    console.log("untranslate")
    if (!showOriginalTitlesRan) {
        console.log("untranslate inside", mutationIdx, MUTATION_UPDATE_STEP);
        mutationIdx = 2;
        if (mutationIdx % MUTATION_UPDATE_STEP == 0) {
            console.log("actually ran");
            untranslateCurrentVideo(statusObject);
            untranslateOtherVideos();
        }
        mutationIdx++;
    }
}

@artisticfox8
Copy link
Owner Author

And the same message is seen
image

artisticfox8 added a commit that referenced this issue Aug 2, 2024
Make untranslateCurrentVideo only run on "/watch" pages.
More info at #1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant