code-charity / youtube

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

[Feature Request] Allow diffrent preset for video quality based on power source #103 #2292

Closed Nirbhay97 closed 1 month ago

Nirbhay97 commented 1 month ago

issue -> quality and bandwidth #2240

ImprovedTube commented 1 month ago

Thank you! @Nirbhay97

Typically our users might want 1080p usually and 720p or 480p on battery or to pause when the charger is unplugged i also appreciate the gradually switching down like a gradual reminder.

    "qualityWhenRunningOnBattery":{   "message": "Quality, when running on battery"  },
    "pauseWhileIUnplugTheCharger":{    "message": "Pause when I unplug the charger!" },
    "whenBatteryIslowDecreaseQuality":{  "message": "Low battery: Gradually decrease the quality!"  },

The battery listener/s can depend on battery feature/s being enabled.

if (improvedTube.storage.qualityWhenRunningOnBattery || improvedTube.storage.pauseWhileIUnplugTheCharger || improvedTube.storage.whenBatteryIslowDecreaseQuality) {
    async function battery() {
        try {
            const battery = await navigator.getBattery(); 
            if (battery === null) { 
                console.log('Desktop!(?) "Battery === null"'); 
            } else if (battery.level === null) { 
                console.log("No battery present"); 
            } else if (improvedTube.storage.qualityWhenRunningOnBattery || improvedTube.storage.whenBatteryIslowDecreaseQuality) {
                battery.addEventListener("levelchange", () => {
                    if (!battery.charging) {
                        if (improvedTube.storage.qualityWhenRunningOnBattery) {
                            playerQuality(improvedTube.storage.qualityWhenRunningOnBattery);
                        }
                        if (improvedTube.storage.whenBatteryIslowDecreaseQuality && (battery.level < 0.15 || battery.dischargingTime < 1200)) {
                            if (battery.level > 0.11 || battery.dischargingTime > 900) {
                                playerQuality("large");
                            } else if (battery.level > 0.08 || battery.dischargingTime > 600) {
                                playerQuality("medium");
                            } else if (battery.level > 0.04 && battery.dischargingTime > 360) {
                                playerQuality("small");
                            } else {
                                playerQuality("tiny");
                            }
                        }
                    }
                });
            } 
            if (improvedTube.storage.pauseWhileIUnplugTheCharger) {
                battery.addEventListener("chargingchange", () => {
                    if (!battery.charging) { 
                        ImprovedTube.elements.player.pauseVideo(); 
                        ImprovedTube.paused = true; 
                    } else if (ImprovedTube.paused === true) { 
                        ImprovedTube.elements.player.playVideo(); 
                        delete ImprovedTube.paused; 
                    }
                });
            }
        } catch (error) { 
            console.error("Error accessing battery information:", error.message); 
        }
    } 
    battery();  
}

(untested)

Nirbhay97 commented 1 month ago

That's a really well written and improved code considering different battery cases, a whole lot of learning for me! Thanks @ImprovedTube .

Since the PR is still open, what is to be done from my side now? Or what things shall I focus on now for this feat.

ImprovedTube commented 1 month ago

Hi! @Nirbhay97 was yours tested already?

if you like you can.. : ..test/finish this with my code:

    ImprovedTube.playerQuality();
    ImprovedTube.batteryFeatures();

(we dont need to call it on focus, like QualityWithoutFocus)


..and/or check how to do this too:

So in our menu we can make battery features invisible on Desktops.

(which wont matter if it will be the same PR or another)

Nirbhay97 commented 1 month ago

Yes, I tested my code and it worked well,

  1. It will automatically resume previous set quality when on charging
  2. If battery goes below certain level, it will automatically start decreasing player quality

Yes, it makes more sense to hide this feat when using on desktop, which is currently not handled.

ImprovedTube commented 1 month ago

hi! @Nirbhay97 I commented out the toggles for now. As my continuation of your code might not be finished.
Looking forward!


It will automatically resume previous set quality when on charging

if it was set with our quality feature,

raszpl commented 2 weeks ago
qualityWhenRunningOnBattery: {
whenBatteryIslowDecreaseQuality: {
pauseWhileIUnplugTheCharger

inconsistent Naming convention, lets stick to snake case since every other option is already that please :)

someone already planned that, because: https://github.com/code-charity/youtube/blob/e8bd44f798263e76ec32afd1b02a2521b580fbac/menu/skeleton-parts/player.js#L925 but didnt finish, didnt add id and forgot to comment it :) no quality_when_low_battery ID = Codec h254 modal doesnt close on OK :

player.js:920 Uncaught 
TypeError: Cannot read properties of null (reading 'dispatchEvent')
    at refresh (player.js:920:58)
    at Object.ok (player.js:934:9)
    at HTMLButtonElement.click (satus.js:1234:36)

https://github.com/code-charity/youtube/pull/2377

ImprovedTube commented 2 weeks ago

Sorry my additions & tuned numbers delayed the release of the feature yet.

Greetings @Nirbhay97