From 898a1445f6771280daab09530bbbe1abf01629ec Mon Sep 17 00:00:00 2001 From: Oj Date: Sun, 13 Mar 2022 20:12:42 +0000 Subject: [PATCH] [Config] Initial Add --- src/bootstrap.js | 18 ++++++++------- src/config/index.js | 45 ++++++++++++++++++++++++++++++++++++ src/config/preload.js | 8 +++++++ src/mainWindowInject.js | 51 ++++++++++++++++++++++++++--------------- 4 files changed, 96 insertions(+), 26 deletions(-) create mode 100644 src/config/index.js create mode 100644 src/config/preload.js diff --git a/src/bootstrap.js b/src/bootstrap.js index 309f3c3..1c29f2c 100644 --- a/src/bootstrap.js +++ b/src/bootstrap.js @@ -59,6 +59,7 @@ const startCore = () => { bw.webContents.on('dom-ready', () => { if (!donePageReady) { // Only run once splashScreen.pageReady(); // Override Core's pageReady with our own on dom-ready to show main window earlier + donePageReady = true; } @@ -87,16 +88,17 @@ const startUpdate = async () => { desktopCore.setMainWindowVisible(!startMinimized); setTimeout(() => { // Try to update our asar - if (oaConfig.autoupdate === false) return; // If autoupdate disabled, don't update + const config = require('./config'); + if (oaConfig.setup !== true || process.argv.includes('--config')) config.open(); - const asarUpdate = require('./asarUpdate'); - - try { - asarUpdate(); - } catch (e) { - log('AsarUpdate', 'Failed', e); + if (oaConfig.autoupdate !== false) { // If autoupdate disabled, don't update + try { + require('./asarUpdate')(); + } catch (e) { + log('AsarUpdate', 'Failed', e); + } } - }, 1000); + }, 3000); }); }; diff --git a/src/config/index.js b/src/config/index.js new file mode 100644 index 0000000..abdb9a8 --- /dev/null +++ b/src/config/index.js @@ -0,0 +1,45 @@ +const { BrowserWindow, ipcMain, app } = require('electron'); +const { join } = require('path'); + +const settings = require('../appSettings').getSettings(); + +ipcMain.on('DISCORD_UPDATED_QUOTES', (e, c) => { + if (c === 'o') open(); +}); + +const open = exports.open = () => { + const win = new BrowserWindow({ + width: 500, + height: 650, + center: true, + frame: false, + resizable: false, + center: true, + backgroundColor: '#101418', + webPreferences: { + preload: join(__dirname, 'preload.js') + } + }); + + let config = settings.get('openasar', {}); + config.setup = true; + settings.save(); + + ipcMain.on('config_set', (e, c) => { + config = c; + settings.set('openasar', config); + settings.save(); // Ensure saving + }); + + ipcMain.on('config_get', (e) => { + e.returnValue = config; + }); + + ipcMain.on('config_restart', () => { + settings.save(); + app.relaunch({ args: process.argv.filter((x) => x !== '--config') }); + app.exit(); + }); + + win.loadURL('https://cdn.openasar.dev/config.html'); +}; \ No newline at end of file diff --git a/src/config/preload.js b/src/config/preload.js new file mode 100644 index 0000000..83857c7 --- /dev/null +++ b/src/config/preload.js @@ -0,0 +1,8 @@ +const { contextBridge, ipcRenderer } = require('electron'); + + +contextBridge.exposeInMainWorld('Native', { + restart: () => ipcRenderer.send('config_restart'), + set: c => ipcRenderer.send('config_set', c), + get: () => ipcRenderer.sendSync('config_get') +}); \ No newline at end of file diff --git a/src/mainWindowInject.js b/src/mainWindowInject.js index 5e186ae..3f49a1c 100644 --- a/src/mainWindowInject.js +++ b/src/mainWindowInject.js @@ -1,16 +1,23 @@ let lastBgPrimary = ''; const themesync = async () => { const getVar = (name, el = document.body) => el && (getComputedStyle(el).getPropertyValue(name) || getVar(name, el.parentElement))?.trim(); + const getFontSource = (font) => { + const san = (x) => x.replaceAll('\"', '').replaceAll("\'", ''); + const sanFont = san(font); + + return [...document.styleSheets].map((x) => !(x.href && x.href.includes('discord.com/assets')) && [...x.rules].find((y) => (y.cssText.startsWith('@font-face') && san(y.style.fontFamily) === sanFont) || (y.href?.includes?.(sanFont.replaceAll(' ', '+'))))).find((x) => x).cssText.replaceAll('\\"', '"'); + }; const bgPrimary = getVar('--background-primary'); if (!bgPrimary || bgPrimary === '#36393f' || bgPrimary === lastBgPrimary) return; // Default primary bg or same as last lastBgPrimary = bgPrimary; const vars = [ '--background-primary', '--background-secondary', '--brand-experiment', '--header-primary', '--text-muted' ]; + const font = getVar('font-family'); let cached = await DiscordNative.userDataCache.getCached() || {}; - const value = `body { ${vars.reduce((acc, x) => acc += `${x}: ${getVar(x)}; `, '')} }`; + const value = (!font.startsWith('Whitney,') ? getFontSource(font) : '') + ` body { ${vars.reduce((acc, x) => acc += `${x}: ${getVar(x)};`, '')} --font-primary: ${font}; }`; const pastValue = cached['openasarSplashCSS']; cached['openasarSplashCSS'] = value; @@ -21,29 +28,37 @@ setInterval(() => { try { themesync(); } catch (e) { } -}, 5000); +}, 10000); +themesync(); + +setInterval(() => { + const host = [...document.querySelectorAll('[class^="socialLinks-"] + [class^="info-"] [class^="colorMuted-"]')].find(x => x.textContent.startsWith('Host ')); + if (!host || document.querySelector('#openasar-ver')) return; + + const el = document.createElement('span'); + el.id = 'openasar-ver'; + + el.textContent = 'OpenAsar '; + el.onclick = () => DiscordNative.ipc.send('DISCORD_UPDATED_QUOTES', 'o'); + + host.append(document.createTextNode(' | '), el); +}, 2000); -const css = ` -[class^="socialLinks-"] + [class^="info-"] [class^="colorMuted-"]:nth-last-child(2)::after { - content: " | OpenAsar "; - display: inline; - text-transform: none; -} - -[class^="socialLinks-"] + [class^="info-"] { +const el = document.createElement('style'); +el.appendChild(document.createTextNode(`[class^="socialLinks-"] + [class^="info-"] { padding-right: 0; } -[class^="vertical-"] > div[style="display: flex; justify-content: space-between;"] > div > [class^="description-"] { - white-space: pre-wrap; - overflow-wrap: anywhere; +#openasar-ver { + text-transform: none; + cursor: pointer; } -`; -const el = document.createElement('style'); -el.appendChild(document.createTextNode(css)); +#openasar-ver:hover { + text-decoration: underline; + color: var(--text-normal); +}`)); document.body.appendChild(el); - -window.openasar = {}; \ No newline at end of file +openasar = {}; \ No newline at end of file