自动切换到你预先设定的画質。会优先使用Premium比特率。
当前为
// ==UserScript==
// @name Youtube HD Premium
// @name:zh-TW Youtube HD Premium
// @name:zh-CN Youtube HD Premium
// @name:ja Youtube HD Premium
// @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @author ElectroKnight22
// @namespace electroknight22_youtube_hd_namespace
// @version 2025.09.19
// @note I would prefer semantic versioning but it's a bit too late to change it at this point. Calendar versioning was originally chosen to maintain similarity to the adisib's code.
// @match *://www.youtube.com/*
// @match *://m.youtube.com/*
// @match *://www.youtube-nocookie.com/*
// @exclude *://www.youtube.com/live_chat*
// @grant none
// @run-at document-idle
// @inject-into page
// @license MIT
// @description Automatically switches to your pre-selected resolution. Enables premium when possible.
// @description:zh-TW 自動切換到你預先設定的畫質。會優先使用Premium位元率。
// @description:zh-CN 自动切换到你预先设定的画質。会优先使用Premium比特率。
// @description:ja 自動的に設定した画質に替わります。Premiumのビットレートを優先的に選択します。
// @homepage https://gf.qytechs.cn/scripts/498145-youtube-hd-premium
// ==/UserScript==
/*jshint esversion: 11 */
(function () {
'use strict';
const IS_MOBILE = window.location.hostname === 'm.youtube.com';
const STORAGE_KEY = 'YTHD_settings';
const DEFAULT_SETTINGS = {
targetResolution: 'hd2160',
};
const QUALITIES = {
highres: { p: 4320, label: '8K' },
hd2160: { p: 2160, label: '4K' },
hd1440: { p: 1440, label: '1440p' },
hd1080: { p: 1080, label: '1080p' },
hd720: { p: 720, label: '720p' },
large: { p: 480, label: '480p' },
medium: { p: 360, label: '360p' },
small: { p: 240, label: '240p' },
tiny: { p: 144, label: '144p' },
};
const PREMIUM_INDICATOR = 'Premium';
const SVG_NS = 'http://www.w3.org/2000/svg';
const ICONS = {
createPinIcon: () => {
const svg = document.createElementNS(SVG_NS, 'svg');
svg.setAttribute('viewBox', '0 0 24 24');
svg.setAttribute('height', '24');
svg.setAttribute('width', '24');
const path = document.createElementNS(SVG_NS, 'path');
path.setAttribute('d', 'M16,12V4H17V2H7V4H8V12L6,14V16H11.5V22H12.5V16H18V14L16,12Z');
path.setAttribute('fill', 'currentColor');
svg.appendChild(path);
return svg;
},
};
const state = {
userSettings: { ...DEFAULT_SETTINGS },
moviePlayer: null,
};
// --- Core Logic ---
function resolveOptimalQuality(videoQualityData, targetResolutionString) {
const availableQualities = [...new Set(videoQualityData.map((q) => q.quality))];
const targetValue = QUALITIES[targetResolutionString].p;
const bestQualityString = availableQualities
.filter((q) => QUALITIES[q] && QUALITIES[q].p <= targetValue)
.sort((a, b) => QUALITIES[b].p - QUALITIES[a].p)[0];
if (!bestQualityString) return null;
let normalCandidate = null;
let premiumCandidate = null;
for (const quality of videoQualityData) {
if (quality.quality === bestQualityString && quality.isPlayable) {
if (quality.qualityLabel?.trim().endsWith(PREMIUM_INDICATOR)) premiumCandidate = quality;
else normalCandidate = quality;
}
}
return premiumCandidate || normalCandidate;
}
function setResolution() {
try {
state.moviePlayer = state.moviePlayer || fallbackGetPlayer();
if (!state.moviePlayer || typeof state.moviePlayer.getAvailableQualityData !== 'function') {
throw new Error('No valid video player found.');
}
const videoQualityData = state.moviePlayer.getAvailableQualityData();
if (!videoQualityData || !videoQualityData.length) {
throw new Error('Cannot determine available video qualities.');
}
const optimalQuality = resolveOptimalQuality(videoQualityData, state.userSettings.targetResolution);
if (optimalQuality) {
state.moviePlayer.setPlaybackQualityRange(optimalQuality.quality, optimalQuality.quality, optimalQuality.formatId);
}
} catch (error) {
console.error('Error when setting resolution:', error);
}
}
function fallbackGetPlayer() {
if (window.location.pathname.startsWith('/shorts')) {
return document.querySelector('#shorts-player');
} else if (window.location.pathname.startsWith('/watch')) {
return document.querySelector('#movie_player');
} else {
return document.querySelector('.inline-preview-player');
}
}
function handlePlayerStateChange(playerState) {
const playerElement = state.moviePlayer ?? fallbackGetPlayer();
if (!playerElement) return;
if (playerState === 1 && !playerElement.hasAttribute('YTHD-resolution-set')) {
playerElement.setAttribute('YTHD-resolution-set', 'true');
setResolution();
} else if (playerState === -1 && playerElement.hasAttribute('YTHD-resolution-set')) {
playerElement.removeAttribute('YTHD-resolution-set');
}
}
function processVideoLoad(event = null) {
state.moviePlayer = event?.target?.player_ ?? fallbackGetPlayer();
setResolution();
const playerElement = document.getElementById('movie_player');
if (playerElement && !playerElement.hasAttribute('YTHD-listener-added')) {
playerElement.addEventListener('onStateChange', handlePlayerStateChange);
playerElement.setAttribute('YTHD-listener-added', 'true');
}
}
// --- Settings Persistence ---
function saveUserSettings() {
try {
localStorage.setItem(STORAGE_KEY, JSON.stringify(state.userSettings));
} catch (error) {
console.error('Error saving settings:', error);
}
}
function loadUserSettings() {
try {
const storedSettings = JSON.parse(localStorage.getItem(STORAGE_KEY));
if (storedSettings) state.userSettings = { ...DEFAULT_SETTINGS, ...storedSettings };
if (!QUALITIES[state.userSettings.targetResolution]) {
state.userSettings.targetResolution = DEFAULT_SETTINGS.targetResolution;
}
saveUserSettings();
} catch (error) {
console.error('Error loading settings:', error);
}
}
// --- Desktop UI Logic ---
function createYTHDHeaderTrigger(titleText) {
const header = document.createElement('div');
header.id = 'ythd-header-trigger';
header.className = 'ytp-panel-header';
header.style.cursor = 'pointer';
const title = document.createElement('div');
title.className = 'ytp-panel-title';
title.style.display = 'flex';
title.style.justifyContent = 'space-between';
title.style.width = '100%';
title.style.alignItems = 'center';
title.style.padding = '16px';
const leftGroup = document.createElement('span');
leftGroup.style.display = 'flex';
leftGroup.style.alignItems = 'center';
leftGroup.style.gap = '8px';
leftGroup.append(ICONS.createPinIcon(), titleText);
const rightGroup = document.createElement('span');
rightGroup.id = 'ythd-header-label';
rightGroup.textContent = `${QUALITIES[state.userSettings.targetResolution].label} >`;
title.append(leftGroup, rightGroup);
header.appendChild(title);
return header;
}
function setupQualityMenuNavigation(qualityPanel) {
if (qualityPanel.querySelector('#ythd-animation-wrapper')) return;
const nativeHeader = qualityPanel.querySelector('.ytp-panel-header');
const settingsPopup = qualityPanel.closest('.ytp-popup.ytp-settings-menu');
const nativeTitleText = nativeHeader?.querySelector('.ytp-panel-title')?.textContent.trim() || 'Quality';
if (!nativeHeader || !settingsPopup) return;
const ythdHeaderTrigger = createYTHDHeaderTrigger(nativeTitleText);
nativeHeader.after(ythdHeaderTrigger);
const animationWrapper = document.createElement('div');
animationWrapper.id = 'ythd-animation-wrapper';
animationWrapper.style.position = 'relative';
animationWrapper.style.overflow = 'hidden';
const originalWrapperChildren = [...qualityPanel.children];
animationWrapper.append(...originalWrapperChildren);
qualityPanel.replaceChildren(animationWrapper);
const animateAndSwap = (contentSetupCallback, isForward) => {
const animationDuration = 250;
const oldContent = document.createElement('div');
oldContent.style.position = 'absolute';
oldContent.style.width = '100%';
oldContent.append(...animationWrapper.childNodes);
const newContent = document.createElement('div');
newContent.style.position = 'absolute';
newContent.style.width = '100%';
contentSetupCallback(newContent);
const oldFinalX = isForward ? '-100%' : '100%';
const newInitialX = isForward ? '100%' : '-100%';
oldContent.style.transform = 'translateX(0)';
newContent.style.transform = `translateX(${newInitialX})`;
const oldHeight = animationWrapper.offsetHeight;
animationWrapper.replaceChildren(oldContent, newContent);
const newHeight = newContent.offsetHeight;
animationWrapper.style.height = `${oldHeight}px`;
requestAnimationFrame(() => {
animationWrapper.style.transition = `height ${animationDuration}ms linear`;
oldContent.style.transition = `transform ${animationDuration}ms ease-in-out`;
newContent.style.transition = `transform ${animationDuration}ms ease-in-out`;
animationWrapper.style.height = `${newHeight}px`;
oldContent.style.transform = `translateX(${oldFinalX})`;
newContent.style.transform = 'translateX(0)';
});
setTimeout(() => {
animationWrapper.replaceChildren(...newContent.childNodes);
animationWrapper.style.cssText = 'position: relative;';
}, animationDuration);
};
const switchToNativeMenu = () => {
animateAndSwap((newWrapper) => {
newWrapper.append(...originalWrapperChildren);
const restoredTriggerLabel = newWrapper.querySelector('#ythd-header-label');
if (restoredTriggerLabel) {
restoredTriggerLabel.textContent = `${QUALITIES[state.userSettings.targetResolution].label} >`;
}
}, false);
};
const switchToYTHDMenu = () => {
animateAndSwap((newWrapper) => {
const backButton = document.createElement('button');
backButton.className = 'ytp-panel-back-button ytp-button';
const title = document.createElement('div');
title.className = 'ytp-panel-title';
title.style.display = 'flex';
title.style.alignItems = 'center';
title.style.gap = '8px';
title.append(ICONS.createPinIcon(), nativeTitleText);
const ythdHeader = document.createElement('div');
ythdHeader.className = 'ytp-panel-header';
ythdHeader.append(backButton, title);
const ythdMenu = document.createElement('div');
ythdMenu.className = 'ytp-panel-menu';
Object.entries(QUALITIES).forEach(([key, value]) => {
const menuItem = document.createElement('div');
menuItem.className = 'ytp-menuitem';
menuItem.setAttribute('role', 'menuitemradio');
menuItem.setAttribute('aria-checked', (state.userSettings.targetResolution === key).toString());
menuItem.dataset.resolutionKey = key;
const labelDiv = document.createElement('div');
labelDiv.className = 'ytp-menuitem-label';
labelDiv.textContent = `${value.p}p ${value.label.includes('K') ? `(${value.label})` : ''}`.trim();
menuItem.append(labelDiv);
ythdMenu.appendChild(menuItem);
});
newWrapper.append(ythdHeader, ythdMenu);
backButton.addEventListener('click', (event) => {
event.stopPropagation();
switchToNativeMenu();
});
ythdMenu.querySelectorAll('.ytp-menuitem').forEach((item) => {
item.addEventListener('click', (event) => {
event.stopPropagation();
const newResolution = item.dataset.resolutionKey;
if (state.userSettings.targetResolution === newResolution) return;
ythdMenu.querySelector('[aria-checked="true"]')?.setAttribute('aria-checked', 'false');
item.setAttribute('aria-checked', 'true');
state.userSettings.targetResolution = newResolution;
saveUserSettings();
setResolution();
document.getElementById('ythd-header-label').textContent = `${QUALITIES[newResolution].label} >`;
switchToNativeMenu();
});
});
}, true);
};
animationWrapper.querySelector('#ythd-header-trigger').addEventListener('click', (event) => {
event.stopPropagation();
switchToYTHDMenu();
});
}
function startDesktopObserver() {
const observer = new MutationObserver((mutations) => {
for (const mutation of mutations) {
for (const node of mutation.addedNodes) {
if (node.nodeType === 1 && node.classList.contains('ytp-panel')) {
if (node.querySelector('.ytp-menuitem[role="menuitemradio"]') && node.classList.contains('ytp-quality-menu')) {
setupQualityMenuNavigation(node);
}
}
}
}
});
observer.observe(document.body, { childList: true, subtree: true });
}
// --- Mobile UI Logic ---
function showMobileQualityOverlay(closeNativeMenuCallback) {
if (document.getElementById('ythd-mobile-overlay')) return;
const overlay = document.createElement('div');
overlay.id = 'ythd-mobile-overlay';
overlay.style.cssText = `
position: fixed; top: 0; left: 0; width: 100%; height: 100%;
background: rgba(0, 0, 0, 0.7); display: flex; align-items: center;
justify-content: center; z-index: 9999; backdrop-filter: blur(4px);
`;
const menuBox = document.createElement('div');
menuBox.style.cssText = `
background: #282828; color: white; padding: 16px;
border-radius: 12px; min-width: 280px; max-width: 90%;
font-family: "Roboto", "Arial", sans-serif;
`;
const title = document.createElement('div');
title.textContent = 'Set Quality Preference';
title.style.cssText = 'font-size: 1.2em; margin-bottom: 16px; font-weight: 500;';
menuBox.appendChild(title);
Object.entries(QUALITIES).forEach(([key, value]) => {
const menuItem = document.createElement('div');
menuItem.textContent = `${value.p}p ${value.label.includes('K') ? `(${value.label})` : ''}`.trim();
menuItem.style.cssText = 'padding: 14px; cursor: pointer; border-radius: 8px;';
if (state.userSettings.targetResolution === key) {
menuItem.style.fontWeight = 'bold';
menuItem.style.backgroundColor = 'rgba(255, 255, 255, 0.1)';
}
menuItem.onclick = () => {
state.userSettings.targetResolution = key;
saveUserSettings();
setResolution();
document.body.removeChild(overlay);
if (closeNativeMenuCallback) closeNativeMenuCallback();
};
menuBox.appendChild(menuItem);
});
overlay.onclick = (event) => {
if (event.target === overlay) {
document.body.removeChild(overlay);
}
};
overlay.appendChild(menuBox);
document.body.appendChild(overlay);
}
function injectMobileTrigger(bottomSheetNode) {
if (bottomSheetNode.querySelector('.ythd-mobile-trigger')) return;
const templateItem = bottomSheetNode.querySelector('[role=menuitem], ytm-menu-service-item-renderer');
if (!templateItem) return;
const triggerItem = templateItem.cloneNode(true);
triggerItem.classList.add('ythd-mobile-trigger');
triggerItem.removeAttribute('aria-disabled');
const icon = triggerItem.querySelector('yt-icon, c3-icon');
const label = triggerItem.querySelector('.yt-formatted-string, .menu-service-item-text');
if (icon) {
icon.replaceChildren(ICONS.createPinIcon());
}
if (label) {
label.textContent = 'Set Quality Preference';
}
triggerItem.onclick = (event) => {
event.preventDefault();
event.stopPropagation();
const closeNativeMenu = () => document.querySelector('ytw-scrim')?.click();
showMobileQualityOverlay(closeNativeMenu);
};
const parent = templateItem.parentElement;
parent.insertBefore(triggerItem, parent.firstChild);
}
function initializeMobileEventListeners() {
let settingsClicked = false;
const observer = new MutationObserver((mutations, obs) => {
if (!settingsClicked) return;
const bottomSheet = document.querySelector('bottom-sheet-container:not(:empty)');
if (bottomSheet) {
injectMobileTrigger(bottomSheet);
settingsClicked = false;
obs.disconnect();
}
});
document.addEventListener(
'click',
(event) => {
const settingsButton = event.target.closest(
'.player-settings-icon, [aria-label*="Settings"], .slim-video-metadata-actions button',
);
if (settingsButton) {
settingsClicked = true;
observer.observe(document.body, { childList: true, subtree: true });
}
},
true,
);
}
// --- Initialization ---
function addEventListeners() {
const playerUpdateEvent = IS_MOBILE ? 'state-navigateend' : 'yt-player-updated';
window.addEventListener(playerUpdateEvent, processVideoLoad);
window.addEventListener('pageshow', processVideoLoad);
}
function initialize() {
loadUserSettings();
addEventListeners();
if (IS_MOBILE) {
initializeMobileEventListeners();
} else {
startDesktopObserver();
}
}
initialize();
})();
QingJ © 2025
镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址