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

Fix eval() when TrustedScript assignment is required #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
18 changes: 16 additions & 2 deletions YouTube Livechat Channel Resolver.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ var main = function() {
// backup the original fetch function
var originalFetch = window.fetch;

// trusted type policy
var trustedTypePolicy = window.trustedTypePolicy;

// helper functions used to intercept and modify youtube api responses
var responseProxy = function(callback) {
XMLHttpRequest.prototype.open = function() {
Expand Down Expand Up @@ -206,7 +209,7 @@ var main = function() {
var scripts = document.getElementsByTagName("script");
for (var script of scripts) {
if(script.text.indexOf("window[\"ytInitialData\"]") >= 0) {
window.eval(script.text.replace("ytInitialData", "ytInitialData_original"));
window.eval(trustedTypePolicy.createScript(script.text.replace("ytInitialData", "ytInitialData_original")));
}
}

Expand All @@ -224,7 +227,18 @@ var injectScript = function(frameWindow) {

console.info("Run Fury, run!");

frameWindow.eval("("+ main.toString() +")();");
// Set up passthrough trusted types policy if supported by the browser
if (frameWindow.trustedTypes) {
frameWindow.trustedTypePolicy = frameWindow.trustedTypes.createPolicy("ytgtc_policy", {
createScript: value => value,
});
} else {
frameWindow.trustedTypePolicy = {
createScript: value => value,
};
}

frameWindow.eval(frameWindow.trustedTypePolicy.createScript("("+ main.toString() +")();"));
}

// We need this to detect the chat frame in firefox
Expand Down