快速入门
复制包含安装步骤和该插件完整 Markdown 指南的配置 prompt。
Set up this Capacitor plugin in the project.
Use the package manager already used by the project.
Install these package(s): `@capgo/capacitor-webview-guardian`
Run the required Capacitor sync/update step after installation.
Read this markdown guide for the full setup steps: https://raw.githubusercontent.com/Cap-go/website/refs/heads/main/apps/docs/src/content/docs/docs/plugins/webview-guardian/getting-started.mdx
Use that guide for platform-specific steps, native file edits, permissions, config changes, imports, and usage setup.
If that guide references other docs pages, read them too.
安装
“安装”章节你可以使用我们的 AI 辅助设置来安装插件。使用以下命令将 Capgo skills 添加到你的 AI 工具中:
bunx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins然后使用以下 prompt:
Use the `capacitor-plugins` skill from `Cap-go/capgo-skills` to install the `@capgo/capacitor-webview-guardian` plugin in my project.如果你更想手动设置,请运行以下命令安装插件,并按照下方各平台的说明操作:
bun add @capgo/capacitor-webview-guardianbunx cap sync导入
“导入”章节import { WebviewGuardian } from '@capgo/capacitor-webview-guardian';快速入门
章节标题为“快速入门”import { WebviewGuardian } from '@capgo/capacitor-webview-guardian';
const result = await WebviewGuardian.startMonitoring();console.log(result);示例:stopMonitoring
标题为“示例:stopMonitoring”的章节import { WebviewGuardian } from '@capgo/capacitor-webview-guardian';
const result = await WebviewGuardian.stopMonitoring();console.log(result);示例:getState
标题为“示例:getState”的章节import { WebviewGuardian } from '@capgo/capacitor-webview-guardian';
const result = await WebviewGuardian.getState();console.log(result);常见坑点
章节标题为“常见陷阱”- 监听器: 在页面卸载时移除句柄。
- 原生同步:运行
bunx cap sync在插件升级后。 - 唯一可信来源:信任
src/definitions.ts在 GitHub 仓库中。
API 概览
章节标题:“API 概览”startMonitoring
“startMonitoring” 章节开始监听前台事件并自动检查 WebView 健康状态。
import { WebviewGuardian } from '@capgo/capacitor-webview-guardian';
const result = await WebviewGuardian.startMonitoring();console.log(result);stopMonitoring
“stopMonitoring” 章节停止所有自动前台监控。
import { WebviewGuardian } from '@capgo/capacitor-webview-guardian';
const result = await WebviewGuardian.stopMonitoring();console.log(result);getState
标题为 “getState” 的章节返回当前已知的最新监控状态。
import { WebviewGuardian } from '@capgo/capacitor-webview-guardian';
const result = await WebviewGuardian.getState();console.log(result);checkNow
标题为 “checkNow” 的章节立即强制触发一次 WebView 健康探测。
import { WebviewGuardian } from '@capgo/capacitor-webview-guardian';
const result = await WebviewGuardian.checkNow();console.log(result);类型参考
“类型参考” 章节StartMonitoringOptions
标题为 “StartMonitoringOptions” 的章节export interface StartMonitoringOptions { /** * Delay (in ms) before running a health check after the app re-enters the foreground. * Defaults to 600ms to let the bridge finish resuming. */ foregroundDebounceMs?: number;
/** * Script executed via `evaluateJavascript`/`evaluateJavaScript` to confirm the WebView is alive. * Defaults to `document.readyState`. */ pingScript?: string;
/** * Automatically reloads the WebView when a terminated render process is detected. * Disable to receive `webviewCrashed` events and restart manually. */ autoRestart?: boolean;
/** * Strategy used when restarting the WebView. Defaults to `reload`. */ restartStrategy?: RestartStrategy;
/** * Custom HTTPS/HTTP URL to load when `restartStrategy` is `customUrl`. */ customRestartUrl?: string;
/** * Emits verbose logging in the native layer when true. */ debug?: boolean;
/** * Whether an immediate health check should be executed right after enabling monitoring. * Defaults to `true`. */ runInitialCheck?: boolean;}GuardianState
标题为 “GuardianState” 的章节export interface GuardianState { monitoring: boolean; reason: string; timestamp: string; lastHealthyAt?: string; lastRestartAt?: string; lastCrashAt?: string; pendingRestartReason?: string; error?: string;}CheckNowOptions
“CheckNowOptions” 章节export interface CheckNowOptions { /** * Text tag describing why a manual check is being requested. */ reason?: string;}CheckResult
“CheckResult” 章节export interface CheckResult { healthy: boolean; restarted: boolean; reason: string; timestamp: string; error?: string; pendingRestart?: boolean;}GuardianEvent
“GuardianEvent” 章节export type GuardianEvent = GuardianState;RestartStrategy
“RestartStrategy” 章节export type RestartStrategy = 'reload' | 'reloadFromOrigin' | 'customUrl';单一事实来源
标题为“单一事实来源”的部分本页面由插件的 src/definitions.ts生成。当上游的公共 API 发生变化时,请重新运行 sync。
从入门指南继续
“从入门指南继续”章节如果你正在使用 快速入门 来规划原生媒体和界面行为,并将其与 使用 @capgo/capacitor-webview-guardian 在使用 @capgo/capacitor-webview-guardian 时,获取原生能力的方法: 使用 @capgo/capacitor-live-activities 用于使用 @capgo/capacitor-live-activities 时的原生能力, @capgo/capacitor-live-activities 在 @capgo/capacitor-live-activities 中的实现细节, 使用 @capgo/capacitor-video-player 有关使用 @capgo/capacitor-video-player 时的原生能力,以及 @capgo/capacitor-video-player 关于 @capgo/capacitor-video-player. 中的实现细节