import {shell} from "electron/common"; import fs from "node:fs"; import os from "node:os"; import path from "node:path"; import {html} from "./html.js"; export async function openBrowser(url: URL): Promise { if (["http:", "https:", "mailto:"].includes(url.protocol)) { await shell.openExternal(url.href); } else { // For security, indirect links to non-whitelisted protocols // through a real web browser via a local HTML file. const directory = fs.mkdtempSync(path.join(os.tmpdir(), "zulip-redirect-")); const file = path.join(directory, "redirect.html"); fs.writeFileSync( file, html` Redirecting

Opening ${url.href}

`.html, ); await shell.openPath(file); setTimeout(() => { fs.unlinkSync(file); fs.rmdirSync(directory); }, 15_000); } }