Fix Trusted Types violation in manifest interceptor injection, which fixes data not showing for YouTube DRM

This commit is contained in:
voldemort 2025-07-20 17:43:13 +07:00
parent 889a4c63f3
commit 6e22837047
2 changed files with 202 additions and 160 deletions

View File

@ -70,8 +70,7 @@ function safeHeaderShellEscape(str) {
// Intercep network to find manifest
function injectManifestInterceptor() {
const script = document.createElement("script");
script.textContent = `
// Execute the interceptor code directly instead of injecting a script
(function () {
function isProbablyManifest(text = "", contentType = "") {
const lowerCT = contentType?.toLowerCase() ?? "";
@ -87,8 +86,13 @@ function injectManifestInterceptor() {
const isJsonManifest = sample.includes('"playlist"') && sample.includes('"segments"');
return (
isHLSMime || isDASHMime || isSmoothMime ||
isHLSKeyword || isDASHKeyword || isSmoothKeyword || isJsonManifest
isHLSMime ||
isDASHMime ||
isSmoothMime ||
isHLSKeyword ||
isDASHKeyword ||
isSmoothKeyword ||
isJsonManifest
);
}
@ -113,14 +117,24 @@ function injectManifestInterceptor() {
});
const headerFlags = Object.entries(headersObj)
.map(([key, val]) => '--add-headers "' + safeHeaderShellEscape(key) + ': ' + safeHeaderShellEscape(val) + '"')
.map(
([key, val]) =>
'--add-headers "' +
safeHeaderShellEscape(key) +
": " +
safeHeaderShellEscape(val) +
'"'
)
.join(" ");
window.postMessage({
window.postMessage(
{
type: "__MANIFEST_HEADERS__",
url,
headers: headerFlags
}, "*");
headers: headerFlags,
},
"*"
);
}
} catch (e) {}
@ -146,8 +160,8 @@ function injectManifestInterceptor() {
console.log("[Manifest][xhr]", this.__url, contentType);
const xhrHeaders = {};
const rawHeaders = this.getAllResponseHeaders().trim().split(/\\r?\\n/);
rawHeaders.forEach(line => {
const rawHeaders = this.getAllResponseHeaders().trim().split(/\r?\n/);
rawHeaders.forEach((line) => {
const parts = line.split(": ");
if (parts.length === 2) {
xhrHeaders[parts[0]] = parts[1];
@ -155,23 +169,30 @@ function injectManifestInterceptor() {
});
const headerFlags = Object.entries(xhrHeaders)
.map(([key, val]) => '--add-headers "' + safeHeaderShellEscape(key) + ': ' + safeHeaderShellEscape(val) + '"')
.map(
([key, val]) =>
'--add-headers "' +
safeHeaderShellEscape(key) +
": " +
safeHeaderShellEscape(val) +
'"'
)
.join(" ");
window.postMessage({
window.postMessage(
{
type: "__MANIFEST_HEADERS__",
url: this.__url,
headers: headerFlags
}, "*");
headers: headerFlags,
},
"*"
);
}
} catch (e) {}
});
return originalXHRSend.apply(this, arguments);
};
})();
`;
document.documentElement.appendChild(script);
script.remove();
}
injectManifestInterceptor();

View File

@ -70,8 +70,7 @@ function safeHeaderShellEscape(str) {
// Intercep network to find manifest
function injectManifestInterceptor() {
const script = document.createElement("script");
script.textContent = `
// Execute the interceptor code directly instead of injecting a script
(function () {
function isProbablyManifest(text = "", contentType = "") {
const lowerCT = contentType?.toLowerCase() ?? "";
@ -87,8 +86,13 @@ function injectManifestInterceptor() {
const isJsonManifest = sample.includes('"playlist"') && sample.includes('"segments"');
return (
isHLSMime || isDASHMime || isSmoothMime ||
isHLSKeyword || isDASHKeyword || isSmoothKeyword || isJsonManifest
isHLSMime ||
isDASHMime ||
isSmoothMime ||
isHLSKeyword ||
isDASHKeyword ||
isSmoothKeyword ||
isJsonManifest
);
}
@ -113,14 +117,24 @@ function injectManifestInterceptor() {
});
const headerFlags = Object.entries(headersObj)
.map(([key, val]) => '--add-headers "' + safeHeaderShellEscape(key) + ': ' + safeHeaderShellEscape(val) + '"')
.map(
([key, val]) =>
'--add-headers "' +
safeHeaderShellEscape(key) +
": " +
safeHeaderShellEscape(val) +
'"'
)
.join(" ");
window.postMessage({
window.postMessage(
{
type: "__MANIFEST_HEADERS__",
url,
headers: headerFlags
}, "*");
headers: headerFlags,
},
"*"
);
}
} catch (e) {}
@ -146,8 +160,8 @@ function injectManifestInterceptor() {
console.log("[Manifest][xhr]", this.__url, contentType);
const xhrHeaders = {};
const rawHeaders = this.getAllResponseHeaders().trim().split(/\\r?\\n/);
rawHeaders.forEach(line => {
const rawHeaders = this.getAllResponseHeaders().trim().split(/\r?\n/);
rawHeaders.forEach((line) => {
const parts = line.split(": ");
if (parts.length === 2) {
xhrHeaders[parts[0]] = parts[1];
@ -155,23 +169,30 @@ function injectManifestInterceptor() {
});
const headerFlags = Object.entries(xhrHeaders)
.map(([key, val]) => '--add-headers "' + safeHeaderShellEscape(key) + ': ' + safeHeaderShellEscape(val) + '"')
.map(
([key, val]) =>
'--add-headers "' +
safeHeaderShellEscape(key) +
": " +
safeHeaderShellEscape(val) +
'"'
)
.join(" ");
window.postMessage({
window.postMessage(
{
type: "__MANIFEST_HEADERS__",
url: this.__url,
headers: headerFlags
}, "*");
headers: headerFlags,
},
"*"
);
}
} catch (e) {}
});
return originalXHRSend.apply(this, arguments);
};
})();
`;
document.documentElement.appendChild(script);
script.remove();
}
injectManifestInterceptor();