Skip to content

Commit

Permalink
Use JS-Interpreter for youtube
Browse files Browse the repository at this point in the history
  • Loading branch information
qsniyg committed Mar 11, 2024
1 parent b4d3d71 commit de98509
Showing 1 changed file with 57 additions and 5 deletions.
62 changes: 57 additions & 5 deletions src/userscript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20000,6 +20000,10 @@ var $$IMU_EXPORT$$;
size: 21482,
crc32: 337445473,
crc32_size: 2641711624
},
"JSInterpreter": {
name: "acorn_interpreter",
url: ""
}
};

Expand Down Expand Up @@ -31067,17 +31071,65 @@ var $$IMU_EXPORT$$;
return done(null, 6*60*60);
}

return done(new Function(["a"], match3[1]), 60*60);
let sanitized = match3[1];

let sanitize = function(code) {
return code
.replace(/new Date\("([^"]+)"\)/, function(_, x) {
return new Date(x).getTime();
})
.replace(/Math.pow\(([0-9]+),([0-9]+)\)/, function(_, x, y) {
return Math_pow(x, y);
});
};

while (true) {
let new_sanitized = sanitize(sanitized);
if (new_sanitized === sanitized)
break;

sanitized = new_sanitized;
}

get_library("JSInterpreter", options, options.do_request, function(JSInterpreter) {
if (!JSInterpreter) {
console_error(cache_key, "Unable to fetch JSInterpreter");
return done(null, false);
}

let source = "var replacer = function(a) {" + sanitized + "}; replacer(a);";

//let func = new Function(["a"], sanitized); // for comparison
let cache = {};

let replacer_wrap = function(n) {
if (n in cache)
return cache[n];

// TODO: move this out of replacer_wrap, somehow change "a".
let source = "var a = \"" + n + "\";var replacer = function(a) {" + sanitized + "}; replacer(a);";
let interp = new JSInterpreter(source);
let globalObject = interp.getGlobalScope().object;
//interp.setProperty(globalObject, "a", interp.nativeToPseudo(n));
interp.run();
cache[n] = interp.value;
return interp.value;
};

return done(replacer_wrap, 60*60);
});
});
});
};

var replace_n = function(n, cb) {
get_n_replacer(function(replacer) {
if (!replacer)
return cb(n);
api_cache.fetch("youtube_n:" + n, cb, function(done) {
get_n_replacer(function(replacer) {
if (!replacer)
return done(n, false);

cb(replacer(n));
done(replacer(n), 60*60);
});
});
};

Expand Down

0 comments on commit de98509

Please sign in to comment.