+ js_decode_script = ("""
+ const vm = require('vm');
+
+ const sandbox = {
+ location: {
+ hash: '',
+ href: '',
+ protocol: 'http:'
+ },
+ history: {
+ pushState: function(){}
+ },
+ document: {},
+ navigator: {
+ userAgent: ''
+ },
+ XMLHttpRequest: class XMLHttpRequest {},
+ matchMedia: () => ({matches: () => {}, media: ''}),
+ signature: %(signature)s,
+ transformed_signature: null,
+ g: function(){} // this is _yt_player
+ };
+ sandbox.window = sandbox;
+
+ const code_string = %(code)s + ';';
+ const exec_string = 'transformed_signature = %(func_name)s("", "MARKER", signature);';
+ vm.runInNewContext(code_string + exec_string, sandbox);
+
+ function findSignature(obj) {
+ if (typeof obj !== 'object') {
+ return;
+ }
+ for (const [key, value] of Object.entries(obj)) {
+ if (key === 'MARKER') {
+ return value;
+ }
+ const result = findSignature(value);
+ if (result) {
+ return result;
+ }
+ }
+ }
+ console.log(findSignature(sandbox.transformed_signature));
+ """ % params)
+
+ p.stdin.write(js_decode_script.encode("utf-8"))