[Config] Initial Add

This commit is contained in:
Ducko 2022-03-13 20:12:42 +00:00
parent c510e44aaa
commit 898a1445f6
4 changed files with 96 additions and 26 deletions

18
src/bootstrap.js vendored
View file

@ -59,6 +59,7 @@ const startCore = () => {
bw.webContents.on('dom-ready', () => { bw.webContents.on('dom-ready', () => {
if (!donePageReady) { // Only run once if (!donePageReady) { // Only run once
splashScreen.pageReady(); // Override Core's pageReady with our own on dom-ready to show main window earlier splashScreen.pageReady(); // Override Core's pageReady with our own on dom-ready to show main window earlier
donePageReady = true; donePageReady = true;
} }
@ -87,16 +88,17 @@ const startUpdate = async () => {
desktopCore.setMainWindowVisible(!startMinimized); desktopCore.setMainWindowVisible(!startMinimized);
setTimeout(() => { // Try to update our asar 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'); if (oaConfig.autoupdate !== false) { // If autoupdate disabled, don't update
try {
try { require('./asarUpdate')();
asarUpdate(); } catch (e) {
} catch (e) { log('AsarUpdate', 'Failed', e);
log('AsarUpdate', 'Failed', e); }
} }
}, 1000); }, 3000);
}); });
}; };

45
src/config/index.js Normal file
View file

@ -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');
};

8
src/config/preload.js Normal file
View file

@ -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')
});

View file

@ -1,16 +1,23 @@
let lastBgPrimary = ''; let lastBgPrimary = '';
const themesync = async () => { const themesync = async () => {
const getVar = (name, el = document.body) => el && (getComputedStyle(el).getPropertyValue(name) || getVar(name, el.parentElement))?.trim(); 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'); const bgPrimary = getVar('--background-primary');
if (!bgPrimary || bgPrimary === '#36393f' || bgPrimary === lastBgPrimary) return; // Default primary bg or same as last if (!bgPrimary || bgPrimary === '#36393f' || bgPrimary === lastBgPrimary) return; // Default primary bg or same as last
lastBgPrimary = bgPrimary; lastBgPrimary = bgPrimary;
const vars = [ '--background-primary', '--background-secondary', '--brand-experiment', '--header-primary', '--text-muted' ]; const vars = [ '--background-primary', '--background-secondary', '--brand-experiment', '--header-primary', '--text-muted' ];
const font = getVar('font-family');
let cached = await DiscordNative.userDataCache.getCached() || {}; 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']; const pastValue = cached['openasarSplashCSS'];
cached['openasarSplashCSS'] = value; cached['openasarSplashCSS'] = value;
@ -21,29 +28,37 @@ setInterval(() => {
try { try {
themesync(); themesync();
} catch (e) { } } 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 <hash>';
el.onclick = () => DiscordNative.ipc.send('DISCORD_UPDATED_QUOTES', 'o');
host.append(document.createTextNode(' | '), el);
}, 2000);
const css = ` const el = document.createElement('style');
[class^="socialLinks-"] + [class^="info-"] [class^="colorMuted-"]:nth-last-child(2)::after { el.appendChild(document.createTextNode(`[class^="socialLinks-"] + [class^="info-"] {
content: " | OpenAsar <hash>";
display: inline;
text-transform: none;
}
[class^="socialLinks-"] + [class^="info-"] {
padding-right: 0; padding-right: 0;
} }
[class^="vertical-"] > div[style="display: flex; justify-content: space-between;"] > div > [class^="description-"] { #openasar-ver {
white-space: pre-wrap; text-transform: none;
overflow-wrap: anywhere; cursor: pointer;
} }
`;
const el = document.createElement('style'); #openasar-ver:hover {
el.appendChild(document.createTextNode(css)); text-decoration: underline;
color: var(--text-normal);
}`));
document.body.appendChild(el); document.body.appendChild(el);
openasar = {};
window.openasar = {};