From 6f0f88753cb1361059d53eb03d36be7970c1ac4b Mon Sep 17 00:00:00 2001 From: jdev082 Date: Mon, 13 May 2024 16:25:16 -0500 Subject: [PATCH] feat(themes): support for installing themes online Signed-off-by: jdev082 --- main/main.js | 17 +++++++++++++++++ main/preload.js | 3 +++ src/search.js | 8 ++++++++ 3 files changed, 28 insertions(+) diff --git a/main/main.js b/main/main.js index 5db33fc2..7c085aca 100644 --- a/main/main.js +++ b/main/main.js @@ -5,6 +5,7 @@ const { app, BrowserWindow, dialog, Menu, session, ipcMain, electron } = require const path = require('path'); const fs = require('fs'); const fetch = require('cross-fetch'); +const https = require('https'); const contextMenu = require('electron-context-menu'); if (require('electron-squirrel-startup')) app.quit(); @@ -191,6 +192,22 @@ ipcMain.handle('get-themes', async (event) => { return buf; }); +function download(url, dest, cb) { + var file = fs.createWriteStream(dest); + https.get(url, function (response) { + response.pipe(file); + file.on('finish', function () { + file.close(cb); + }); + }); +} + +ipcMain.handle('download-theme', async (event, url, name) => { + download(url, `${app.getPath('userData')}/themes/${name}`, () => { + return; + }) +}); + ipcMain.handle('toggle-full-screen', async (event) => { mainWindow.setFullScreen(!mainWindow.isFullScreen()); }); diff --git a/main/preload.js b/main/preload.js index 305d67a7..b770f3a5 100644 --- a/main/preload.js +++ b/main/preload.js @@ -49,6 +49,9 @@ contextBridge.exposeInMainWorld('native', { } ) }, + downloadTheme: (url, name) => { + ipcRenderer.invoke('download-theme', url, name); + }, unloadTheme: () => { document.getElementsByClassName('theme')[0].remove() }, diff --git a/src/search.js b/src/search.js index 5b71422f..7f6ec038 100644 --- a/src/search.js +++ b/src/search.js @@ -63,6 +63,14 @@ function loadURL(url) { if ( url.startsWith('http://') ) { alert(`Page ${url} is not secure.`); } + if (url.startsWith('https://raw.githubusercontent.com/CatalystDevOrg/Themes/master/') && url.endsWith('.css')) { + if (confirm("This link looks like a theme URL. Attempt to install theme?")) { + catalyst.native.downloadTheme(url, url.split("/")[6]) + if (confirm(`Theme ${url.split("/")[6]} installed successfully! Switch to theme?`)) { + catalyst.native.loadTheme(url.split("/")[6]) + }; + } + } view.src = url; view.addEventListener('did-fail-load', () => { view.src = 'home.html';