Files
ms365-electron/main.js

611 lines
18 KiB
JavaScript
Raw Normal View History

const { app, Menu, BrowserWindow, dialog, shell } = require("electron");
const { autoUpdater } = require("electron-updater");
const openAboutWindow = require("about-window").default;
const checkInternetConnected = require("check-internet-connected");
const axios = require("axios");
const ElectronDl = require("electron-dl");
const contextMenu = require("electron-context-menu");
const path = require("path");
const Store = require("electron-store");
const log = require("electron-log");
const store = new Store();
const isMac = process.platform === "darwin";
const isWin = process.platform === "win32";
const isLinux = process.platform === "linux";
const RPC = require("discord-rpc");
const clientId = "942637872530460742";
const rpc = new RPC.Client({ transport: "ipc" });
2022-03-31 14:54:54 +05:30
const windowsuseragent =
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36";
2022-03-31 14:54:54 +05:30
const macuseragent =
"Mozilla/5.0 (Macintosh; Intel Mac OS X 12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36";
2022-03-31 14:54:54 +05:30
const linuxuseragent =
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36";
2022-03-31 14:54:54 +05:30
log.transports.file.level = "verbose";
console.log = log.log;
Object.assign(console, log.functions);
ElectronDl({
dlPath: "./downloads",
onStarted: (item) => {
BrowserWindow.getAllWindows().forEach((window) => {
window.setOverlayIcon(
__dirname + "/assets/icons/download.png",
"Downloading"
);
});
dialog.showMessageBox({
type: "info",
title: "Downloading File",
message: `Downloading "${item.getFilename()}" to "${item.getSavePath()}"`,
buttons: ["OK"],
});
},
onCompleted: () => {
BrowserWindow.getAllWindows().forEach((window) => {
window.setOverlayIcon(
__dirname + "/assets/icons/download-success.png",
"Download Successful"
);
});
dialog.showMessageBox({
type: "info",
title: "Download Completed",
message: `Downloading Completed! Please check your "Downloads" folder.`,
buttons: ["OK"],
});
setTimeout(() => {
BrowserWindow.getAllWindows().forEach((window) => {
window.setOverlayIcon(null, "");
});
}, 7000);
},
onError: (item) => {
dialog.showMessageBox({
type: "error",
title: "Download failed",
message: `Downloading "${item.getFilename()}" failed :(`,
buttons: ["OK"],
});
BrowserWindow.getAllWindows().forEach((window) => {
window.setOverlayIcon(
__dirname + "/download-fail.png",
"Download Failed"
);
});
setTimeout(() => {
BrowserWindow.getAllWindows().forEach((window) => {
window.setOverlayIcon(null, "");
});
}, 7000);
},
});
contextMenu({
showInspectElement: false,
showServices: false,
});
const menulayout = [
...(isMac
? [
{
label: app.name,
submenu: [
{ role: "about" },
{ type: "separator" },
{ role: "services" },
{ type: "separator" },
{ role: "hide" },
{ role: "hideothers" },
{ role: "unhide" },
{ type: "separator" },
{ role: "quit" },
],
},
]
: []),
{
label: "Application",
submenu: [
{
label: "About MS-Office-Electron",
click: () =>
openAboutWindow({
icon_path:
"https://raw.githubusercontent.com/agam778/MS-Office-Electron/main/assets/icon.png",
product_name: "MS-Office-Electron",
copyright:
2022-09-27 18:34:30 +05:30
"Copyright (c) 2021-2022 Agampreet Singh\nOffice, the name, website, images/icons\nare the intellectual properties of Microsoft.",
package_json_dir: __dirname,
bug_report_url:
"https://github.com/agam778/Microsoft-Office-Electron/issues/",
bug_link_text: "Report an issue",
adjust_window_size: "2",
show_close_button: "Close",
}),
},
{
label: "Check for Updates",
click: async () => {
axios
.get(
"https://api.github.com/repos/agam778/MS-Office-Electron/releases/latest"
)
.then((res) => {
let data = res.data;
let currentVersion = "v" + app.getVersion();
let latestVersion = data.tag_name;
if (currentVersion !== latestVersion) {
const updatedialog = dialog.showMessageBoxSync({
type: "info",
title: "Update Available",
message: `Your App's version: ${currentVersion}\nLatest version: ${latestVersion}\n\nPlease update to the latest version.`,
buttons: ["Download", "Close"],
});
if (updatedialog === 0) {
shell.openExternal(
"https://github.com/agam778/MS-Office-Electron/releases/latest"
);
}
} else {
dialog.showMessageBoxSync({
type: "info",
title: "No Update Available",
message: `Your App's version: ${currentVersion}\nLatest version: ${latestVersion}\n\nYou are already using the latest version.`,
buttons: ["OK"],
});
}
});
},
},
{
label: "Learn More",
click: async () => {
const { shell } = require("electron");
await shell.openExternal(
"https://github.com/agam778/MS-Office-Electron"
);
},
},
{
label: "Open Logs Folder",
click: async () => {
const { shell } = require("electron");
if (isWin) {
await shell.openPath(
"C:\\Users\\" +
process.env.USERNAME +
"\\AppData\\Roaming\\ms-office-electron\\logs\\"
);
} else if (process.platform === "darwin") {
await shell.openPath(
"/Users/" + process.env.USER + "/Library/Logs/ms-office-electron/"
);
} else if (isLinux) {
await shell.openPath(
"/home/" + process.env.USER + "/.config/ms-office-electron/logs/"
);
}
},
},
{ type: "separator" },
{
label: "Open Normal version of MS Office",
type: "radio",
click() {
2022-11-21 20:12:47 +05:30
store.set("enterprise-or-normal", "https://microsoft365.com/?auth=1");
2022-03-31 14:54:54 +05:30
dialog.showMessageBoxSync({
type: "info",
title: "Normal version of MS Office",
message:
"The normal version of MS Office will be opened.\n\nPlease restart the app to apply the changes.",
buttons: ["OK"],
});
},
checked:
2022-11-21 20:12:47 +05:30
store.get("enterprise-or-normal") ===
"https://microsoft365.com/?auth=1",
},
{
label: "Open Enterprise version of MS Office",
type: "radio",
click() {
2022-11-21 20:12:47 +05:30
store.set("enterprise-or-normal", "https://microsoft365.com/?auth=2");
2022-03-31 14:54:54 +05:30
dialog.showMessageBoxSync({
type: "info",
title: "Enterprise version of MS Office",
message:
"The enterprise version of MS Office will be opened.\n\nPlease restart the app to apply the changes.",
buttons: ["OK"],
});
},
checked:
2022-11-21 20:12:47 +05:30
store.get("enterprise-or-normal") ===
"https://microsoft365.com/?auth=2",
},
{ type: "separator" },
{
label: "Open Websites in New Windows (Recommended)",
type: "radio",
click: () => {
store.set("websites-in-new-window", "true");
dialog.showMessageBoxSync({
type: "info",
title: "Websites in New Windows",
message:
"Websites which are targeted to open in new tabs will now open in new windows.",
buttons: ["OK"],
});
},
checked: store.get("websites-in-new-window")
? store.get("websites-in-new-window") === "true"
: true,
},
{
label: "Open Websites in the Same Window",
type: "radio",
click: () => {
store.set("websites-in-new-window", "false");
dialog.showMessageBoxSync({
type: "info",
title: "Websites in New Windows",
message:
2022-03-31 14:54:54 +05:30
"Websites which are targeted to open in new tabs will now open in the same window.\n\nNote: This will be buggy in some cases if you are using Enterprise version of MS Office.",
buttons: ["OK"],
});
},
checked: store.get("websites-in-new-window")
? store.get("websites-in-new-window") === "false"
: false,
},
{ type: "separator" },
{
label: "Enable Discord Rich Presence",
type: "radio",
click: () => {
store.set("discordrpcstatus", "true");
dialog.showMessageBoxSync({
type: "info",
title: "Discord Rich Presence",
message: "Discord Rich Presence is now enabled.",
buttons: ["OK"],
});
discordrpcupdate(
`On "${BrowserWindow.getFocusedWindow().webContents.getTitle()}"`
);
},
checked: store.get("discordrpcstatus")
? store.get("discordrpcstatus") === "true"
: true,
},
{
label: "Disable Discord Rich Presence",
type: "radio",
click: () => {
store.set("discordrpcstatus", "false");
dialog.showMessageBoxSync({
type: "info",
title: "Discord Rich Presence",
message: "Discord Rich Presence is now disabled.",
buttons: ["OK"],
});
rpc.clearActivity();
},
checked: store.get("discordrpcstatus")
? store.get("discordrpcstatus") === "false"
: false,
},
{ type: "separator" },
2022-03-31 14:54:54 +05:30
{
label: "Windows Useragent",
type: "radio",
click: () => {
store.set("useragentstring", windowsuseragent);
dialog.showMessageBoxSync({
type: "info",
title: "User agent switcher",
message:
"You have switched to Windows Useragent.\n\nPlease restart the app to apply the changes.",
buttons: ["OK"],
});
},
checked:
store.get("useragentstring") === windowsuseragent ? true : false,
},
{
label: "Mac os Useragent",
type: "radio",
click: () => {
store.set("useragentstring", macuseragent);
dialog.showMessageBoxSync({
type: "info",
title: "User agent switcher",
message:
"You have switched to Mac OS Useragent.\n\nPlease restart the app to apply the changes.",
buttons: ["OK"],
});
},
checked: store.get("useragentstring") === macuseragent ? true : false,
},
{
label: "Linux Useragent",
type: "radio",
click: () => {
store.set("useragentstring", linuxuseragent);
dialog.showMessageBoxSync({
type: "info",
title: "User agent switcher",
message:
"You have switched to Linux Useragent.\n\nPlease restart the app to apply the changes.",
buttons: ["OK"],
});
},
checked: store.get("useragentstring") === linuxuseragent ? true : false,
},
{ type: "separator" },
{
role: "quit",
accelerator: process.platform === "darwin" ? "Ctrl+Q" : "Ctrl+Q",
},
],
},
{
label: "Navigation",
submenu: [
{
label: "Back",
click: () => {
BrowserWindow.getFocusedWindow().webContents.goBack();
},
},
{
label: "Forward",
click: () => {
BrowserWindow.getFocusedWindow().webContents.goForward();
},
},
{
label: "Reload",
click: () => {
BrowserWindow.getFocusedWindow().webContents.reload();
},
},
{
label: "Home",
click: () => {
BrowserWindow.getFocusedWindow().loadURL(
`${store.get("enterprise-or-normal")}`
);
},
},
],
},
{
label: "Edit",
submenu: [
{ role: "undo" },
{ role: "redo" },
{ type: "separator" },
{ role: "cut" },
{ role: "copy" },
{ role: "paste" },
...(isMac
? [
{ role: "pasteAndMatchStyle" },
{ role: "delete" },
{ role: "selectAll" },
{ type: "separator" },
{
label: "Speech",
submenu: [{ role: "startSpeaking" }, { role: "stopSpeaking" }],
},
]
: [{ role: "delete" }, { type: "separator" }, { role: "selectAll" }]),
],
},
{
label: "View",
submenu: [
{ role: "reload" },
{ role: "forceReload" },
{ type: "separator" },
{ role: "resetZoom" },
{
role: "zoomIn",
accelerator: process.platform === "darwin" ? "Control+=" : "Control+=",
},
{ role: "zoomOut" },
{ type: "separator" },
{ role: "togglefullscreen" },
],
},
{
label: "Window",
submenu: [
{ role: "minimize" },
{ role: "zoom" },
...(isMac
? [
{ type: "separator" },
{ role: "front" },
{ type: "separator" },
{ role: "window" },
]
: [{ role: "close" }]),
{
label: "Show Menu Bar",
type: "radio",
click: () => {
store.set("autohide-menubar", "false");
dialog.showMessageBoxSync({
type: "info",
title: "Menu Bar Settings",
message:
"Menu will be visible now. Please restart the app for changes to take effect.",
buttons: ["OK"],
});
},
checked: store.get("autohide-menubar") === "false",
},
{
label: "Hide Menu Bar (Press ALT To show for some time)",
type: "radio",
click: () => {
store.set("autohide-menubar", "true");
dialog.showMessageBoxSync({
type: "info",
title: "Menu Bar Settings",
message:
"Menu bar will be automatically hidden now. Please restart the app for changes to take effect.",
buttons: ["OK"],
});
},
checked: store.get("autohide-menubar") === "true",
},
],
},
];
const menu = Menu.buildFromTemplate(menulayout);
Menu.setApplicationMenu(menu);
function discordrpc(title) {
if (store.get("discordrpcstatus") === "true") {
rpc
.setActivity({
details: `${title}`,
largeImageKey: "logo",
largeImageText: "MS-Office-Electron",
startTimestamp: Date.now(),
instance: false,
})
.catch((err) => {
console.log(err);
});
}
}
function discordrpcupdate(title) {
rpc.clearActivity();
rpc
.setActivity({
details: `${title}`,
largeImageKey: "logo",
largeImageText: "MS-Office-Electron",
startTimestamp: Date.now(),
instance: false,
})
.catch((err) => {
console.log(err);
});
}
function createWindow() {
const win = new BrowserWindow({
width: 1181,
height: 670,
2022-10-07 17:31:10 +05:30
icon: path.join(__dirname, "/assets/icons/png/1024x1024.png"),
show: false,
webPreferences: {
nodeIntegration: true,
devTools: true,
},
});
if (store.get("autohide-menubar") === "true") {
win.setAutoHideMenuBar(true);
} else {
win.setAutoHideMenuBar(false);
}
const splash = new BrowserWindow({
width: 810,
height: 610,
transparent: true,
frame: false,
alwaysOnTop: true,
2022-10-07 17:31:10 +05:30
icon: path.join(__dirname, "/assets/icons/png/1024x1024.png"),
});
splash.loadURL(`https://agam778.github.io/MS-Office-Electron/loading`);
2022-03-31 14:54:54 +05:30
win.loadURL(
2022-11-21 20:12:47 +05:30
`${
store.get("enterprise-or-normal") || "https://microsoft365.com/?auth=1"
}`,
2022-03-31 14:54:54 +05:30
{
userAgent: store.get("useragentstring") || windowsuseragent,
}
);
win.webContents.on("did-finish-load", () => {
splash.destroy();
win.show();
discordrpc(`On "${win.webContents.getTitle()}"`);
});
}
app.on("ready", () => {
createWindow();
});
app.on("web-contents-created", (event, contents) => {
contents.on("new-window", (event, url) => {
if (store.get("websites-in-new-window") === "false") {
event.preventDefault();
BrowserWindow.getFocusedWindow().loadURL(url);
}
});
});
app.on("window-all-closed", () => {
rpc.clearActivity();
if (process.platform !== "darwin") {
app.quit();
}
});
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
app.on("ready", function () {
checkInternetConnected()
2022-09-24 22:58:52 +05:30
.then(() => {
console.log("You are connected to the internet!");
})
2022-09-24 22:58:52 +05:30
.catch(() => {
const options = {
type: "warning",
2022-09-24 22:58:52 +05:30
buttons: ["Ok"],
defaultId: 2,
title: "Warning",
message: "You appear to be offline!",
detail:
"Please check your Internet Connectivity. This app cannot run without an Internet Connection!",
};
dialog.showMessageBox(null, options, (response) => {
console.log(response);
});
});
autoUpdater.checkForUpdatesAndNotify();
if (store.get("discordrpcstatus") === "true") {
rpc.login({ clientId }).catch(() =>
dialog.showMessageBox(BrowserWindow.getFocusedWindow(), {
type: "error",
title: "Discord RPC Error",
message:
"Oops! An Error occured while connecting to Discord RPC. Probably discord isn't installed or opened?",
buttons: ["OK"],
})
);
}
});