DannyAlas / DGG-Everywhere

Replaces live chats with destiny.gg chat.
https://chrome.google.com/webstore/detail/dgg-chat-everywhere/ncbnabljhfmaedpkdgcoembdcpdbnkma
Other
24 stars 0 forks source link

Sometimes DGG doesn't load #6

Closed mattroseman closed 1 year ago

mattroseman commented 1 year ago

If I toggle the DGG chat on or off, it works fine. But if I have it toggled on, and then I refresh, or leave and come back, it sometimes doesn't load.

My guess is it's from the 500 ms timeout before attempting to add the DGG chat iframe.

I was briefly fiddling around with the code and think it was consistent with this code:

document.onreadystatechange = function() {
    if (document.readyState === 'complete') {
        const observer = new MutationObserver((mutationList, observer) => {
            if ($("ytd-live-chat-frame").length && $("#chatframe").length) {
                loadDGGYT();
                observer.disconnect();
            }
        });
        observer.observe($('ytd-app')[0], {childList: true, subtree: true});
    }
}

MutationObserver's might be a more consistent solution, but I really didn't test that much.

PS: I like this project idea. I had a similar idea, but opposite, by adding YT chat to DGG 😄

DannyAlas commented 1 year ago

Hi @mattroseman! I actually initially used mutation observers for youtube but was getting some weird behavior with AdBlockers. The timeout ended up fixing it for the time, but as you've noted, this definitely isn't effective.

Messing with the mutation observers again, the code you provided seems to be working okay for youtube. I'll open a branch where we can do some more testing and integrate twitch as well!

PS: I'm very much a JS noob and open to any advice!! 😄

mattroseman commented 1 year ago

If there are issues with using MutationObservers, another idea is to use setInterval instead of setTimeout.

document.onreadystatechange = function() {
    if (document.readyState === 'complete') {
        const intervalId = setInterval(() => {
            if ($('ytd-live-chat-frame').length && $('#chatframe').length) {
                loadDGGYT();
                clearInterval(intervalId);
            }
        }, 500);
    }
}

My thinking is you check ever 500 ms, and keep checking until the necessary elements show up. Then you call the clearInterval to stop checking.