Napacitor's JavaScript API
Napacitor has several JavaScript functions available to ensure apps run successfully across multiple platforms with the same codebase.
The Global Napacitor Object
You can import the global Napacitor object with the following code:
import { Napacitor } from '@napacitor/core';
The Napacitor object has several functions that help with the most common WebView to Native problems you may face when developing a Napacitor app.
Napacitor.convertFileSrc
convertFileSrc: (filePath: string) => string;
Converts a device filepath into a Web View-friendly path.
In Capacitor, apps are served on a different protocol than device files, so paths to device files must be rewritten before being used in the Web View. Napacitor apps run in a MAUI BlazorWebView where static assets share the same origin as the page, so the path is returned unchanged — the same relative URL works in the WebView directly. The function exists for API compatibility; see the Web API reference for details.
const fixedPhotoUri = Napacitor.convertFileSrc(rawPhotoUri.uri);
Napacitor.getPlatform
getPlatform: () => string;
Get the name of the platform the app is currently running on. This will return a value of "web", "ios", "android", "windows", or "macos" depending on the device the app is running on.
const platform = Napacitor.getPlatform();
if (platform === 'web') {
console.log('Running in a browser!');
} else {
console.log(`Running natively on ${platform}!`);
}
Napacitor.isNativePlatform
isNativePlatform: () => boolean;
Check whether the currently running platform is native (ios, android, windows, or macos). This function returns a value of true if the app is running as a native, installed Napacitor app, or false if it is served via a browser or installed as a PWA.
if (Napacitor.isNativePlatform()) {
console.log("I'm a native app!");
} else {
console.log("I'm a PWA or Web app!");
}
Napacitor.isPluginAvailable
isPluginAvailable: (name: string) => boolean;
Check if a plugin is available on the currently running platform. The plugin name is used in the plugin registry, which means it also works with custom plugins.
const isAvailable = Napacitor.isPluginAvailable('Camera');
if (!isAvailable) {
// Have the user upload a file instead
} else {
// Otherwise, make the call:
const photos = await Camera.pickPhotos();
}
For the complete JavaScript API surface — including registerPlugin and NapacitorException — see the Napacitor Web API reference.