You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GiteaOriginUrl.js 793B

123456789101112131415161718192021
  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. window.customElements.define('gitea-origin-url', class extends HTMLElement {
  17. connectedCallback() {
  18. this.textContent = toOriginUrl(this.getAttribute('data-url'));
  19. }
  20. });