SOLVED: How do I access functions on the "Window" object from JavaScript running in ClearScript? #611
-
When running JavaScript in a browser, there is a global
Normally you can just write the following JavaScript in a browser with no errors:
However, the Is there something I can do to make those standard |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @PoseidonEnergy, yes there is a way to do this, you need to provide the function your self. Here are a example how i am doing it for private void Patch(V8ScriptEngine scriptEngine)
{
string jsPatch = """
if(typeof(btoa) !== "function")
{
globalThis.btoa = (stringToEncode) => {
const b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
stringToEncode = String(stringToEncode);
const rest = stringToEncode.length % 3;
let result = "";
for (let i = 0; i < stringToEncode.length;) {
const a = stringToEncode.charCodeAt(i++);
if (a > 255) {
throw new TypeError(`Fehler beim Ausführen von 'btoa': Der String der Encoded werden soll enthält ein ungültiges Zeichen (Position: ${i - 1}), diese liegt aushalb von 'Latin1'.`);
}
const b = stringToEncode.charCodeAt(i++);
if (b > 255) {
throw new TypeError(`Fehler beim Ausführen von 'btoa': Der String der Encoded werden soll enthält ein ungültiges Zeichen (Position: ${i - 1}), diese liegt aushalb von 'Latin1'.`);
}
const c = stringToEncode.charCodeAt(i++);
if (c > 255) {
throw new TypeError(`Fehler beim Ausführen von 'btoa': Der String der Encoded werden soll enthält ein ungültiges Zeichen (Position: ${i - 1}), diese liegt aushalb von 'Latin1'.`);
}
const bitmap = (a << 16) | (b << 8) | c;
result += b64.charAt(bitmap >> 18 & 63) + b64.charAt(bitmap >> 12 & 63) + b64.charAt(bitmap >> 6 & 63) + b64.charAt(bitmap & 63);
}
return rest ? result.slice(0, rest - 3) + "===".substring(rest) : result;
};
}
if (typeof atob !== 'function')
{
globalThis.atob = (string) =>
{
const b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
const b64re = /^(?:[A-Za-z\d+\/]{4})*?(?:[A-Za-z\d+\/]{2}(?:==)?|[A-Za-z\d+\/]{3}=?)?$/;
// atob can work with strings with whitespaces, even inside the encoded part,
// but only \t, \n, \f, \r and ' ', which can be stripped.
string = String(string).replace(/[\t\n\f\r ]+/g, "");
if (!b64re.test(string))
throw new TypeError("Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded.");
// Adding the padding if missing, for semplicity
string += "==".slice(2 - (string.length & 3));
let bitmap, result = "", r1, r2, i = 0;
for (; i < string.length;)
{
bitmap = b64.indexOf(string.charAt(i++)) << 18 | b64.indexOf(string.charAt(i++)) << 12
| (r1 = b64.indexOf(string.charAt(i++))) << 6 | (r2 = b64.indexOf(string.charAt(i++)));
result += r1 === 64 ? String.fromCharCode(bitmap >> 16 & 255)
: r2 === 64 ? String.fromCharCode(bitmap >> 16 & 255, bitmap >> 8 & 255)
: String.fromCharCode(bitmap >> 16 & 255, bitmap >> 8 & 255, bitmap & 255);
}
return result;
};
}
if(typeof isNaN !== 'function')
{
globalThis.isNaN = (num) =>{
return Number.isNaN(num);
}
}
""";
scriptEngine.Evaluate(jsPatch);
} You need to execute this code before running your script. I hope this helps. Kind regards, |
Beta Was this translation helpful? Give feedback.
Hi @PoseidonEnergy,
yes there is a way to do this, you need to provide the function your self.
Here are a example how i am doing it for
atob
,btoa
andisNaN
: