From 9d2b63fedb88f3d79b20d237e281bbcbb2843267 Mon Sep 17 00:00:00 2001 From: Agampreet Singh <68941022+agam778@users.noreply.github.com> Date: Sun, 11 Jun 2023 21:15:36 +0530 Subject: [PATCH] Move everything related to store in store.js --- app/main.js | 28 +++++----- app/menu.js | 151 ++++++++++++++++++++++++--------------------------- app/rpc.js | 4 +- app/store.js | 19 ++++++- 4 files changed, 106 insertions(+), 96 deletions(-) diff --git a/app/main.js b/app/main.js index 79ec5ab..243feec 100644 --- a/app/main.js +++ b/app/main.js @@ -4,11 +4,11 @@ const checkInternetConnected = require("check-internet-connected"); const ElectronDl = require("electron-dl"); const contextMenu = require("electron-context-menu"); const path = require("path"); -const store = require("./store"); const log = require("electron-log"); const { setActivity, loginToRPC, clearActivity } = require("./rpc"); const useragents = require("./useragents.json"); const { ElectronBlocker } = require("@cliqz/adblocker-electron"); +const { getValue } = require("./store"); log.transports.file.level = "verbose"; console.log = log.log; @@ -92,7 +92,7 @@ function createWindow() { }, }); - if (store.get("autohide-menubar") === "true") { + if (getValue("autohide-menubar") === "true") { win.setAutoHideMenuBar(true); } else { win.setAutoHideMenuBar(false); @@ -110,10 +110,10 @@ function createWindow() { splash.loadURL(`https://agam778.github.io/MS-365-Electron/loading`); win.loadURL( `${ - store.get("enterprise-or-normal") || "https://microsoft365.com/?auth=1" + getValue("enterprise-or-normal") || "https://microsoft365.com/?auth=1" }`, { - userAgent: store.get("useragentstring") || useragents.Windows, + userAgent: getValue("useragentstring") || useragents.Windows, } ); @@ -122,15 +122,15 @@ function createWindow() { win.webContents.on("did-finish-load", () => { splash.destroy(); win.show(); - if (store.get("discordrpcstatus") === "true") { + if (getValue("discordrpcstatus") === "true") { setActivity(`On "${win.webContents.getTitle()}"`); } - if (store.get("blockads") === "true") { + if (getValue("blockads") === "true") { ElectronBlocker.fromPrebuiltAdsOnly(fetch).then((blocker) => { blocker.enableBlockingInSession(win.webContents.session); }); } - if (store.get("blockadsandtrackers") === "true") { + if (getValue("blockadsandtrackers") === "true") { ElectronBlocker.fromPrebuiltAdsAndTracking(fetch).then((blocker) => { blocker.enableBlockingInSession(win.webContents.session); }); @@ -144,12 +144,12 @@ app.on("ready", () => { app.on("web-contents-created", (event, contents) => { contents.setWindowOpenHandler(({ url }) => { - if (store.get("websites-in-new-window") === "false") { + if (getValue("websites-in-new-window") === "false") { if (url.includes("page=Download")) { return { action: "allow" }; } else { BrowserWindow.getFocusedWindow().loadURL(url); - if (store.get("discordrpcstatus") === "true") { + if (getValue("discordrpcstatus") === "true") { setActivity( `On "${BrowserWindow.getFocusedWindow().webContents.getTitle()}"` ); @@ -157,7 +157,7 @@ app.on("web-contents-created", (event, contents) => { return { action: "deny" }; } } else { - if (store.get("discordrpcstatus") === "true") { + if (getValue("discordrpcstatus") === "true") { setActivity( `On "${BrowserWindow.getFocusedWindow().webContents.getTitle()}"` ); @@ -301,16 +301,16 @@ app.on("web-contents-created", (event, contents) => { app.on("browser-window-created", (event, window) => { window.webContents.on("did-finish-load", () => { - if (store.get("discordrpcstatus") === "true") { + if (getValue("discordrpcstatus") === "true") { setActivity(`On "${window.webContents.getTitle()}"`); } }); - if (store.get("blockads") === "true") { + if (getValue("blockads") === "true") { ElectronBlocker.fromPrebuiltAdsOnly(fetch).then((blocker) => { blocker.enableBlockingInSession(window.webContents.session); }); } - if (store.get("blockadsandtrackers") === "true") { + if (getValue("blockadsandtrackers") === "true") { ElectronBlocker.fromPrebuiltAdsAndTracking(fetch).then((blocker) => { blocker.enableBlockingInSession(window.webContents.session); }); @@ -353,7 +353,7 @@ app.on("ready", function () { }); }); autoUpdater.checkForUpdatesAndNotify(); - if (store.get("discordrpcstatus") === "true") { + if (getValue("discordrpcstatus") === "true") { loginToRPC(); setActivity(`Opening Microsoft 365...`); } diff --git a/app/menu.js b/app/menu.js index 1e7d51f..0522d0d 100644 --- a/app/menu.js +++ b/app/menu.js @@ -1,4 +1,3 @@ -const store = require("./store"); const useragents = require("./useragents.json"); const { app, dialog, BrowserWindow } = require("electron"); const axios = require("axios"); @@ -9,15 +8,7 @@ const { ElectronBlocker } = require("@cliqz/adblocker-electron"); const fetch = require("cross-fetch"); const openAboutWindow = require("about-window").default; const path = require("path"); - -function getValueOrDefault(key, defaultValue) { - const value = store.get(key); - if (value === undefined) { - store.set(key, defaultValue); - return defaultValue; - } - return value; -} +const { getValue, setValue, getValueOrDefault } = require("./store"); async function checkForUpdates() { try { @@ -92,7 +83,7 @@ async function openLogsFolder() { } function setUserAgent(useragent) { - store.set("useragentstring", useragent); + setValue("useragentstring", useragent); const updatedialog = dialog.showMessageBoxSync({ type: "info", title: "User-Agent string changed", @@ -230,7 +221,7 @@ const menulayout = [ label: "Open Normal version of MS 365", type: "radio", click() { - store.set("enterprise-or-normal", "https://microsoft365.com/?auth=1"); + setValue("enterprise-or-normal", "https://microsoft365.com/?auth=1"); dialog.showMessageBoxSync({ type: "info", title: "Normal version of MS 365", @@ -240,14 +231,14 @@ const menulayout = [ }); }, checked: - store.get("enterprise-or-normal") === + getValue("enterprise-or-normal") === "https://microsoft365.com/?auth=1", }, { label: "Open Enterprise version of MS 365", type: "radio", click() { - store.set("enterprise-or-normal", "https://microsoft365.com/?auth=2"); + setValue("enterprise-or-normal", "https://microsoft365.com/?auth=2"); dialog.showMessageBoxSync({ type: "info", title: "Enterprise version of MS 365", @@ -257,7 +248,7 @@ const menulayout = [ }); }, checked: - store.get("enterprise-or-normal") === + getValue("enterprise-or-normal") === "https://microsoft365.com/?auth=2", }, { type: "separator" }, @@ -265,7 +256,7 @@ const menulayout = [ label: "Open Websites in New Windows (Recommended)", type: "radio", click: () => { - store.set("websites-in-new-window", "true"); + setValue("websites-in-new-window", "true"); dialog.showMessageBoxSync({ type: "info", title: "Websites in New Windows", @@ -274,15 +265,15 @@ const menulayout = [ buttons: ["OK"], }); }, - checked: store.get("websites-in-new-window") - ? store.get("websites-in-new-window") === "true" + checked: getValue("websites-in-new-window") + ? getValue("websites-in-new-window") === "true" : true, }, { label: "Open Websites in the Same Window", type: "radio", click: () => { - store.set("websites-in-new-window", "false"); + setValue("websites-in-new-window", "false"); dialog.showMessageBoxSync({ type: "info", title: "Websites in New Windows", @@ -291,8 +282,8 @@ const menulayout = [ buttons: ["OK"], }); }, - checked: store.get("websites-in-new-window") - ? store.get("websites-in-new-window") === "false" + checked: getValue("websites-in-new-window") + ? getValue("websites-in-new-window") === "false" : false, }, { type: "separator" }, @@ -300,8 +291,8 @@ const menulayout = [ label: "Enable Discord RPC", type: "checkbox", click: () => { - if (store.get("discordrpcstatus") === "true") { - store.set("discordrpcstatus", "false"); + if (getValue("discordrpcstatus") === "true") { + setValue("discordrpcstatus", "false"); dialog.showMessageBoxSync({ type: "info", title: "Discord RPC", @@ -311,10 +302,10 @@ const menulayout = [ clearActivity(); return; } else if ( - store.get("discordrpcstatus") === "false" || - store.get("discordrpcstatus") === undefined + getValue("discordrpcstatus") === "false" || + getValue("discordrpcstatus") === undefined ) { - store.set("discordrpcstatus", "true"); + setValue("discordrpcstatus", "true"); dialog.showMessageBoxSync({ type: "info", title: "Discord RPC", @@ -327,22 +318,22 @@ const menulayout = [ return; } }, - checked: store.get("discordrpcstatus") === "true", + checked: getValue("discordrpcstatus") === "true", }, { type: "separator" }, { label: "Block Ads", type: "checkbox", click: () => { - if (store.get("blockads") === "true") { - store.set("blockads", "false"); + if (getValue("blockads") === "true") { + setValue("blockads", "false"); dialog.showMessageBoxSync({ type: "info", title: "Block Ads", message: "Ads will no longer be blocked.", buttons: ["OK"], }); - if (!store.get("blockadsandtrackers") === "true") { + if (!getValue("blockadsandtrackers") === "true") { ElectronBlocker.fromPrebuiltAdsOnly(fetch).then((blocker) => blocker.disableBlockingInSession( BrowserWindow.getFocusedWindow().webContents.session @@ -352,10 +343,10 @@ const menulayout = [ return; } if ( - store.get("blockads") === "false" || - store.get("blockads") === undefined + getValue("blockads") === "false" || + getValue("blockads") === undefined ) { - store.set("blockads", "true"); + setValue("blockads", "true"); ElectronBlocker.fromPrebuiltAdsOnly(fetch).then((blocker) => blocker.enableBlockingInSession( BrowserWindow.getFocusedWindow().webContents.session @@ -371,21 +362,21 @@ const menulayout = [ return; } }, - checked: store.get("blockads") === "true", + checked: getValue("blockads") === "true", }, { label: "Block Ads and Trackers", type: "checkbox", click: () => { - if (store.get("blockadsandtrackers") === "true") { - store.set("blockadsandtrackers", "false"); + if (getValue("blockadsandtrackers") === "true") { + setValue("blockadsandtrackers", "false"); dialog.showMessageBoxSync({ type: "info", title: "Block Ads and Trackers", message: "Ads and trackers will no longer be blocked.", buttons: ["OK"], }); - if (!store.get("blockads") === "true") { + if (!getValue("blockads") === "true") { ElectronBlocker.fromPrebuiltAdsAndTracking(fetch).then( (blocker) => { blocker.disableBlockingInSession( @@ -397,10 +388,10 @@ const menulayout = [ return; } if ( - store.get("blockadsandtrackers") === "false" || - store.get("blockadsandtrackers") === undefined + getValue("blockadsandtrackers") === "false" || + getValue("blockadsandtrackers") === undefined ) { - store.set("blockadsandtrackers", "true"); + setValue("blockadsandtrackers", "true"); ElectronBlocker.fromPrebuiltAdsAndTracking(fetch).then( (blocker) => { blocker.enableBlockingInSession( @@ -417,7 +408,7 @@ const menulayout = [ return; } }, - checked: store.get("blockadsandtrackers") === "true", + checked: getValue("blockadsandtrackers") === "true", }, { type: "separator" }, { @@ -426,7 +417,7 @@ const menulayout = [ click: () => { setUserAgent(useragents.Windows); }, - checked: store.get("useragentstring") === useragents.Windows, + checked: getValue("useragentstring") === useragents.Windows, }, { label: "macOS User-Agent String", @@ -434,13 +425,13 @@ const menulayout = [ click: () => { setUserAgent(useragents.macOS); }, - checked: store.get("useragentstring") === useragents.macOS, + checked: getValue("useragentstring") === useragents.macOS, }, { label: "Linux User-Agent String", type: "radio", click: () => { - store.set("useragentstring", useragents.Linux); + setValue("useragentstring", useragents.Linux); dialog.showMessageBoxSync({ type: "info", title: "User agent switcher", @@ -449,7 +440,7 @@ const menulayout = [ buttons: ["OK"], }); }, - checked: store.get("useragentstring") === useragents.Linux, + checked: getValue("useragentstring") === useragents.Linux, }, { type: "separator" }, ...(!process.platform === "darwin" @@ -469,10 +460,10 @@ const menulayout = [ label: "Word", click: () => { if ( - store.get("enterprise-or-normal") === + getValue("enterprise-or-normal") === "https://microsoft365.com/?auth=2" ) { - if (store.get("websites-in-new-window") === "true") { + if (getValue("websites-in-new-window") === "true") { let wordwindow = new BrowserWindow({ width: 800, height: 600, @@ -488,10 +479,10 @@ const menulayout = [ ); } } else if ( - store.get("enterprise-or-normal") === + getValue("enterprise-or-normal") === "https://microsoft365.com/?auth=1" ) { - if (store.get("websites-in-new-window") === "true") { + if (getValue("websites-in-new-window") === "true") { let wordwindow = new BrowserWindow({ width: 800, height: 600, @@ -513,10 +504,10 @@ const menulayout = [ label: "Excel", click: () => { if ( - store.get("enterprise-or-normal") === + getValue("enterprise-or-normal") === "https://microsoft365.com/?auth=2" ) { - if (store.get("websites-in-new-window") === "true") { + if (getValue("websites-in-new-window") === "true") { let excelwindow = new BrowserWindow({ width: 800, height: 600, @@ -534,10 +525,10 @@ const menulayout = [ ); } } else if ( - store.get("enterprise-or-normal") === + getValue("enterprise-or-normal") === "https://microsoft365.com/?auth=1" ) { - if (store.get("websites-in-new-window") === "true") { + if (getValue("websites-in-new-window") === "true") { let excelwindow = new BrowserWindow({ width: 800, height: 600, @@ -561,10 +552,10 @@ const menulayout = [ label: "PowerPoint", click: () => { if ( - store.get("enterprise-or-normal") === + getValue("enterprise-or-normal") === "https://microsoft365.com/?auth=2" ) { - if (store.get("websites-in-new-window") === "true") { + if (getValue("websites-in-new-window") === "true") { let powerpointwindow = new BrowserWindow({ width: 800, height: 600, @@ -582,10 +573,10 @@ const menulayout = [ ); } } else if ( - store.get("enterprise-or-normal") === + getValue("enterprise-or-normal") === "https://microsoft365.com/?auth=1" ) { - if (store.get("websites-in-new-window") === "true") { + if (getValue("websites-in-new-window") === "true") { let powerpointwindow = new BrowserWindow({ width: 800, height: 600, @@ -609,10 +600,10 @@ const menulayout = [ label: "Outlook", click: () => { if ( - store.get("enterprise-or-normal") === + getValue("enterprise-or-normal") === "https://microsoft365.com/?auth=2" ) { - if (store.get("websites-in-new-window") === "true") { + if (getValue("websites-in-new-window") === "true") { let outlookwindow = new BrowserWindow({ width: 800, height: 600, @@ -628,10 +619,10 @@ const menulayout = [ ); } } else if ( - store.get("enterprise-or-normal") === + getValue("enterprise-or-normal") === "https://microsoft365.com/?auth=1" ) { - if (store.get("websites-in-new-window") === "true") { + if (getValue("websites-in-new-window") === "true") { let outlookwindow = new BrowserWindow({ width: 800, height: 600, @@ -655,10 +646,10 @@ const menulayout = [ label: "OneDrive", click: () => { if ( - store.get("enterprise-or-normal") === + getValue("enterprise-or-normal") === "https://microsoft365.com/?auth=2" ) { - if (store.get("websites-in-new-window") === "true") { + if (getValue("websites-in-new-window") === "true") { let onedrivewindow = new BrowserWindow({ width: 800, height: 600, @@ -676,10 +667,10 @@ const menulayout = [ ); } } else if ( - store.get("enterprise-or-normal") === + getValue("enterprise-or-normal") === "https://microsoft365.com/?auth=1" ) { - if (store.get("websites-in-new-window") === "true") { + if (getValue("websites-in-new-window") === "true") { let onedrivewindow = new BrowserWindow({ width: 800, height: 600, @@ -688,7 +679,9 @@ const menulayout = [ contextIsolation: true, }, }); - onedrivewindow.loadURL("https://microsoft365.com/launch/onedrive?auth=1"); + onedrivewindow.loadURL( + "https://microsoft365.com/launch/onedrive?auth=1" + ); } else { BrowserWindow.getFocusedWindow().loadURL( "https://microsoft365.com/launch/onedrive?auth=1" @@ -701,10 +694,10 @@ const menulayout = [ label: "OneNote", click: () => { if ( - store.get("enterprise-or-normal") === + getValue("enterprise-or-normal") === "https://microsoft365.com/?auth=2" ) { - if (store.get("websites-in-new-window") === "true") { + if (getValue("websites-in-new-window") === "true") { let onenotewindow = new BrowserWindow({ width: 800, height: 600, @@ -722,10 +715,10 @@ const menulayout = [ ); } } else if ( - store.get("enterprise-or-normal") === + getValue("enterprise-or-normal") === "https://microsoft365.com/?auth=1" ) { - if (store.get("websites-in-new-window") === "true") { + if (getValue("websites-in-new-window") === "true") { let onenotewindow = new BrowserWindow({ width: 800, height: 600, @@ -747,10 +740,10 @@ const menulayout = [ label: "All Apps", click: () => { if ( - store.get("enterprise-or-normal") === + getValue("enterprise-or-normal") === "https://microsoft365.com/?auth=2" ) { - if (store.get("websites-in-new-window") === "true") { + if (getValue("websites-in-new-window") === "true") { let allappswindow = new BrowserWindow({ width: 800, height: 600, @@ -766,10 +759,10 @@ const menulayout = [ ); } } else if ( - store.get("enterprise-or-normal") === + getValue("enterprise-or-normal") === "https://microsoft365.com/?auth=1" ) { - if (store.get("websites-in-new-window") === "true") { + if (getValue("websites-in-new-window") === "true") { let allappswindow = new BrowserWindow({ width: 800, height: 600, @@ -814,7 +807,7 @@ const menulayout = [ label: "Home", click: () => { BrowserWindow.getFocusedWindow().loadURL( - `${store.get("enterprise-or-normal")}` + `${getValue("enterprise-or-normal")}` ); }, }, @@ -874,7 +867,7 @@ const menulayout = [ label: "Show Menu Bar", type: "radio", click: () => { - store.set("autohide-menubar", "false"); + setValue("autohide-menubar", "false"); dialog.showMessageBoxSync({ type: "info", title: "Menu Bar Settings", @@ -883,13 +876,13 @@ const menulayout = [ buttons: ["OK"], }); }, - checked: store.get("autohide-menubar") === "false", + checked: getValue("autohide-menubar") === "false", }, { label: "Hide Menu Bar (ALT to show)", type: "radio", click: () => { - store.set("autohide-menubar", "true"); + setValue("autohide-menubar", "true"); dialog.showMessageBoxSync({ type: "info", title: "Menu Bar Settings", @@ -898,7 +891,7 @@ const menulayout = [ buttons: ["OK"], }); }, - checked: store.get("autohide-menubar") === "true", + checked: getValue("autohide-menubar") === "true", }, ] : []), diff --git a/app/rpc.js b/app/rpc.js index d6cb94a..e2e9b8c 100644 --- a/app/rpc.js +++ b/app/rpc.js @@ -1,6 +1,6 @@ const { Client } = require("@xhayper/discord-rpc"); const { dialog, BrowserWindow } = require("electron"); -const store = require("./store"); +const { setValue } = require("./store"); const client = new Client({ clientId: "942637872530460742", @@ -15,7 +15,7 @@ async function rpcError(status) { }); if (rpcerror === 1) { - store.set("discordrpcstatus", "false"); + setValue("discordrpcstatus", "false"); } } diff --git a/app/store.js b/app/store.js index 3163f83..3e9888a 100644 --- a/app/store.js +++ b/app/store.js @@ -1,4 +1,21 @@ const Store = require("electron-store"); const store = new Store(); -module.exports = store; +function getValue(key) { + return store.get(key); +} + +function setValue(key, value) { + store.set(key, value); +} + +function getValueOrDefault(key, defaultValue) { + const value = store.get(key); + if (value === undefined) { + store.set(key, defaultValue); + return defaultValue; + } + return value; +} + +module.exports = { getValue, setValue, getValueOrDefault };