code.gitea.io/gitea@v1.21.7/web_src/js/webcomponents/GiteaOriginUrl.js (about)

     1  // Convert an absolute or relative URL to an absolute URL with the current origin
     2  export function toOriginUrl(urlStr) {
     3    try {
     4      // only process absolute HTTP/HTTPS URL or relative URLs ('/xxx' or '//host/xxx')
     5      if (urlStr.startsWith('http://') || urlStr.startsWith('https://') || urlStr.startsWith('/')) {
     6        const {origin, protocol, hostname, port} = window.location;
     7        const url = new URL(urlStr, origin);
     8        url.protocol = protocol;
     9        url.hostname = hostname;
    10        url.port = port || (protocol === 'https:' ? '443' : '80');
    11        return url.toString();
    12      }
    13    } catch {}
    14    return urlStr;
    15  }
    16  
    17  window.customElements.define('gitea-origin-url', class extends HTMLElement {
    18    connectedCallback() {
    19      this.textContent = toOriginUrl(this.getAttribute('data-url'));
    20    }
    21  });