eval/native.ts

46 lines
1.5 KiB
TypeScript
Raw Permalink Normal View History

2024-12-19 07:31:54 -05:00
import { dialog } from "electron";
export async function evalCode(_, code: string) {
const d = await dialog.showMessageBox({
title: "Confirm code eval",
message: "IF YOU DID NOT INITIATE THIS, PRESS NO. The following code will be ran in the NodeJS context, meaning it will have FULL access to your computer. Do you still want to continue?\n\n" + code,
buttons: ["Yes", "No"],
});
if (d.response === 1) throw "Cancelled by user";
2024-12-23 14:42:43 -05:00
// lines 12 to 36 shamelessly stolen from codeberg.org/vee/bot
2024-12-19 07:31:54 -05:00
const console: any = {
_lines: [] as string[],
_log(...things: string[]) {
this._lines.push(
...things
.join(" ")
.split("\n")
);
}
};
console.log = console.error = console.warn = console.info = console._log.bind(console);
const fs = require("fs");
const http = require("http");
const https = require("https");
const crypto = require("crypto");
const net = require("net");
const path = require("path");
const util = require("util");
const assert = require("assert");
const os = require("os");
let script = code.replace(/(^`{3}(js|javascript)?|`{3}$)/g, "");
if (script.includes("await")) script = `(async () => { ${script} })()`;
try {
var result = await eval(script);
} catch (e: any) {
var result = e;
}
2024-12-23 14:45:54 -05:00
return `${result}\n\n${console._lines.join("\n")}`;
2024-12-19 07:31:54 -05:00
}