2026-03-03 13:28:53 +02:00
|
|
|
import logger from './logger';
|
|
|
|
|
|
2019-10-11 19:09:50 +01:00
|
|
|
/**
|
|
|
|
|
* Opens URL in the browser.
|
|
|
|
|
*
|
|
|
|
|
* @param {string} url - The URL to be opened.
|
2020-05-14 15:30:24 +03:00
|
|
|
* @param {boolean} openInNewTab - If the link should be opened in a new tab.
|
2019-10-11 19:09:50 +01:00
|
|
|
* @returns {void}
|
|
|
|
|
*/
|
2020-05-14 15:30:24 +03:00
|
|
|
export function openURLInBrowser(url: string, openInNewTab?: boolean) {
|
2026-03-03 13:28:53 +02:00
|
|
|
let parsed;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
parsed = new URL(url);
|
|
|
|
|
} catch {
|
|
|
|
|
logger.warn(`Blocked invalid URL: ${url}`);
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (![ 'http:', 'https:' ].includes(parsed.protocol)) {
|
|
|
|
|
logger.warn(`Blocked URL with disallowed protocol: ${parsed.protocol}`);
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-14 15:30:24 +03:00
|
|
|
const target = openInNewTab ? '_blank' : '';
|
|
|
|
|
|
|
|
|
|
window.open(url, target, 'noopener');
|
2019-10-11 19:09:50 +01:00
|
|
|
}
|