Fix opening links in desktop IDE (#6507)

* Fix opening links

* Address review

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
somebody1234 2023-05-05 23:49:34 +10:00 committed by GitHub
parent b5578ec2c9
commit e1b4019b45
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 3 deletions

View File

@ -10,6 +10,9 @@ import * as electron from 'electron'
/** The list of hosts that the app can access. They are required for user authentication to work. */
const TRUSTED_HOSTS = ['accounts.google.com', 'accounts.youtube.com', 'github.com']
/** The list of hosts that the app can open external links to. */
const TRUSTED_EXTERNAL_HOSTS = ['discord.gg']
/** The list of URLs a new WebView can be pointed to. */
const WEBVIEW_URL_WHITELIST: string[] = []
@ -79,7 +82,12 @@ function preventNavigation() {
electron.app.on('web-contents-created', (_event, contents) => {
contents.on('will-navigate', (event, navigationUrl) => {
const parsedUrl = new URL(navigationUrl)
if (parsedUrl.origin !== origin && !TRUSTED_HOSTS.includes(parsedUrl.host)) {
const currentWindowUrl = electron.BrowserWindow.getFocusedWindow()?.webContents.getURL()
const parsedCurrentWindowUrl = currentWindowUrl ? new URL(currentWindowUrl) : null
if (
parsedUrl.origin !== parsedCurrentWindowUrl?.origin &&
!TRUSTED_HOSTS.includes(parsedUrl.host)
) {
event.preventDefault()
console.error(`Prevented navigation to '${navigationUrl}'.`)
}
@ -95,8 +103,14 @@ function preventNavigation() {
function disableNewWindowsCreation() {
electron.app.on('web-contents-created', (_event, contents) => {
contents.setWindowOpenHandler(({ url }) => {
console.error(`Blocking new window creation request to '${url}'.`)
return { action: 'deny' }
const parsedUrl = new URL(url)
if (TRUSTED_EXTERNAL_HOSTS.includes(parsedUrl.host)) {
void electron.shell.openExternal(url)
return { action: 'deny' }
} else {
console.error(`Blocking new window creation request to '${url}'.`)
return { action: 'deny' }
}
})
})
}

View File

@ -101,6 +101,7 @@ function TopBar(props: TopBarProps) {
<div className="grow" />
<a
href="https://discord.gg/enso"
target="_blank"
className="flex items-center bg-help rounded-full px-2.5 text-white mx-2"
>
<span>help chat</span>