code.gitea.io/gitea@v1.22.3/web_src/js/webcomponents/origin-url.js (about)

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