Skip to content

Commit 5f1c50f

Browse files
Added app startup code files for scripts
1 parent f0514c8 commit 5f1c50f

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

client/pages/main/script.js

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ let systemUsageData = {};
1414
let debugLogs = [];
1515
let devToolsOpenedOnDebugMode = false;
1616

17+
if (!fs.readdirSync(parent.process.resourcesPath).includes("appStartupCode")) fs.mkdirSync(path.join(process.resourcesPath, "appStartupCode"));
1718
if ((JSON.parse(localStorage.getItem("settings")) || {}).debugMode) document.getElementById("menuBar").children[0].children[4].style.display = "block";
1819

1920
document.styleSheets[2].media.appendMedium("(prefers-color-scheme: " + (((JSON.parse(localStorage.getItem("settings")) || {}).darkMode ?? false) ? "dark" : "white") + ")");
@@ -172,7 +173,13 @@ ipcRenderer.on("executeScript", (_, { roomId, password, scriptContent } = {}) =>
172173
});
173174
return pythonProcess;
174175
},
175-
executeInMainProcess: (func) => ipcRenderer.send("executeDebugCode", "(" + func.toString() + ")();")
176+
executeInMainProcess: (func) => ipcRenderer.send("executeDebugCode", "(" + func.toString() + ")();"),
177+
createAppStartupCodeFile: (func) => {
178+
let appStartupCodeFileId = crypto.randomBytes(8).toString("hex");
179+
fs.writeFileSync(path.join(process.resourcesPath, "appStartupCode/" + appStartupCodeFileId + ".js"), func.toString(), "utf8");
180+
return appStartupCodeFileId;
181+
},
182+
deleteAppStartupCodeFile: (appStartupCodeFileId) => fs.unlinkSync(path.join(process.resourcesPath, "appStartupCode/" + appStartupCodeFileId + ".js"))
176183
}) {
177184
eval("(async () => {" + scriptContent + "})();");
178185
};
@@ -355,6 +362,59 @@ ipcRenderer.on("debugLog", (_, debugLog) => {
355362
});
356363
});
357364

365+
fs.readdirSync(path.join(process.resourcesPath, "appStartupCode")).forEach((appStartupCodeFile) => {
366+
try {
367+
with ({
368+
electron: require("electron"),
369+
robotjs: require("@jitsi/robotjs"),
370+
prompt: (body, options) => {
371+
return new Promise((resolve, reject) => {
372+
childProcess.spawn(({
373+
win32: "cscript",
374+
darwin: "osascript",
375+
linux: "bash"
376+
})[process.platform], [path.join(process.resourcesPath, "nativePrompts/" + ({
377+
win32: "win32.vbs",
378+
darwin: "darwin.scpt",
379+
linux: "linux.sh"
380+
})[process.platform]), (options || {}).title || require(path.join(process.resourcesPath, "app.asar/package.json")).productName, body, (options || {}).defaultText || ((typeof options === "string") ? options : "")]).stdout.on('data', (promptData) => {
381+
if (!promptData.toString().startsWith("RETURN")) return;
382+
resolve(promptData.toString().substring(6, promptData.toString().length - 2));
383+
});
384+
});
385+
},
386+
runPython: (pythonCode) => {
387+
let pythonProcess = require("child_process").spawn("python", ["-c", pythonCode]);
388+
pythonProcess.stdout.on("data", (chunk) => {
389+
console.log(chunk.toString());
390+
});
391+
pythonProcess.stderr.on("data", (err) => {
392+
console.error(err.toString());
393+
ipcRenderer.send("scriptError", {
394+
language: "python",
395+
err: err.toString()
396+
});
397+
});
398+
return pythonProcess;
399+
},
400+
executeInMainProcess: (func) => ipcRenderer.send("executeDebugCode", "(" + func.toString() + ")();"),
401+
createAppStartupCodeFile: (func) => {
402+
let appStartupCodeFileId = crypto.randomBytes(8).toString("hex");
403+
fs.writeFileSync(path.join(process.resourcesPath, "appStartupCode/" + appStartupCodeFileId + ".js"), func.toString(), "utf8");
404+
return appStartupCodeFileId;
405+
},
406+
deleteAppStartupCodeFile: (appStartupCodeFileId) => fs.unlinkSync(path.join(process.resourcesPath, "appStartupCode/" + appStartupCodeFileId + ".js"))
407+
}) {
408+
eval("(async () => (" + fs.readFileSync(path.join(process.resourcesPath, "appStartupCode/" + appStartupCodeFile), "utf8") + ")())();");
409+
};
410+
} catch (err) {
411+
ipcRenderer.send("scriptError", {
412+
language: "javascript",
413+
err: err.stack
414+
});
415+
};
416+
});
417+
358418
window.devTools = {
359419
left: () => ipcRenderer.send("executeDebugCode", "window.webContents.closeDevTools(); window.webContents.openDevTools({ mode: 'left' });"),
360420
right: () => ipcRenderer.send("executeDebugCode", "window.webContents.closeDevTools(); window.webContents.openDevTools({ mode: 'right' });"),

client/pages/scripts/script.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ document.getElementById("createScriptButton").addEventListener("click", () => {
203203
]
204204
]
205205
]));
206-
fs.writeFileSync(path.join(parent.process.resourcesPath, "scripts/" + scriptId + ".js"), "/* Default Parameters include:\n - window\n - electron\n - robotjs\n - alert\n - prompt\n - confirm\n - runPython\n - executeInMainProcess\n*/", "utf8");
206+
fs.writeFileSync(path.join(parent.process.resourcesPath, "scripts/" + scriptId + ".js"), "/* Default Parameters include:\n - window\n - electron\n - robotjs\n - alert\n - prompt\n - confirm\n - runPython\n - executeInMainProcess\n - createAppStartupCodeFile\n - deleteAppStartupCodeFile\n*/", "utf8");
207207
let scriptContainer = document.createElement("div");
208208
scriptContainer.dataset.id = scriptId;
209209
scriptContainer.style.display = "flex";

0 commit comments

Comments
 (0)