Add YouTube codec check and their settings

This commit is contained in:
wuggy 2026-09-15 14:15:53 -07:00
commit 9230d4f436
6 changed files with 157 additions and 0 deletions

View file

@ -0,0 +1,77 @@
/*
* Adapted from enhanced-h264ify's inject_codec_check.js.
*
* The MIT License (MIT)
* Copyright (c) 2019 alextrv
* Copyright (c) 2015 erkserkserks
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*/
/* YouTube-only codec selection controls. */
(function () {
"use strict";
var settings = window.__dactyloidaeYouTubeVideoSettings || {};
var originalCanPlayType = HTMLMediaElement.prototype.canPlayType;
var originalIsTypeSupported = window.MediaSource &&
window.MediaSource.isTypeSupported;
function isVideoType(type) {
return /(^|\s|;)video\//i.test(type) ||
/(^|[; ])codecs\s*=\s*["']?(?:avc|vp0|av0)/i.test(type);
}
function isBlocked(type) {
if (!type || !isVideoType(type)) {
return false;
}
// AV1 is always blocked, independently of the preferences.
if (/av01|av99/i.test(type)) {
return true;
}
if (settings.forceH264 && !/avc1|avc3/i.test(type)) {
return true;
}
if (settings.hideUnaccelerated &&
!settings.vp9HardwareAccelerated && /vp9|vp09/i.test(type)) {
return true;
}
if (settings.disable60fps) {
var match = /(?:framerate|fps)\s*=\s*([0-9]+(?:\.[0-9]+)?)/i.exec(type);
if (match && Number(match[1]) > 30) {
return true;
}
}
return false;
}
HTMLMediaElement.prototype.canPlayType = function (type) {
if (isBlocked(type)) {
return "";
}
return originalCanPlayType.call(this, type);
};
if (window.MediaSource && originalIsTypeSupported) {
window.MediaSource.isTypeSupported = function (type) {
if (isBlocked(type)) {
return false;
}
return originalIsTypeSupported.call(this, type);
};
}
}());

View file

@ -99,6 +99,54 @@ InternalUserscriptsService.prototype = {
}
} catch (e) {}
let contentWin = win.wrappedJSObject || win;
let documentURI = null;
try {
documentURI = win.document.documentURIObject;
} catch (e) {}
// The codec shim is deliberately limited to YouTube. Use the browser's
// decoder probe rather than guessing from the user's graphics settings.
if (documentURI &&
documentURI.host &&
/(^|\.)youtube(?:-nocookie)?\.com$/i.test(documentURI.host)) {
let settings = {
hideUnaccelerated: Services.prefs.getBoolPref(
"browser.video.youtube.hide-unaccelerated", true),
forceH264: Services.prefs.getBoolPref(
"browser.video.youtube.force-h264", false),
disable60fps: Services.prefs.getBoolPref(
"browser.video.youtube.disable-60fps", false),
vp9HardwareAccelerated: false,
};
let loadCodecShim = function (vp9HardwareAccelerated) {
settings.vp9HardwareAccelerated = vp9HardwareAccelerated;
contentWin.__dactyloidaeYouTubeVideoSettings = settings;
try {
Services.scriptloader.loadSubScript(
"chrome://internaluserscripts/content/bundled-scripts/youtube-codec-check.user.js",
contentWin);
} catch (e) {}
};
try {
let utils = win.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindowUtils);
let hardwareSupport = utils.supportsHardwareVP9Decoding;
if (hardwareSupport && typeof hardwareSupport.then === "function") {
hardwareSupport.then(function (result) {
loadCodecShim(/^Yes;/.test(result));
}, function () {
loadCodecShim(false);
});
} else {
loadCodecShim(/^Yes;/.test(hardwareSupport));
}
} catch (e) {
loadCodecShim(false);
}
}
let logPolyfill = function (name, source) {
try {
if (

View file

@ -3,3 +3,6 @@
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
pref("browser.internal-userscripts.enabled", true);
pref("browser.video.youtube.hide-unaccelerated", true);
pref("browser.video.youtube.force-h264", false);
pref("browser.video.youtube.disable-60fps", false);

View file

@ -30,4 +30,5 @@ FINAL_TARGET_FILES['internal-userscripts'] += [
'bundled-scripts/textencoderstream-polyfill.user.js',
'bundled-scripts/transformstream-polyfill.user.js',
'bundled-scripts/webauthn-microsoft-shim.user.js',
'bundled-scripts/youtube-codec-check.user.js',
]