diff --git a/app/dimensions.js b/app/config/dimensions.js similarity index 70% rename from app/dimensions.js rename to app/config/dimensions.js index acede9e..f1e4f8a 100644 --- a/app/dimensions.js +++ b/app/config/dimensions.js @@ -1,16 +1,14 @@ import { app, screen } from "electron"; - let screenWidth, screenHeight; + app.on("ready", () => { ({ width: screenWidth, height: screenHeight } = screen.getPrimaryDisplay().workAreaSize); }); -function getScreenWidth() { +export function getScreenWidth() { return screenWidth; } -function getScreenHeight() { +export function getScreenHeight() { return screenHeight; } - -export { getScreenWidth, getScreenHeight }; diff --git a/app/menu.js b/app/config/menu.js similarity index 91% rename from app/menu.js rename to app/config/menu.js index 2f6e80d..0a0cb38 100644 --- a/app/menu.js +++ b/app/config/menu.js @@ -1,97 +1,12 @@ -import { app, dialog, BrowserWindow, ShareMenu, clipboard } from "electron"; +import { dialog, BrowserWindow, ShareMenu, clipboard } from "electron"; import { getValue, setValue } from "./store.js"; import { ElectronBlocker } from "@cliqz/adblocker-electron"; import { clearActivity, setActivity } from "./rpc.js"; import prompt from "electron-prompt"; -import { fileURLToPath } from "url"; -import { shell } from "electron"; -import { dirname } from "path"; import { getScreenWidth, getScreenHeight } from "./dimensions.js"; -import useragents from "./useragents.json" with { type: "json" }; -import updaterpkg from "electron-updater"; -import fetch from "cross-fetch"; -import axios from "axios"; - -const __filename = fileURLToPath(import.meta.url); -const __dirname = dirname(__filename); -const { autoUpdater } = updaterpkg; - -async function checkForUpdates() { - try { - const res = await axios.get( - "https://api.github.com/repos/agam778/MS-365-Electron/releases/latest" - ); - const data = res.data; - const currentVersion = "v" + app.getVersion(); - const latestVersion = data.tag_name; - - if (currentVersion !== latestVersion) { - if (process.platform === "win32" || process.platform === "darwin") { - autoUpdater.checkForUpdatesAndNotify().then((result) => { - if (result === null) { - dialog.showMessageBoxSync({ - type: "info", - title: "No Update Available", - message: `Current version: ${currentVersion}\nLatest version: ${latestVersion}\n\nYou are already using the latest version.`, - buttons: ["OK"], - }); - } - }); - return; - } else { - const updatedialog = dialog.showMessageBoxSync({ - type: "info", - title: "Update Available", - message: `Current 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-365-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"], - }); - } - } catch (error) { - console.error("Error checking for updates:", error); - } -} - -async function openExternalLink(url) { - await shell.openExternal(url); -} - -async function openLogsFolder() { - if (process.platform === "win32") { - await shell.openPath( - "C:\\Users\\" + process.env.USERNAME + "\\AppData\\Roaming\\ms-365-electron\\logs\\" - ); - } else if (process.platform === "darwin") { - await shell.openPath("/Users/" + process.env.USER + "/Library/Logs/ms-365-electron/"); - } else if (process.platform === "linux") { - await shell.openPath("/home/" + process.env.USER + "/.config/ms-365-electron/logs/"); - } -} - -function setUserAgent(useragent) { - setValue("useragentstring", useragent); - const updatedialog = dialog.showMessageBoxSync({ - type: "info", - title: "User-Agent string changed", - message: `You have switched to the ${useragent} User-Agent string.\n\nPlease restart the app for the changes to take effect.`, - buttons: ["Later", "Restart"], - }); - if (updatedialog === 1) { - app.relaunch(); - app.exit(); - } -} +import { checkForUpdates, openExternalLink, openLogsFolder, setUserAgent } from "./utils.js"; +import useragents from "../useragents.json" with { type: "json" }; const commonPreferencesSubmenu = [ { diff --git a/app/rpc.js b/app/config/rpc.js similarity index 100% rename from app/rpc.js rename to app/config/rpc.js diff --git a/app/store.js b/app/config/store.js similarity index 94% rename from app/store.js rename to app/config/store.js index d8380fd..9067d3a 100644 --- a/app/store.js +++ b/app/config/store.js @@ -1,4 +1,4 @@ -import useragents from "./useragents.json" with { type: "json" }; +import useragents from "../useragents.json" with { type: "json" }; import Store from "electron-store"; const store = new Store(); diff --git a/app/config/utils.js b/app/config/utils.js new file mode 100644 index 0000000..104961f --- /dev/null +++ b/app/config/utils.js @@ -0,0 +1,82 @@ +import { app, dialog, shell } from "electron"; +import axios from "axios"; +import { setValue } from "./store.js"; +import updaterpkg from "electron-updater"; + +const { autoUpdater } = updaterpkg; + +export async function checkForUpdates() { + try { + const res = await axios.get( + "https://api.github.com/repos/agam778/MS-365-Electron/releases/latest" + ); + const data = res.data; + const currentVersion = "v" + app.getVersion(); + const latestVersion = data.tag_name; + + if (currentVersion !== latestVersion) { + if (process.platform === "win32" || process.platform === "darwin") { + autoUpdater.checkForUpdatesAndNotify().then((result) => { + if (result === null) { + dialog.showMessageBoxSync({ + type: "info", + title: "No Update Available", + message: `Current version: ${currentVersion}\nLatest version: ${latestVersion}\n\nYou are already using the latest version.`, + buttons: ["OK"], + }); + } + }); + return; + } else { + const updatedialog = dialog.showMessageBoxSync({ + type: "info", + title: "Update Available", + message: `Current 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-365-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"], + }); + } + } catch (error) { + console.error("Error checking for updates:", error); + } +} + +export async function openExternalLink(url) { + await shell.openExternal(url); +} + +export async function openLogsFolder() { + if (process.platform === "win32") { + await shell.openPath( + "C:\\Users\\" + process.env.USERNAME + "\\AppData\\Roaming\\ms-365-electron\\logs\\" + ); + } else if (process.platform === "darwin") { + await shell.openPath("/Users/" + process.env.USER + "/Library/Logs/ms-365-electron/"); + } else if (process.platform === "linux") { + await shell.openPath("/home/" + process.env.USER + "/.config/ms-365-electron/logs/"); + } +} + +export function setUserAgent(useragent) { + setValue("useragentstring", useragent); + const updatedialog = dialog.showMessageBoxSync({ + type: "info", + title: "User-Agent string changed", + message: `You have switched to the ${useragent} User-Agent string.\n\nPlease restart the app for the changes to take effect.`, + buttons: ["Later", "Restart"], + }); + if (updatedialog === 1) { + app.relaunch(); + app.exit(); + } +} diff --git a/app/main.js b/app/main.js index 16ce99e..3de15e8 100644 --- a/app/main.js +++ b/app/main.js @@ -1,19 +1,19 @@ import { app, Menu, BrowserWindow, dialog, nativeImage, screen } from "electron"; -import { clearActivity, setActivity, loginToRPC } from "./rpc.js"; +import { clearActivity, setActivity, loginToRPC } from "./config/rpc.js"; import { initialize, trackEvent } from "@aptabase/electron/main"; import { ElectronBlocker } from "@cliqz/adblocker-electron"; -import { getValue } from "./store.js"; +import { getValue } from "./config/store.js"; import { fileURLToPath } from "url"; import { dirname } from "path"; import { join } from "path"; -import { getScreenWidth, getScreenHeight } from "./dimensions.js"; +import { getScreenWidth, getScreenHeight } from "./config/dimensions.js"; import Windows from "./useragents.json" with { type: "json" }; import checkInternetConnected from "check-internet-connected"; import contextMenu from "electron-context-menu"; import updaterpkg from "electron-updater"; import ElectronDl from "electron-dl"; -import menulayout from "./menu.js"; +import menulayout from "./config/menu.js"; import logpkg from "electron-log"; const { transports, log: _log, functions } = logpkg; @@ -122,11 +122,7 @@ app.on("web-contents-created", (event, contents) => { if (url.includes("page=Download")) { return { action: "allow" }; } else { - BrowserWindow.getFocusedWindow() - .loadURL(url) - .catch((err) => { - // do not show error - }); + BrowserWindow.getFocusedWindow().loadURL(url).catch(); if (getValue("discordrpcstatus") === "true") { setActivity(`On "${BrowserWindow.getFocusedWindow().webContents.getTitle()}"`); }