bitburner-src/electron/main.js

108 lines
3.0 KiB
JavaScript
Raw Normal View History

2021-12-12 18:48:42 +01:00
const { app, BrowserWindow, Menu, shell } = require("electron");
2021-11-27 21:07:25 +01:00
const greenworks = require("./greenworks");
2021-09-21 19:29:16 +02:00
2021-11-27 21:07:25 +01:00
if (greenworks.init()) {
console.log("Steam API has been initialized.");
} else {
console.log("Steam API has failed to initialize.");
}
2021-11-29 05:51:01 +01:00
const debug = false;
2021-11-05 05:14:11 +01:00
2021-09-15 18:22:36 +02:00
function createWindow() {
const win = new BrowserWindow({
show: false,
webPreferences: {
2021-11-05 05:14:11 +01:00
devTools: debug,
2021-09-15 18:22:36 +02:00
},
});
win.removeMenu();
win.maximize();
win.loadFile("index.html");
win.show();
2021-11-05 05:19:41 +01:00
if (debug) win.webContents.openDevTools();
win.webContents.on("new-window", function (e, url) {
// make sure local urls stay in electron perimeter
2021-09-25 07:26:03 +02:00
if (url.substr(0, "file://".length) === "file://") {
return;
}
// and open every other protocols on the browser
e.preventDefault();
shell.openExternal(url);
});
2021-11-27 21:07:25 +01:00
2021-12-03 00:42:30 +01:00
// Create the Application's main menu
Menu.setApplicationMenu(
Menu.buildFromTemplate([
{
label: "Edit",
submenu: [
{ label: "Undo", accelerator: "CmdOrCtrl+Z", selector: "undo:" },
{ label: "Redo", accelerator: "Shift+CmdOrCtrl+Z", selector: "redo:" },
{ type: "separator" },
{ label: "Cut", accelerator: "CmdOrCtrl+X", selector: "cut:" },
{ label: "Copy", accelerator: "CmdOrCtrl+C", selector: "copy:" },
{ label: "Paste", accelerator: "CmdOrCtrl+V", selector: "paste:" },
{ label: "Select All", accelerator: "CmdOrCtrl+A", selector: "selectAll:" },
],
},
2021-12-12 18:48:42 +01:00
{
label: "reloads",
submenu: [
{
label: "reload",
accelerator: "f5",
click: () => {
win.loadFile("index.html");
},
},
{
label: "reload & kill all scripts",
click: () => {
win.webContents.forcefullyCrashRenderer();
setTimeout(() => win.loadFile("index.html", { query: { noScripts: "true" } }), 5000);
},
},
],
},
2021-12-12 18:58:57 +01:00
{
label: "fullscreen",
submenu: [
{
label: "toggle",
accelerator: "f9",
click: (() => {
let full = false;
return () => {
full = !full;
win.setFullScreen(full);
};
})(),
},
],
},
2021-12-03 00:42:30 +01:00
]),
);
2021-11-29 05:51:01 +01:00
// This is backward but the game fills in an array called `document.achievements` and we retrieve it from
// here. Hey if it works it works.
const achievements = greenworks.getAchievementNames();
2021-12-02 18:43:00 +01:00
// for (const ach of achievements) {
// greenworks.clearAchievement(ach, () => undefined);
// }
2021-11-27 21:07:25 +01:00
setInterval(async () => {
2021-11-29 05:51:01 +01:00
const achs = await win.webContents.executeJavaScript("document.achievements");
for (const ach of achs) {
if (!achievements.includes(ach)) continue;
greenworks.activateAchievement(ach, () => undefined);
}
2021-11-27 21:07:25 +01:00
}, 1000);
2021-09-15 18:22:36 +02:00
}
app.whenReady().then(() => {
createWindow();
});