mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-26 09:09:36 -05:00
6e89eff490
Ported the function as-is and added comments so we don't forget about this in the future. Fixes: https://github.com/go-gitea/gitea/issues/29462 (cherry picked from commit 82405f808d7b50c3580f26e5ca645e2ed6d284ab)
22 lines
859 B
JavaScript
22 lines
859 B
JavaScript
// Convert an absolute or relative URL to an absolute URL with the current origin. It only
|
|
// processes absolute HTTP/HTTPS URLs or relative URLs like '/xxx' or '//host/xxx'.
|
|
// NOTE: Keep this function in sync with clone_script.tmpl
|
|
export function toOriginUrl(urlStr) {
|
|
try {
|
|
if (urlStr.startsWith('http://') || urlStr.startsWith('https://') || urlStr.startsWith('/')) {
|
|
const {origin, protocol, hostname, port} = window.location;
|
|
const url = new URL(urlStr, origin);
|
|
url.protocol = protocol;
|
|
url.hostname = hostname;
|
|
url.port = port || (protocol === 'https:' ? '443' : '80');
|
|
return url.toString();
|
|
}
|
|
} catch {}
|
|
return urlStr;
|
|
}
|
|
|
|
window.customElements.define('gitea-origin-url', class extends HTMLElement {
|
|
connectedCallback() {
|
|
this.textContent = toOriginUrl(this.getAttribute('data-url'));
|
|
}
|
|
});
|