Skip to content

Commit

Permalink
Merge pull request #2292 from Nirbhay97/master
Browse files Browse the repository at this point in the history
[Feature Request] Allow diffrent preset for video quality based on power source #103
  • Loading branch information
ImprovedTube authored May 21, 2024
2 parents df83b6f + ebbb5b3 commit 386e4b0
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
11 changes: 10 additions & 1 deletion _locales/en/messages.json
Original file line number Diff line number Diff line change
@@ -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"
},
Expand Down
1 change: 1 addition & 0 deletions js&css/web-accessible/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
54 changes: 54 additions & 0 deletions js&css/web-accessible/www.youtube.com/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
18 changes: 18 additions & 0 deletions menu/skeleton-parts/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 386e4b0

Please sign in to comment.