diff --git a/_locales/en/messages.json b/_locales/en/messages.json index c04a6f612..531237c44 100644 --- a/_locales/en/messages.json +++ b/_locales/en/messages.json @@ -1,4 +1,13 @@ -{ +{ + "qualityWhenRunningOnBattery":{ + "message": "Quality, when running on battery" + }, + "whenBatteryIslowDecreaseQuality":{ + "message": "Low battery: Gradually decrease the quality!" + }, + "pauseWhileIUnplugTheCharger":{ + "message": "Pause when I unplug the charger!" + }, "analytics": { "message": "Analytics" }, diff --git a/js&css/web-accessible/functions.js b/js&css/web-accessible/functions.js index 0506cfd2e..8803d3888 100644 --- a/js&css/web-accessible/functions.js +++ b/js&css/web-accessible/functions.js @@ -369,6 +369,7 @@ ImprovedTube.initPlayer = function () { ImprovedTube.subtitlesUserSettings(); ImprovedTube.subtitlesDisableLyrics(); ImprovedTube.playerQuality(); + ImprovedTube.batteryFeatures(); ImprovedTube.playerVolume(); if (this.storage.player_always_repeat === true) { ImprovedTube.playerRepeat(); } ImprovedTube.playerScreenshotButton(); diff --git a/js&css/web-accessible/www.youtube.com/player.js b/js&css/web-accessible/www.youtube.com/player.js index 09a1ac6b2..000933105 100644 --- a/js&css/web-accessible/www.youtube.com/player.js +++ b/js&css/web-accessible/www.youtube.com/player.js @@ -415,6 +415,60 @@ ImprovedTube.playerQualityWithoutFocus = function () { } }; /*------------------------------------------------------------------------------ +BATTERY FEATURES; PLAYER QUALITY BASED ON POWER STATUS +------------------------------------------------------------------------------*/ +ImprovedTube.batteryFeatures = function () { + if (ImprovedTube.storage.qualityWhenRunningOnBattery + || ImprovedTube.storage.pauseWhileIUnplugTheCharger + || ImprovedTube.storage.whenBatteryIslowDecreaseQuality) { + async function battery() { + try { + const battery = await navigator.getBattery(); + if (battery) { + const updateQuality = () => { + if (!battery.charging) { + if (ImprovedTube.storage.qualityWhenRunningOnBattery) { + ImprovedTube.playerQuality(ImprovedTube.storage.qualityWhenRunningOnBattery); + } + if (ImprovedTube.storage.whenBatteryIslowDecreaseQuality) { + let quality; + if (battery.level > 0.11 || battery.dischargingTime > 900) { + quality = "large"; + } else if (battery.level > 0.08 || battery.dischargingTime > 600) { + quality = "medium"; + } else if (battery.level > 0.04 || battery.dischargingTime > 360) { + quality = "small"; + } else { + quality = "tiny"; + } + ImprovedTube.playerQuality(quality); + } + } + }; + battery.addEventListener("levelchange", updateQuality); + if (ImprovedTube.storage.pauseWhileIUnplugTheCharger) { + battery.addEventListener("chargingchange", () => { + if (!battery.charging) { + ImprovedTube.elements.player.pauseVideo(); + ImprovedTube.paused = true; + } else if (ImprovedTube.paused) { + ImprovedTube.elements.player.playVideo(); + delete ImprovedTube.paused; + } + }); + } + updateQuality(); + } else { + console.log("No battery info - Desktop?"); + } + } catch (error) { + console.error("Error accessing battery information:", error.message); + } + } + battery(); + } +}; +/*------------------------------------------------------------------------------ FORCED VOLUME ------------------------------------------------------------------------------*/ ImprovedTube.playerVolume = function () { diff --git a/menu/skeleton-parts/player.js b/menu/skeleton-parts/player.js index ef782abcf..302fbdb42 100644 --- a/menu/skeleton-parts/player.js +++ b/menu/skeleton-parts/player.js @@ -746,6 +746,23 @@ extension.skeleton.main.layers.section.player.on.click = { } } }, + qualityWhenRunningOnBattery: { + component: 'switch', + text: 'qualityWhenRunningOnBattery', + on: { + render: function () { + extension.skeleton.main.layers.section.player.on.click.section_1.player_quality.on.render.call(this); + } + } + }, + whenBatteryIslowDecreaseQuality: { + component: 'switch', + text: 'whenBatteryIslowDecreaseQuality' + }, + pauseWhileIUnplugTheCharger: { + component: 'switch', + text: 'pauseWhileIUnplugTheCharger' + }, mini_player: { component: 'switch', text: 'customMiniPlayer' @@ -895,6 +912,7 @@ extension.skeleton.main.layers.section.player.on.click = { document.getElementById('player_codecs').dispatchEvent(new CustomEvent('render')); document.getElementById('optimize_codec_for_hardware_acceleration').dispatchEvent(new CustomEvent('render')); document.getElementById('player_quality_without_focus').dispatchEvent(new CustomEvent('render')); + document.getElementById('quality_when_low_battery').dispatchEvent(new CustomEvent('render')); } if (this.dataset.value === 'false') { let where = this;