1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Introduce and use "openBrowser" (#4876)

Co-authored-by: Steven Johnstone <sjohntone@mirantis.com>
This commit is contained in:
Steven Johnstone 2022-02-17 13:37:34 +00:00 committed by GitHub
parent 8088d3b5c2
commit 625b00c247
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 46 additions and 21 deletions

View File

@ -30,7 +30,7 @@ export * from "./getRandId";
export * from "./hash-set"; export * from "./hash-set";
export * from "./n-fircate"; export * from "./n-fircate";
export * from "./objects"; export * from "./objects";
export * from "./openExternal"; export * from "./openBrowser";
export * from "./paths"; export * from "./paths";
export * from "./promise-exec"; export * from "./promise-exec";
export * from "./reject-promise"; export * from "./reject-promise";

View File

@ -0,0 +1,29 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { shell } from "electron";
const allowedProtocols = new Set(["http:", "https:"]);
/**
* Opens a link using the program configured as the default browser
* on the target platform. Will reject URLs with a scheme other than
* http or https to prevent programs other than the default browser
* running.
*
* @param url The URL to open
*/
export function openBrowser(url: string): Promise<void> {
if (allowedProtocols.has(new URL(url).protocol)) {
return shell.openExternal(url);
}
return Promise.reject(new TypeError("not an http(s) URL"));
}
/**
* @deprecated use openBrowser
*/
export const openExternal = openBrowser;

View File

@ -1,11 +0,0 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
// Opens a link in external browser
import { shell } from "electron";
export function openExternal(url: string) {
return shell.openExternal(url);
}

View File

@ -3,6 +3,6 @@
* Licensed under MIT License. See LICENSE in root directory for more information. * Licensed under MIT License. See LICENSE in root directory for more information.
*/ */
export { Singleton, openExternal } from "../../common/utils"; export { Singleton, openExternal, openBrowser } from "../../common/utils";
export { prevDefault, stopPropagation } from "../../renderer/utils/prevDefault"; export { prevDefault, stopPropagation } from "../../renderer/utils/prevDefault";
export { cssNames } from "../../renderer/utils/cssNames"; export { cssNames } from "../../renderer/utils/cssNames";

View File

@ -2,13 +2,14 @@
* Copyright (c) OpenLens Authors. All rights reserved. * Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information. * Licensed under MIT License. See LICENSE in root directory for more information.
*/ */
import { app, BrowserWindow, dialog, Menu, MenuItem, MenuItemConstructorOptions, webContents, shell } from "electron"; import { app, BrowserWindow, dialog, Menu, MenuItem, MenuItemConstructorOptions, webContents } from "electron";
import { autorun, IComputedValue } from "mobx"; import { autorun, IComputedValue } from "mobx";
import type { WindowManager } from "../window-manager"; import type { WindowManager } from "../window-manager";
import { appName, isMac, isWindows, docsUrl, supportUrl, productName } from "../../common/vars"; import { appName, isMac, isWindows, docsUrl, supportUrl, productName } from "../../common/vars";
import logger from "../logger"; import logger from "../logger";
import { exitApp } from "../exit-app"; import { exitApp } from "../exit-app";
import { broadcastMessage } from "../../common/ipc"; import { broadcastMessage } from "../../common/ipc";
import { openBrowser } from "../../common/utils";
import * as packageJson from "../../../package.json"; import * as packageJson from "../../../package.json";
import { preferencesURL, extensionsURL, addClusterURL, catalogURL, welcomeURL } from "../../common/routes"; import { preferencesURL, extensionsURL, addClusterURL, catalogURL, welcomeURL } from "../../common/routes";
import { checkForUpdates, isAutoUpdateEnabled } from "../app-updater"; import { checkForUpdates, isAutoUpdateEnabled } from "../app-updater";
@ -261,14 +262,18 @@ export function getAppMenu(
label: "Documentation", label: "Documentation",
id: "documentation", id: "documentation",
click: async () => { click: async () => {
shell.openExternal(docsUrl); openBrowser(docsUrl).catch(error => {
logger.error("[MENU]: failed to open browser", { error });
});
}, },
}, },
{ {
label: "Support", label: "Support",
id: "support", id: "support",
click: async () => { click: async () => {
shell.openExternal(supportUrl); openBrowser(supportUrl).catch(error => {
logger.error("[MENU]: failed to open browser", { error });
});
}, },
}, },
...ignoreIf(isMac, [ ...ignoreIf(isMac, [

View File

@ -5,11 +5,11 @@
import type { ClusterId } from "../common/cluster-types"; import type { ClusterId } from "../common/cluster-types";
import { makeObservable, observable } from "mobx"; import { makeObservable, observable } from "mobx";
import { app, BrowserWindow, dialog, ipcMain, shell, webContents } from "electron"; import { app, BrowserWindow, dialog, ipcMain, webContents } from "electron";
import windowStateKeeper from "electron-window-state"; import windowStateKeeper from "electron-window-state";
import { appEventBus } from "../common/app-event-bus/event-bus"; import { appEventBus } from "../common/app-event-bus/event-bus";
import { ipcMainOn } from "../common/ipc"; import { ipcMainOn } from "../common/ipc";
import { delay, iter, Singleton } from "../common/utils"; import { delay, iter, Singleton, openBrowser } from "../common/utils";
import { ClusterFrameInfo, clusterFrameMap } from "../common/cluster-frames"; import { ClusterFrameInfo, clusterFrameMap } from "../common/cluster-frames";
import { IpcRendererNavigationEvents } from "../renderer/navigation/events"; import { IpcRendererNavigationEvents } from "../renderer/navigation/events";
import logger from "./logger"; import logger from "./logger";
@ -134,7 +134,9 @@ export class WindowManager extends Singleton {
webPreferences.nodeIntegration = false; webPreferences.nodeIntegration = false;
}) })
.setWindowOpenHandler((details) => { .setWindowOpenHandler((details) => {
shell.openExternal(details.url); openBrowser(details.url).catch(error => {
logger.error("[WINDOW-MANAGER]: failed to open browser", { error });
});
return { action: "deny" }; return { action: "deny" };
}); });

View File

@ -4,7 +4,7 @@
*/ */
import { openExternal } from "../utils"; import { openBrowser } from "../utils";
import { Notifications } from "../components/notifications"; import { Notifications } from "../components/notifications";
import type { ForwardedPort } from "./port-forward-item"; import type { ForwardedPort } from "./port-forward-item";
import logger from "../../common/logger"; import logger from "../../common/logger";
@ -16,7 +16,7 @@ export function portForwardAddress(portForward: ForwardedPort) {
export function openPortForward(portForward: ForwardedPort) { export function openPortForward(portForward: ForwardedPort) {
const browseTo = portForwardAddress(portForward); const browseTo = portForwardAddress(portForward);
openExternal(browseTo) openBrowser(browseTo)
.catch(error => { .catch(error => {
logger.error(`failed to open in browser: ${error}`, { logger.error(`failed to open in browser: ${error}`, {
port: portForward.port, port: portForward.port,