mirror of
https://github.com/go-gitea/gitea.git
synced 2024-11-25 08:11:19 +01:00
82405f808d
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
23 lines
859 B
JavaScript
23 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'));
|
|
}
|
|
});
|