code-charity / youtube

[top~1 open YouTube & Video browser-extension] - Enrich your experience & choice! 🧰180+ options & clever features 📌set+forget📌Longest-standing(been tough). Please join🧩us👨‍👩‍👧‍👧 ..⋮ {playback|content discovery|player|extra buttons|distractions|related videos|shorts|ads|quality|codec|full tab|full screen}
http://improvedtube.com
Other
3.49k stars 527 forks source link

high CPU usage on idle Youtube pages. #431

Closed user58823 closed 3 years ago

user58823 commented 4 years ago

As I've stated in #427 there are issues with the use of setInterval() without delays defined that are never cleared combined with constantly using document.querySelector()/document.querySelectorAll().

This is a serious issue (i.e. a bug) that causes the extension to use way too much CPU for absolutely nothing. It is NOT just an optimization.

This is also related to #423 / #421 and you can also read some proposed fixes in #427 Unfortunately the other issue was prematurely closed and locked by one of your contributors.

@victor-savinov Can you please not close and lock issues just because you don't seem to understand what the problem is? Also what kind of attitude is "I'm not going to fix my bad code, do it yourself"?

ghost commented 4 years ago

Also what kind of attitude is "I'm not going to fix my bad code, do it yourself"?

ImprovedTube is free open source project. If you want personalized support, then hire a developer.

user58823 commented 4 years ago

But this isn't about personalized support, this is about fixing broken code. This extension has a bug in it that negatively affects many of your users.

I've basically done most of the work anyway, by identifying the cause and proposing a solution. Why is it so hard to take the last step to fix this and make a pull request yourself as a maintainer? It will take me so much longer to set up Git/Github on my computer, clone this repo, get familiar with your code and coding style and proposing a pull request.

user58823 commented 4 years ago

Fine, there, now I've done all of the work, save actually making a pull request:

/* --- content-scripts.js from line 698 --- */

ImprovedTube.livechat_type = function() {
    if (ImprovedTube.storage.livechat_type === 'live') {
        this.livechat_type_wait = setInterval(function() {
            var a = document.querySelector('#chat-messages #dropdown a:nth-child(2)');
            if (a) {
                clearInterval(ImprovedTube.livechat_type_wait);

                ImprovedTube.livechat_type_wait = false;

                a.click();
            }
        }, 250);
        setTimeout(function () {
            clearInterval(ImprovedTube.livechat_type_wait);
        }, 2000);
    }
};

/* --- content-scripts.js from line 3106 --- */

        if (ImprovedTube.storage.default_dark_theme === true) {
            var wait = setInterval(function() {
                if (document.body) {
                    clearInterval(wait);

                    document.body.setAttribute('dark', '');
                }
            }, 250);
        } else {
            var wait = setInterval(function() {
                if (document.body) {
                    clearInterval(wait);

                    document.body.removeAttribute('dark');
                }
            }, 250);
        }

        var wait2 = setInterval(function() {
            var app = document.querySelector('ytd-app');
            if (
                app &&
                typeof app.toggleDarkThemeAttribute_ === 'function'
            ) {
                clearInterval(wait2);
                if (app.isAppDarkTheme_() === false) {
                    app.toggleDarkThemeAttribute_(true);
                }
            }
        }, 250);

        document.documentElement.setAttribute('it-theme', 'true');
    } else {
        document.documentElement.removeAttribute('it-theme');
        document.documentElement.removeAttribute('dark');

        var wait = setInterval(function() {
            if (document.body) {
                clearInterval(wait);

                document.body.removeAttribute('dark');
            }
        }, 250);

        var wait2 = setInterval(function() {
            var app = document.querySelector('ytd-app');
            if (
                app &&
                typeof app.toggleDarkThemeAttribute_ === 'function' &&
            ) {
                clearInterval(wait2);

                if (app.isAppDarkTheme_() === true) {
                    app.toggleDarkThemeAttribute_(true);
                }
            }
        }, 250);
    }
};

There is a very high chance that there are even more cases of this issue, but I have better things to do than to go through so much code and do someone else's job.

It's not everyone else's responsibility to fix your bad code. You're a maintainer here, so take responsibility and actually maintain the code.

ImprovedTube commented 4 years ago

@user58823 Thank you! 🌻🌻 Welcome to the team! If anybody buys us a coffee we will share it with you. Workload is overwhelming right now these days. It was addictive to put our freetime in this all the years. Yet the many users of ImprovedTube also deserve an active open development so we want it to be tidy/inviting. Hope you have another look.

ghost commented 4 years ago

@user58823 don't be mad at me :heart:

Unfortunately setTimeout solution won't work for users with slow internet connections. Anyway, we'll fix performance issues in coming days.

EvgenKo423 commented 4 years ago

@user58823, thanks for your effort! ~It mostly gone now, but some load still exists.~ Nope, after updating a page it's back. I thought it's already patched, but looking at the source it's not.

@victor-savinov, I would love to make some cool things for others and I have my own list, but unfortunately my personal life meter is already at 0...

Unfortunately setTimeout solution won't work for users with slow internet connections. Anyway, we'll fix performance issues in coming days.

~BTW, isn't that what ~document.ready()~ window.onload() is for?~ Nevermind, it doesn't wait for scripts to execute...

ghost commented 4 years ago

@EvgenKo423 i'll spend this night fixing most bugs & bad code, so everyone will get update very soon.

tympapost commented 4 years ago

Excuse the off-topic but I think it fits the conversation. I would like to take the chance and say thanks to Victor for his contributions for this project. I've been following IT for years and it wouldn't be the same today without him. Thanks @victor-savinov ! Sincerely....

ghost commented 4 years ago

HIGH CPU USAGE: Themes (https://github.com/ImprovedTube/ImprovedTube/commit/eb9ffa38bc8f8168d12746201644736eda4a5614) Temporary removal of "Livechat type" feature (https://github.com/ImprovedTube/ImprovedTube/commit/fb31d4b4fa3cb37ad531ad0fd57d70ffe91575b0)

"Livechat type" feature no longer works, will be redeveloped soon.

ghost commented 4 years ago

Does anyone know where YouTube stores preset "Top chat" / "Live chat" data? (or may be some function that can be mutated) It would be great to use a data modification instead of a GUI clicker.

EvgenKo423 commented 4 years ago

Can't say about the function, but it's definitely not stored at all, because it resets on page reload. I guess there would be no need for this option if it would be saved. ;-)

ghost commented 3 years ago

Most have already been fixed in testing version. More optimizations in the future...