fix(electron): do not open external links in the browser for internal links in electron (#5997)
This commit is contained in:
@@ -1,6 +1,14 @@
|
|||||||
import { app, shell } from 'electron';
|
import { app, shell } from 'electron';
|
||||||
|
|
||||||
app.on('web-contents-created', (_, contents) => {
|
app.on('web-contents-created', (_, contents) => {
|
||||||
|
const isInternalUrl = (url: string) => {
|
||||||
|
return (
|
||||||
|
(process.env.DEV_SERVER_URL &&
|
||||||
|
url.startsWith(process.env.DEV_SERVER_URL)) ||
|
||||||
|
url.startsWith('affine://') ||
|
||||||
|
url.startsWith('file://.')
|
||||||
|
);
|
||||||
|
};
|
||||||
/**
|
/**
|
||||||
* Block navigation to origins not on the allowlist.
|
* Block navigation to origins not on the allowlist.
|
||||||
*
|
*
|
||||||
@@ -10,12 +18,7 @@ app.on('web-contents-created', (_, contents) => {
|
|||||||
* @see https://www.electronjs.org/docs/latest/tutorial/security#13-disable-or-limit-navigation
|
* @see https://www.electronjs.org/docs/latest/tutorial/security#13-disable-or-limit-navigation
|
||||||
*/
|
*/
|
||||||
contents.on('will-navigate', (event, url) => {
|
contents.on('will-navigate', (event, url) => {
|
||||||
if (
|
if (isInternalUrl(url)) {
|
||||||
(process.env.DEV_SERVER_URL &&
|
|
||||||
url.startsWith(process.env.DEV_SERVER_URL)) ||
|
|
||||||
url.startsWith('affine://') ||
|
|
||||||
url.startsWith('file://.')
|
|
||||||
) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Prevent navigation
|
// Prevent navigation
|
||||||
@@ -34,9 +37,10 @@ app.on('web-contents-created', (_, contents) => {
|
|||||||
* @see https://www.electronjs.org/docs/latest/tutorial/security#15-do-not-use-openexternal-with-untrusted-content
|
* @see https://www.electronjs.org/docs/latest/tutorial/security#15-do-not-use-openexternal-with-untrusted-content
|
||||||
*/
|
*/
|
||||||
contents.setWindowOpenHandler(({ url }) => {
|
contents.setWindowOpenHandler(({ url }) => {
|
||||||
// Open default browser
|
if (!isInternalUrl(url)) {
|
||||||
shell.openExternal(url).catch(console.error);
|
// Open default browser
|
||||||
|
shell.openExternal(url).catch(console.error);
|
||||||
|
}
|
||||||
// Prevent creating new window in application
|
// Prevent creating new window in application
|
||||||
return { action: 'deny' };
|
return { action: 'deny' };
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user