function getURLFromURL(str) {
const urlRegex =
/(https?:\/\/([\w_-]+(?:(?:\.[\w_-]+)+))(?:[\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-]))/gim;
let segments = [];
try {
const url = new URL(str);
const pathSegments = url.pathname
.split("/")
.map(decodeURIComponent);
let params = Array.from(url.searchParams.values());
segments = [...pathSegments, ...params];
} catch (error) {
return;
}
let urls = segments.flatMap((s) => s.match(urlRegex)).filter(Boolean);
return urls[0];
}