Сделал открытие ссылок в браузере по умолчанию
This commit is contained in:
+34
-1
@@ -1,4 +1,4 @@
|
|||||||
import { app, BrowserWindow } from 'electron';
|
import { app, BrowserWindow, shell } from 'electron';
|
||||||
import path from 'node:path';
|
import path from 'node:path';
|
||||||
import started from 'electron-squirrel-startup';
|
import started from 'electron-squirrel-startup';
|
||||||
|
|
||||||
@@ -7,6 +7,38 @@ if (started) {
|
|||||||
app.quit();
|
app.quit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isAppNavigation(url) {
|
||||||
|
if (MAIN_WINDOW_VITE_DEV_SERVER_URL && url.startsWith(MAIN_WINDOW_VITE_DEV_SERVER_URL)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return url.startsWith('file://');
|
||||||
|
}
|
||||||
|
|
||||||
|
function openExternalLink(url) {
|
||||||
|
shell.openExternal(url).catch((error) => {
|
||||||
|
console.error('Failed to open external link:', url, error);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function configureExternalLinks(mainWindow) {
|
||||||
|
mainWindow.webContents.setWindowOpenHandler(({ url }) => {
|
||||||
|
openExternalLink(url);
|
||||||
|
return { action: 'deny' };
|
||||||
|
});
|
||||||
|
|
||||||
|
mainWindow.webContents.on('will-navigate', (event, url) => {
|
||||||
|
if (isAppNavigation(url)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (url.startsWith('http://') || url.startsWith('https://')) {
|
||||||
|
event.preventDefault();
|
||||||
|
openExternalLink(url);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const createWindow = () => {
|
const createWindow = () => {
|
||||||
const { screen } = require('electron');
|
const { screen } = require('electron');
|
||||||
const primaryDisplay = screen.getPrimaryDisplay();
|
const primaryDisplay = screen.getPrimaryDisplay();
|
||||||
@@ -22,6 +54,7 @@ const createWindow = () => {
|
|||||||
});
|
});
|
||||||
mainWindow.setMenuBarVisibility(false);
|
mainWindow.setMenuBarVisibility(false);
|
||||||
mainWindow.maximize();
|
mainWindow.maximize();
|
||||||
|
configureExternalLinks(mainWindow);
|
||||||
|
|
||||||
// and load the index.html of the app.
|
// and load the index.html of the app.
|
||||||
if (MAIN_WINDOW_VITE_DEV_SERVER_URL) {
|
if (MAIN_WINDOW_VITE_DEV_SERVER_URL) {
|
||||||
|
|||||||
Reference in New Issue
Block a user