Suggestion to the library so that it can be executed in a worker context

Author: mastrixCreated Aug 29, 2023Updated Feb 9, 2024
Labelsenhancement

Hi guys, First of all, it's a really nice lib that works like a charm :)

I have a suggestion to add/modify a few lines in utils.ts, that will allow to use this lib in worker.

https://github.com/imgly/background-removal-js/blob/main/src/utils.ts#L95-L101 Original

typescript
function ensureAbsoluteURL(url: string): string {
  if (isAbsoluteURL(url)) {
    return url;
  } else {
    return new URL(url, window.location.href).href;
  }
}

Option 1

typescript
function ensureAbsoluteURL(url: string): string {
  if (isAbsoluteURL(url)) {
    return url;
  } else {
    let executionContextUrl;
    if (typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope) {
      executionContextUrl = new URL(url, self.location.href).href;
    } else {
       executionContextUrl new URL(url, window.location.href).href;
    }
    return executionContextUrl;
  }
}

Option 2

typescript
function ensureAbsoluteURL(url: string): string {
  if (isAbsoluteURL(url)) {
    return url;
  } else {
    return new URL(url, self.location.href).href;
  }
}

Source: imgly/background-removal-js