Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 1080p Premium support #102

Merged
merged 4 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "youtube-auto-hd",
"displayName": "YouTube Auto HD + FPS",
"version": "1.7.13",
"version": "1.7.14",
"description": "Automatically set the video quality on YouTube according to its FPS!",
"author": "avi12 <avi6106@gmail.com>",
"homepage": "https://github.com/avi12/youtube-auto-hd",
Expand Down
4 changes: 2 additions & 2 deletions src/contents/content-script-init-desktop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ function addTemporaryBodyListenerOnDesktop(): void {
return;
}


// We need to reset global variables, as well as prepare to change the quality of the new video
window.ythdLastQualityClicked = null;
await prepareToChangeQualityOnDesktop();
Expand Down Expand Up @@ -112,7 +111,8 @@ async function init(): Promise<void> {
// the video's quality will be changed as soon as it loads
new MutationObserver(async (_, observer) => {
const elVideo = getVisibleElement<HTMLVideoElement>(SELECTORS.video);
if (!elVideo) {
const elLogo = getVisibleElement(SELECTORS.logo);
if (!elVideo || !elLogo) {
return;
}

Expand Down
11 changes: 6 additions & 5 deletions src/cs-helpers/desktop/content-script-desktop.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
OBSERVER_OPTIONS,
QUALITY_TO_NOT_SELECT,
SELECTORS,
getFpsFromRange,
getIQuality,
Expand Down Expand Up @@ -51,17 +52,17 @@ function getCurrentQualityElements(): HTMLDivElement[] {
return elMenuOptions.filter(getIsQualityElement) as HTMLDivElement[];
}

function convertQualityToNumber(elQuality: Element): VideoQuality | 0 {
const isPremiumAccount = Boolean(getVisibleElement(SELECTORS.logoPremium));
function convertQualityToNumber(elQuality: Element): VideoQuality | typeof QUALITY_TO_NOT_SELECT {
const isPremiumAccount = Boolean(getVisibleElement(SELECTORS.logo).querySelector(SELECTORS.logoPremium));
const isPremiumQuality = Boolean(elQuality.querySelector(SELECTORS.labelPremium));
const qualityNumber = parseInt(elQuality.textContent) as VideoQuality;
if (!isPremiumQuality) {
return qualityNumber;
}
return !isPremiumAccount ? 0 : qualityNumber;
return !isPremiumAccount ? QUALITY_TO_NOT_SELECT : qualityNumber;
}

function getCurrentQualities(): (VideoQuality | 0)[] {
function getCurrentQualities(): (VideoQuality | typeof QUALITY_TO_NOT_SELECT)[] {
const elQualities = getCurrentQualityElements();
return elQualities.map(convertQualityToNumber);
}
Expand Down Expand Up @@ -96,7 +97,7 @@ function changeQuality(qualityCustom?: VideoQuality): void {
if (isQualityExists) {
applyQuality(iQuality);
} else if (getIsQualityLower(elQualities[0], window.ythdLastUserQualities[fpsStep])) {
const iQualityAvailable = qualitiesAvailable.findIndex(quality => quality > 0);
const iQualityAvailable = qualitiesAvailable.findIndex(quality => quality > QUALITY_TO_NOT_SELECT);
applyQuality(iQualityAvailable);
} else {
const iClosestQuality = qualitiesAvailable.findIndex(quality => quality <= window.ythdLastUserQualities[fpsStep]);
Expand Down
2 changes: 2 additions & 0 deletions src/shared-scripts/ythd-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type { QualityFpsPreferences, VideoFPS, VideoQuality, YouTubeLabel } from
const storageLocal = new Storage({ area: "local" });

export const OBSERVER_OPTIONS: MutationObserverInit = Object.freeze({ childList: true, subtree: true });
export const QUALITY_TO_NOT_SELECT = 0;
window.ythdLastUserQualities = { ...initial.qualities };

export async function getStorage<T>({
Expand Down Expand Up @@ -65,6 +66,7 @@ export enum SELECTORS {
actionButtonsContainer = "#top-row #owner",
donationSection = ".ythd-donation-section",
// Premium
logo = "ytd-logo",
logoPremium = "#youtube-red-paths",
labelPremium = ".ytp-premium-label",
// Mobile
Expand Down