diff --git a/desktoputil.js b/desktoputil.js new file mode 100644 index 0000000..2b02b1b --- /dev/null +++ b/desktoputil.js @@ -0,0 +1,27 @@ +window.desktopUtil = class desktopUtil { + + static async authFetch(url, options = {}) { + const token = localStorage.getItem('auth_token'); + + return fetch(url, { + ...options, + headers: { + ...options.headers, + 'Authorization': `Bearer ${token}`, + 'X-Client': 'desktop' + } + }); + } + + static async removeAuthToken() { + localStorage.removeItem('auth_token'); + } + + static async setAuthToken(token) { + localStorage.setItem('auth_token', token); + } + + static async getAuthToken() { + return localStorage.getItem('auth_token'); + } +} \ No newline at end of file diff --git a/index.html b/index.html index a176ae7..92d2d53 100644 --- a/index.html +++ b/index.html @@ -5,6 +5,10 @@ Parchment + + + + + +
diff --git a/index.js b/index.js index 635906e..e9321fa 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,9 @@ -import { app, BrowserWindow, globalShortcut } from 'electron'; +import { app, BrowserWindow, globalShortcut, ipcMain, protocol, net } from 'electron'; +import path from 'path' +import fs from 'fs' +import { fileURLToPath } from "url"; +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); const WINDOW_SHORTCUTS = [ process.platform === "darwin" ? "Command+R" : "Control+R", @@ -9,6 +14,7 @@ class App { devToolsOpened = false; createWindow({onTop = false}) { + const win = new BrowserWindow({ width: 1200, height: 800, @@ -16,6 +22,7 @@ class App { show: false, titleBarStyle: "customButtonsOnHover", webPreferences: { + preload: path.join(__dirname, 'preload.js'), contextIsolation: true, nodeIntegration: false, webviewTag: true, @@ -27,7 +34,22 @@ class App { win.setAlwaysOnTop(true, 'screen-saver', 1); // necessary so it doesn't bring you back out of full screen when spawned } - win.loadFile('index.html'); + protocol.handle('app', (request) => { + const url = new URL(request.url); + let filePath = path.join(__dirname, url.pathname); + + // if file doesn't exist, fall back to index.html + if(filePath.includes("desktoputil")) { + filePath = path.join(__dirname, 'desktoputil.js'); + } + else if (!fs.existsSync(filePath) || fs.statSync(filePath).isDirectory()) { + filePath = path.join(__dirname, 'index.html'); + } + + return net.fetch(`file://${filePath}`); + }); + + win.loadURL('app://local/'); win.on("focus", () => { win.setVibrancy("appearance-based"); // full colors @@ -92,10 +114,9 @@ class App { } }); } + constructor() { - console.log("command line: ", app.commandLine.getSwitchValue('disable-gpu')); // or check flags - app.on("activate", () => { if (BrowserWindow.getAllWindows().length === 0) { this.createWindow({onTop: false}); @@ -110,6 +131,20 @@ class App { this.createWindow({onTop: false}); this.registerShortcuts() }); + + ipcMain.on('focus-window', () => { + const win = BrowserWindow.getAllWindows()[0]; + win?.show(); + win?.focus(); + }); + + ipcMain.on('set-badge', (e, count) => { + app.setBadgeCount(count); + }); + + protocol.registerSchemesAsPrivileged([ + { scheme: 'app', privileges: { standard: true, secure: true, supportFetchAPI: true, corsEnabled: true } } + ]); } } diff --git a/preload.js b/preload.js new file mode 100644 index 0000000..ce2a64f --- /dev/null +++ b/preload.js @@ -0,0 +1,6 @@ +const { contextBridge, ipcRenderer } = require('electron'); + +contextBridge.exposeInMainWorld('electron', { + focusWindow: () => ipcRenderer.send('focus-window'), + setBadge: (count) => ipcRenderer.send('set-badge', count), +}); \ No newline at end of file