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:
parent
8088d3b5c2
commit
625b00c247
@ -30,7 +30,7 @@ export * from "./getRandId";
|
||||
export * from "./hash-set";
|
||||
export * from "./n-fircate";
|
||||
export * from "./objects";
|
||||
export * from "./openExternal";
|
||||
export * from "./openBrowser";
|
||||
export * from "./paths";
|
||||
export * from "./promise-exec";
|
||||
export * from "./reject-promise";
|
||||
|
||||
29
src/common/utils/openBrowser.ts
Normal file
29
src/common/utils/openBrowser.ts
Normal 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;
|
||||
@ -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);
|
||||
}
|
||||
@ -3,6 +3,6 @@
|
||||
* 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 { cssNames } from "../../renderer/utils/cssNames";
|
||||
|
||||
@ -2,13 +2,14 @@
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* 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 type { WindowManager } from "../window-manager";
|
||||
import { appName, isMac, isWindows, docsUrl, supportUrl, productName } from "../../common/vars";
|
||||
import logger from "../logger";
|
||||
import { exitApp } from "../exit-app";
|
||||
import { broadcastMessage } from "../../common/ipc";
|
||||
import { openBrowser } from "../../common/utils";
|
||||
import * as packageJson from "../../../package.json";
|
||||
import { preferencesURL, extensionsURL, addClusterURL, catalogURL, welcomeURL } from "../../common/routes";
|
||||
import { checkForUpdates, isAutoUpdateEnabled } from "../app-updater";
|
||||
@ -261,14 +262,18 @@ export function getAppMenu(
|
||||
label: "Documentation",
|
||||
id: "documentation",
|
||||
click: async () => {
|
||||
shell.openExternal(docsUrl);
|
||||
openBrowser(docsUrl).catch(error => {
|
||||
logger.error("[MENU]: failed to open browser", { error });
|
||||
});
|
||||
},
|
||||
},
|
||||
{
|
||||
label: "Support",
|
||||
id: "support",
|
||||
click: async () => {
|
||||
shell.openExternal(supportUrl);
|
||||
openBrowser(supportUrl).catch(error => {
|
||||
logger.error("[MENU]: failed to open browser", { error });
|
||||
});
|
||||
},
|
||||
},
|
||||
...ignoreIf(isMac, [
|
||||
|
||||
@ -5,11 +5,11 @@
|
||||
|
||||
import type { ClusterId } from "../common/cluster-types";
|
||||
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 { appEventBus } from "../common/app-event-bus/event-bus";
|
||||
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 { IpcRendererNavigationEvents } from "../renderer/navigation/events";
|
||||
import logger from "./logger";
|
||||
@ -134,7 +134,9 @@ export class WindowManager extends Singleton {
|
||||
webPreferences.nodeIntegration = false;
|
||||
})
|
||||
.setWindowOpenHandler((details) => {
|
||||
shell.openExternal(details.url);
|
||||
openBrowser(details.url).catch(error => {
|
||||
logger.error("[WINDOW-MANAGER]: failed to open browser", { error });
|
||||
});
|
||||
|
||||
return { action: "deny" };
|
||||
});
|
||||
|
||||
@ -4,7 +4,7 @@
|
||||
*/
|
||||
|
||||
|
||||
import { openExternal } from "../utils";
|
||||
import { openBrowser } from "../utils";
|
||||
import { Notifications } from "../components/notifications";
|
||||
import type { ForwardedPort } from "./port-forward-item";
|
||||
import logger from "../../common/logger";
|
||||
@ -16,7 +16,7 @@ export function portForwardAddress(portForward: ForwardedPort) {
|
||||
export function openPortForward(portForward: ForwardedPort) {
|
||||
const browseTo = portForwardAddress(portForward);
|
||||
|
||||
openExternal(browseTo)
|
||||
openBrowser(browseTo)
|
||||
.catch(error => {
|
||||
logger.error(`failed to open in browser: ${error}`, {
|
||||
port: portForward.port,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user