66 lines
1.7 KiB
JavaScript
66 lines
1.7 KiB
JavaScript
const { app, BrowserWindow, nativeImage } = require('electron');
|
|
const path = require('path');
|
|
const axios = require('axios');
|
|
const argv = require('yargs')
|
|
.option('url', {
|
|
alias: 'u',
|
|
description: 'URL to open',
|
|
type: 'string',
|
|
})
|
|
.argv;
|
|
|
|
function createWindow() {
|
|
const mainWindow = new BrowserWindow({
|
|
width: 800,
|
|
height: 600,
|
|
webPreferences: {
|
|
nodeIntegration: false,
|
|
contextIsolation: true,
|
|
},
|
|
autoHideMenuBar: true,
|
|
title: 'any-app',
|
|
});
|
|
|
|
const targetUrl = argv.url || `file://${path.join(__dirname, 'index.html')}`;
|
|
|
|
mainWindow.loadURL(targetUrl);
|
|
|
|
mainWindow.webContents.on('did-finish-load', async () => {
|
|
try {
|
|
const faviconUrl = await mainWindow.webContents.executeJavaScript(`
|
|
(() => {
|
|
const link = document.querySelector('link[rel~="icon"]');
|
|
return link ? link.href : '';
|
|
})()
|
|
`);
|
|
if (faviconUrl) {
|
|
const response = await axios.get(faviconUrl, { responseType: 'arraybuffer' });
|
|
const icon = nativeImage.createFromBuffer(Buffer.from(response.data, 'binary'));
|
|
mainWindow.setIcon(icon);
|
|
}
|
|
} catch (error) {
|
|
console.error('Failed to set favicon:', error);
|
|
}
|
|
});
|
|
|
|
mainWindow.on('closed', () => {
|
|
app.quit();
|
|
});
|
|
|
|
mainWindow.maximize();
|
|
}
|
|
|
|
app.on('ready', createWindow);
|
|
|
|
app.on('window-all-closed', () => {
|
|
if (process.platform !== 'darwin') {
|
|
app.quit();
|
|
}
|
|
});
|
|
|
|
app.on('activate', () => {
|
|
if (BrowserWindow.getAllWindows().length === 0) {
|
|
createWindow();
|
|
}
|
|
});
|