Repository: jiahaog/nativefier
File: app/src/helpers/helpers.ts (TypeScript)

Code before:
  url: string,
  options?: OpenExternalOptions,
): Promise<void> {
  const urlShellSafety = isUrlShellSafe(url);
  log.debug('openExternal', { url, options, urlShellSafety });
  if (urlShellSafety.blocked) {
    return new Promise((resolve) => {
      showNavigationBlockedMessage(
        `Navigation blocked to ${url}\n\n${urlShellSafety.reason}`,
      )
        .then(() => resolve())
        .catch((err: unknown) => {
          throw err;
        });
    });
  }

  return shell.openExternal(url, options);
}

export function removeUserAgentSpecifics(
  userAgentFallback: string,
  appName: string,
  appVersion: string,
): string {
  // Electron userAgentFallback is the user agent used if none is specified when creating a window.
  // For our purposes, it's useful because its format is similar enough to a real Chrome's user agent to not need
  // to infer the userAgent. userAgentFallback normally looks like this:
  // Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) app-nativefier-804458/1.0.0 Chrome/89.0.4389.128 Electron/12.0.7 Safari/537.36
  // We just need to strip out the appName/1.0.0 and Electron/electronVersion
  return userAgentFallback
    .replace(`Electron/${process.versions.electron} `, '')
    .replace(`${appName}/${appVersion} `, '');
}

/** Removes extra spaces from a text */
export function cleanupPlainText(text: string): string {
  return text.trim().replace(/\s+/g, ' ');
}


Review comment (suggestion):
For the record `Boolean` or `boolean` on a string only returns its truthiness (i.e., is it not blank, null, nor undefined).

```JavaScript
> Boolean('true')
true
> Boolean('false')
true
```

We have written a `parseBoolean` function in `src/utils/parseUtils.ts` that should probably be copy and pasted here for use in the app.

Code after:
  url: string,
  options?: OpenExternalOptions,
): Promise<void> {
  const urlShellSafety = isUrlShellSafe(url);
  log.debug('openExternal', { url, options, urlShellSafety });
  if (urlShellSafety.blocked) {
    return new Promise((resolve) => {
      showNavigationBlockedMessage(
        `Navigation blocked to ${url}\n\n${urlShellSafety.reason}`,
      )
        .then(() => resolve())
        .catch((err: unknown) => {
          throw err;
        });
    });
  }

  return shell.openExternal(url, options);
}

// Copy-pastaed as unable to get imports to work in preload.
// If modifying, update also app/src/preload.ts
export function isWayland(): boolean {
  return (
    isLinux() &&
    (Boolean(process.env.WAYLAND_DISPLAY) ||
      process.env.XDG_SESSION_TYPE === 'wayland')
  );
}

export function removeUserAgentSpecifics(
  userAgentFallback: string,
  appName: string,
  appVersion: string,
): string {
  // Electron userAgentFallback is the user agent used if none is specified when creating a window.
  // For our purposes, it's useful because its format is similar enough to a real Chrome's user agent to not need
  // to infer the userAgent. userAgentFallback normally looks like this:
  // Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) app-nativefier-804458/1.0.0 Chrome/89.0.4389.128 Electron/12.0.7 Safari/537.36
  // We just need to strip out the appName/1.0.0 and Electron/electronVersion
  return userAgentFallback
    .replace(`Electron/${process.versions.electron} `, '')
    .replace(`${appName}/${appVersion} `, '');
}

/** Removes extra spaces from a text */
export function cleanupPlainText(text: string): string {
  return text.trim().replace(/\s+/g, ' ');
}