自动切换抖音到最高可用画质(4K>2K>1080P>720P),支持自定义检测时间间隔
< Feedback on 抖音自动切换最高画质
失效了,改了一下// ==UserScript==// @name 抖音自动切换最高画质// @namespace http://tampermonkey.net/// @version 1.3// @description 自动切换抖音到最高可用画质(4K>2K>1080P>720P),支持自定义检测时间间隔// @author zpc5560// @match *://*.douyin.com/*// @grant none// @icon https://lf1-cdn-tos.bytegoofy.com/goofy/ies/douyin_web/public/favicon.ico// @downloadURL https://update.example.com/scripts/douyin-auto-quality.user.js// @updateURL https://update.example.com/scripts/douyin-auto-quality.meta.js// ==/UserScript==(function() { 'use strict'; let isQualitySwitched = false; let currentQuality = null; let lastAttemptTime = 0; let currentVideoSrc = null; const DEFAULT_CHECK_INTERVAL = 5 * 60 * 1000; // 默认5分钟 const MAX_CHECK_INTERVAL = 60 * 60 * 1000; // 最大1小时 let CHECK_INTERVAL = DEFAULT_CHECK_INTERVAL; let checkTimer = null; let retryCount = 0; const MAX_RETRY = 5; // 定义画质优先级 const qualityPriority = [ { name: '超清 4K', minHeight: 2160 }, { name: '超清 2K', minHeight: 1440 }, { name: '高清 1080P', minHeight: 1080 }, { name: '高清 720P', minHeight: 720 } ]; // 创建设置按钮 function createSettingsButton() { if (document.querySelector('#qualitySettingsBtn')) return; const button = document.createElement('button'); button.id = 'qualitySettingsBtn'; button.textContent = '画质设置'; button.style.cssText = ` position: fixed; bottom: 90px; left: 20px; z-index: 999999; padding: 8px 12px; background-color: rgba(34, 34, 34, 0.9); color: white; border: none; border-radius: 20px; cursor: pointer; font-size: 14px; box-shadow: 0 2px 10px rgba(0,0,0,0.3); font-weight: bold; `; button.addEventListener('click', showSettingsDialog); document.body.appendChild(button); } // 显示设置对话框 function showSettingsDialog() { if (document.querySelector('#qualitySettingsDialog')) return; const dialog = document.createElement('div'); dialog.id = 'qualitySettingsDialog'; dialog.style.cssText = ` position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); background-color: rgba(25,25,25,0.95); padding: 20px; border-radius: 16px; box-shadow: 0 5px 20px rgba(0,0,0,0.5); z-index: 10000; min-width: 320px; color: white; backdrop-filter: blur(10px); border: 1px solid rgba(255,255,255,0.1); `; const currentInterval = CHECK_INTERVAL / 1000; const minutes = Math.floor(currentInterval / 60); const seconds = currentInterval % 60; dialog.innerHTML = `
`; document.body.appendChild(dialog); const minutesInput = dialog.querySelector('#minutes'); const secondsInput = dialog.querySelector('#seconds'); const saveBtn = dialog.querySelector('#saveBtn'); const cancelBtn = dialog.querySelector('#cancelBtn'); // 保存设置 saveBtn.addEventListener('click', () => { const minutes = parseInt(minutesInput.value) || 0; const seconds = parseInt(secondsInput.value) || 0; const totalSeconds = minutes * 60 + seconds; if (totalSeconds === 0) { alert('检测间隔不能为0'); return; } if (totalSeconds > 3600) { alert('检测间隔不能超过1小时'); return; } CHECK_INTERVAL = totalSeconds * 1000; localStorage.setItem('douyinQualityCheckInterval', CHECK_INTERVAL.toString()); resetState(); document.body.removeChild(dialog); }); // 取消设置 cancelBtn.addEventListener('click', () => { document.body.removeChild(dialog); }); } // 获取当前选中的画质 function getCurrentQuality() { const selectedItem = document.querySelector('.virtual .item.selected'); return selectedItem ? selectedItem.textContent.trim() : null; } // 检查画质是否需要切换 function checkQualityNeedsSwitch() { const currentSelectedQuality = getCurrentQuality(); const currentTime = Date.now(); if (!currentQuality || currentSelectedQuality !== currentQuality || currentTime - lastAttemptTime >= CHECK_INTERVAL) { isQualitySwitched = false; switchToHighQuality(); } } // 检查视频源是否更改 function checkVideoSourceChanged() { const videoElement = document.querySelector('video.xgplayer-video'); if (videoElement) { const newVideoSrc = videoElement.currentSrc || videoElement.src; if (newVideoSrc && newVideoSrc !== currentVideoSrc) { currentVideoSrc = newVideoSrc; isQualitySwitched = false; retryCount = 0; console.log('检测到视频源变化,重置状态'); switchToHighQuality(); } } } // 尝试点击画质按钮展开菜单 function tryExpandQualityMenu() { const qualityButton = document.querySelector('.gear.isSmoothSwitchClarityLogin'); if (qualityButton) { // 新版抖音需要点击两次才能展开菜单 qualityButton.click(); setTimeout(() => qualityButton.click(), 100); return true; } return false; } function switchToHighQuality() { if (isQualitySwitched || retryCount > MAX_RETRY) return; retryCount++; console.log(`尝试切换画质 (${retryCount}/${MAX_RETRY})`); // 尝试展开画质菜单 if (!tryExpandQualityMenu()) { console.log('未找到画质按钮,等待重试'); setTimeout(switchToHighQuality, 1000); return; } setTimeout(() => { const qualityOptions = Array.from(document.querySelectorAll('.virtual .item')); if (!qualityOptions.length) { console.log('未找到画质选项,等待重试'); setTimeout(switchToHighQuality, 1000); return; } let selectedOption = null; for (const quality of qualityPriority) { const option = qualityOptions.find(opt => opt.textContent.includes(quality.name) ); if (option) { selectedOption = option; break; } } if (selectedOption && !selectedOption.classList.contains('selected')) { selectedOption.click(); currentQuality = selectedOption.textContent.trim(); lastAttemptTime = Date.now(); console.log(`已切换到${currentQuality}画质`); isQualitySwitched = true; retryCount = 0; startQualityCheck(); } else if (selectedOption) { console.log(`已是最高画质: ${currentQuality}`); isQualitySwitched = true; retryCount = 0; } else { const smartOption = qualityOptions.find(opt => opt.textContent.includes('智能') ); if (smartOption) { smartOption.click(); currentQuality = '智能'; lastAttemptTime = Date.now(); console.log('未找到720P或更高画质,暂时切换到智能画质'); isQualitySwitched = true; retryCount = 0; startQualityCheck(); } } // 关闭菜单 setTimeout(() => { const closeBtn = document.querySelector('.gear.isSmoothSwitchClarityLogin'); if (closeBtn) closeBtn.click(); }, 300); }, 800); } // 启动定时检测 function startQualityCheck() { if (checkTimer) { clearInterval(checkTimer); } checkTimer = setInterval(() => { console.log('定时检测视频源中...'); checkVideoSourceChanged(); }, CHECK_INTERVAL); } // 停止定时检测 function stopQualityCheck() { if (checkTimer) { clearInterval(checkTimer); checkTimer = null; } } // 使用 MutationObserver 监听页面变化 const observer = new MutationObserver((mutations) => { for (const mutation of mutations) { if (mutation.type === 'childList') { // 检测新视频加载 const videoAdded = mutation.addedNodes.length > 0 && Array.from(mutation.addedNodes).some(node => node.nodeName === 'VIDEO' || (node.querySelector && node.querySelector('video')) ); if (videoAdded) { console.log('检测到新视频加载'); setTimeout(() => { isQualitySwitched = false; switchToHighQuality(); }, 1500); } } } }); observer.observe(document.body, { childList: true, subtree: true }); // 在页面切换时重置状态 function resetState() { isQualitySwitched = false; currentQuality = null; lastAttemptTime = 0; currentVideoSrc = null; retryCount = 0; stopQualityCheck(); startQualityCheck(); } // 初始化检测间隔 function initCheckInterval() { const savedInterval = localStorage.getItem('douyinQualityCheckInterval'); if (savedInterval) { const interval = parseInt(savedInterval); if (!isNaN(interval) && interval > 0 && interval <= MAX_CHECK_INTERVAL) { CHECK_INTERVAL = interval; } } } // 监听路由变化 (SPA) let currentURL = window.location.href; setInterval(() => { if (window.location.href !== currentURL) { currentURL = window.location.href; resetState(); } }, 1000); // 页面加载完成后初始化 function initScript() { initCheckInterval(); createSettingsButton(); resetState(); // 初始延迟执行 setTimeout(() => { switchToHighQuality(); // 添加额外检测 setInterval(() => { if (!isQualitySwitched) switchToHighQuality(); }, 5000); }, 3000); } if (document.readyState === 'complete' || document.readyState === 'interactive') { setTimeout(initScript, 1000); } else { window.addEventListener('load', initScript); } // 页面关闭时清理 window.addEventListener('unload', () => { stopQualityCheck(); observer.disconnect(); });})();
Sign in to post a reply.
一年攒够 12 元
云驰互联
失效了,改了一下
// ==UserScript==
// @name 抖音自动切换最高画质
// @namespace http://tampermonkey.net/
// @version 1.3
// @description 自动切换抖音到最高可用画质(4K>2K>1080P>720P),支持自定义检测时间间隔
// @author zpc5560
// @match *://*.douyin.com/*
// @grant none
// @icon https://lf1-cdn-tos.bytegoofy.com/goofy/ies/douyin_web/public/favicon.ico
// @downloadURL https://update.example.com/scripts/douyin-auto-quality.user.js
// @updateURL https://update.example.com/scripts/douyin-auto-quality.meta.js
// ==/UserScript==
(function() {
'use strict';
let isQualitySwitched = false;
let currentQuality = null;
let lastAttemptTime = 0;
let currentVideoSrc = null;
const DEFAULT_CHECK_INTERVAL = 5 * 60 * 1000; // 默认5分钟
const MAX_CHECK_INTERVAL = 60 * 60 * 1000; // 最大1小时
let CHECK_INTERVAL = DEFAULT_CHECK_INTERVAL;
let checkTimer = null;
let retryCount = 0;
const MAX_RETRY = 5;
// 定义画质优先级
const qualityPriority = [
{ name: '超清 4K', minHeight: 2160 },
{ name: '超清 2K', minHeight: 1440 },
{ name: '高清 1080P', minHeight: 1080 },
{ name: '高清 720P', minHeight: 720 }
];
// 创建设置按钮
function createSettingsButton() {
if (document.querySelector('#qualitySettingsBtn')) return;
const button = document.createElement('button');
button.id = 'qualitySettingsBtn';
button.textContent = '画质设置';
button.style.cssText = `
position: fixed;
bottom: 90px;
left: 20px;
z-index: 999999;
padding: 8px 12px;
background-color: rgba(34, 34, 34, 0.9);
color: white;
border: none;
border-radius: 20px;
cursor: pointer;
font-size: 14px;
box-shadow: 0 2px 10px rgba(0,0,0,0.3);
font-weight: bold;
`;
button.addEventListener('click', showSettingsDialog);
document.body.appendChild(button);
}
// 显示设置对话框
function showSettingsDialog() {
if (document.querySelector('#qualitySettingsDialog')) return;
const dialog = document.createElement('div');
dialog.id = 'qualitySettingsDialog';
dialog.style.cssText = `
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: rgba(25,25,25,0.95);
padding: 20px;
border-radius: 16px;
box-shadow: 0 5px 20px rgba(0,0,0,0.5);
z-index: 10000;
min-width: 320px;
color: white;
backdrop-filter: blur(10px);
border: 1px solid rgba(255,255,255,0.1);
`;
const currentInterval = CHECK_INTERVAL / 1000;
const minutes = Math.floor(currentInterval / 60);
const seconds = currentInterval % 60;
dialog.innerHTML = `
画质检测设置
检测间隔:
分钟
秒钟
注意:间隔时间越长越省电,但切换可能不及时
取消
保存
`;
document.body.appendChild(dialog);
const minutesInput = dialog.querySelector('#minutes');
const secondsInput = dialog.querySelector('#seconds');
const saveBtn = dialog.querySelector('#saveBtn');
const cancelBtn = dialog.querySelector('#cancelBtn');
// 保存设置
saveBtn.addEventListener('click', () => {
const minutes = parseInt(minutesInput.value) || 0;
const seconds = parseInt(secondsInput.value) || 0;
const totalSeconds = minutes * 60 + seconds;
if (totalSeconds === 0) {
alert('检测间隔不能为0');
return;
}
if (totalSeconds > 3600) {
alert('检测间隔不能超过1小时');
return;
}
CHECK_INTERVAL = totalSeconds * 1000;
localStorage.setItem('douyinQualityCheckInterval', CHECK_INTERVAL.toString());
resetState();
document.body.removeChild(dialog);
});
// 取消设置
cancelBtn.addEventListener('click', () => {
document.body.removeChild(dialog);
});
}
// 获取当前选中的画质
function getCurrentQuality() {
const selectedItem = document.querySelector('.virtual .item.selected');
return selectedItem ? selectedItem.textContent.trim() : null;
}
// 检查画质是否需要切换
function checkQualityNeedsSwitch() {
const currentSelectedQuality = getCurrentQuality();
const currentTime = Date.now();
if (!currentQuality ||
currentSelectedQuality !== currentQuality ||
currentTime - lastAttemptTime >= CHECK_INTERVAL) {
isQualitySwitched = false;
switchToHighQuality();
}
}
// 检查视频源是否更改
function checkVideoSourceChanged() {
const videoElement = document.querySelector('video.xgplayer-video');
if (videoElement) {
const newVideoSrc = videoElement.currentSrc || videoElement.src;
if (newVideoSrc && newVideoSrc !== currentVideoSrc) {
currentVideoSrc = newVideoSrc;
isQualitySwitched = false;
retryCount = 0;
console.log('检测到视频源变化,重置状态');
switchToHighQuality();
}
}
}
// 尝试点击画质按钮展开菜单
function tryExpandQualityMenu() {
const qualityButton = document.querySelector('.gear.isSmoothSwitchClarityLogin');
if (qualityButton) {
// 新版抖音需要点击两次才能展开菜单
qualityButton.click();
setTimeout(() => qualityButton.click(), 100);
return true;
}
return false;
}
function switchToHighQuality() {
if (isQualitySwitched || retryCount > MAX_RETRY) return;
retryCount++;
console.log(`尝试切换画质 (${retryCount}/${MAX_RETRY})`);
// 尝试展开画质菜单
if (!tryExpandQualityMenu()) {
console.log('未找到画质按钮,等待重试');
setTimeout(switchToHighQuality, 1000);
return;
}
setTimeout(() => {
const qualityOptions = Array.from(document.querySelectorAll('.virtual .item'));
if (!qualityOptions.length) {
console.log('未找到画质选项,等待重试');
setTimeout(switchToHighQuality, 1000);
return;
}
let selectedOption = null;
for (const quality of qualityPriority) {
const option = qualityOptions.find(opt =>
opt.textContent.includes(quality.name)
);
if (option) {
selectedOption = option;
break;
}
}
if (selectedOption && !selectedOption.classList.contains('selected')) {
selectedOption.click();
currentQuality = selectedOption.textContent.trim();
lastAttemptTime = Date.now();
console.log(`已切换到${currentQuality}画质`);
isQualitySwitched = true;
retryCount = 0;
startQualityCheck();
} else if (selectedOption) {
console.log(`已是最高画质: ${currentQuality}`);
isQualitySwitched = true;
retryCount = 0;
} else {
const smartOption = qualityOptions.find(opt =>
opt.textContent.includes('智能')
);
if (smartOption) {
smartOption.click();
currentQuality = '智能';
lastAttemptTime = Date.now();
console.log('未找到720P或更高画质,暂时切换到智能画质');
isQualitySwitched = true;
retryCount = 0;
startQualityCheck();
}
}
// 关闭菜单
setTimeout(() => {
const closeBtn = document.querySelector('.gear.isSmoothSwitchClarityLogin');
if (closeBtn) closeBtn.click();
}, 300);
}, 800);
}
// 启动定时检测
function startQualityCheck() {
if (checkTimer) {
clearInterval(checkTimer);
}
checkTimer = setInterval(() => {
console.log('定时检测视频源中...');
checkVideoSourceChanged();
}, CHECK_INTERVAL);
}
// 停止定时检测
function stopQualityCheck() {
if (checkTimer) {
clearInterval(checkTimer);
checkTimer = null;
}
}
// 使用 MutationObserver 监听页面变化
const observer = new MutationObserver((mutations) => {
for (const mutation of mutations) {
if (mutation.type === 'childList') {
// 检测新视频加载
const videoAdded = mutation.addedNodes.length > 0 &&
Array.from(mutation.addedNodes).some(node =>
node.nodeName === 'VIDEO' ||
(node.querySelector && node.querySelector('video'))
);
if (videoAdded) {
console.log('检测到新视频加载');
setTimeout(() => {
isQualitySwitched = false;
switchToHighQuality();
}, 1500);
}
}
}
});
observer.observe(document.body, { childList: true, subtree: true });
// 在页面切换时重置状态
function resetState() {
isQualitySwitched = false;
currentQuality = null;
lastAttemptTime = 0;
currentVideoSrc = null;
retryCount = 0;
stopQualityCheck();
startQualityCheck();
}
// 初始化检测间隔
function initCheckInterval() {
const savedInterval = localStorage.getItem('douyinQualityCheckInterval');
if (savedInterval) {
const interval = parseInt(savedInterval);
if (!isNaN(interval) && interval > 0 && interval <= MAX_CHECK_INTERVAL) {
CHECK_INTERVAL = interval;
}
}
}
// 监听路由变化 (SPA)
let currentURL = window.location.href;
setInterval(() => {
if (window.location.href !== currentURL) {
currentURL = window.location.href;
resetState();
}
}, 1000);
// 页面加载完成后初始化
function initScript() {
initCheckInterval();
createSettingsButton();
resetState();
// 初始延迟执行
setTimeout(() => {
switchToHighQuality();
// 添加额外检测
setInterval(() => {
if (!isQualitySwitched) switchToHighQuality();
}, 5000);
}, 3000);
}
if (document.readyState === 'complete' || document.readyState === 'interactive') {
setTimeout(initScript, 1000);
} else {
window.addEventListener('load', initScript);
}
// 页面关闭时清理
window.addEventListener('unload', () => {
stopQualityCheck();
observer.disconnect();
});
})();