# Firefox 上的 PWA

> Firefox 上的安装意味着什么：Firefox 143 起 Windows 支持任务栏 Web 应用，Firefox for Android 可按 manifest 安装，两者都没有 beforeinstallprompt。

Firefox 支持在 Windows 和 Android 上安装 Web 应用。Windows 上的 Firefox 143 及之后版本可以把网站固定到任务栏并在简化窗口中运行；Firefox for Android 可以从菜单把带 manifest 的网站添加到主屏幕。两条路径都不会触发 `beforeinstallprompt`，为 Chromium 写的安装流程代码必须分支处理。

## 工作原理

Firefox 没有 Chromium 式的地址栏安装图标，也没有页面内的安装事件。Windows 和 Android 的安装功能都从 Firefox 自己的界面进入。

### Windows 上的任务栏 Web 应用（Firefox 143+）

[Firefox 143.0 发行说明](https://www.firefox.com/en-US/firefox/143.0/releasenotes/)（2025-09-16）把该功能描述为"把网站作为 Web 应用直接固定到任务栏运行"：用户固定网站后，它从任务栏启动、在简化窗口中打开，并保留用户已安装的附加组件。发行说明同时排除了从 Microsoft Store 安装的 Firefox，且该版本只支持 Windows。用户从地址栏发起安装，网站的 manifest 无法发起这个流程。

### Firefox for Android 上的主屏幕 Web 应用

Android 上从菜单安装。按 Mozilla 支持文章 [Use Web Apps with Firefox for Android](https://support.mozilla.org/en-US/kb/use-web-apps-firefox-android) 的步骤，打开网站后点按菜单按钮，再点 **Install**。Android 会显示 **Add to Home Screen** 面板，你可以拖动图标选择位置，也可以点 **Add automatically**（英文界面）。

安装后的图标用 Firefox 打开网站。卸载 Firefox 时，这个 Web 应用也会删除。

### macOS 与 Linux 上的桌面版 Firefox

Firefox 143 在 macOS 和 Linux 上没有由 manifest 驱动的安装流程。MDN 的[安装指南](https://developer.mozilla.org/en-US/docs/Web/Progressive_web_apps/Guides/Installing)指出，桌面版 Firefox 需要第三方 PWA 扩展才能安装网站。可以引导这些用户收藏书签或使用普通标签页，并确保应用在标签页中功能完整。

### Firefox 没有实现的部分

`BeforeInstallPromptEvent` 不是标准，[MDN 未列出任何实现它的 Firefox 或 Firefox for Android 版本](https://developer.mozilla.org/en-US/docs/Web/API/BeforeInstallPromptEvent)（Chrome 44+、Edge 79+、Samsung Internet 4.0+ 有实现）。页面在 Firefox 上无法推迟、触发或检测安装提示，`appinstalled` 也不会触发。

## 示例

两个示例在 Chromium 浏览器中原样运行；分支是为 Firefox 和 Safari 准备的。

### 只在事件触发后显示安装按钮

显示"安装应用"按钮前，先检测浏览器是否支持安装事件。不支持时，给用户相应的安装说明。

```html
<button id="install-button" hidden>安装应用</button>
<p id="install-instructions" hidden>
  这个浏览器不支持从页面一键安装。可以直接当作标签页使用，或收藏为书签。
  在 Firefox for Android 上，打开菜单并点按 Install。
</p>

<script>
  let deferredPrompt = null;
  const installButton = document.getElementById('install-button');
  const installInstructions = document.getElementById('install-instructions');

  if (!('onbeforeinstallprompt' in window)) {
    // Firefox（任何系统）与 Safari：这里不存在该事件。
    installInstructions.hidden = false;
  } else {
    window.addEventListener('beforeinstallprompt', (event) => {
      event.preventDefault();
      deferredPrompt = event;
      installButton.hidden = false;
    });

    installButton.addEventListener('click', async () => {
      if (!deferredPrompt) return;
      deferredPrompt.prompt();
      deferredPrompt = null;
    });
  }
</script>
```

用 `'onbeforeinstallprompt' in window` 检查属性，无需等待事件触发。Firefox 不触发这个事件，如果一直等待，回退说明也就一直隐藏。

### 检测简化窗口

只要 manifest 请求了 `standalone`，Firefox 任务栏 Web 应用和 Firefox for Android 主屏幕 Web 应用都会报告 `display-mode: standalone`，与 Chromium 和 WebKit 使用同一个媒体特性。

```js
function runsAsInstalledApp() {
  if (typeof window.matchMedia !== 'function') {
    // 没有 matchMedia：当作普通标签页处理。
    return false;
  }
  return window.matchMedia('(display-mode: standalone)').matches;
}

document.body.dataset.installed = runsAsInstalledApp() ? 'yes' : 'no';
```

用它在已安装窗口内隐藏安装提示即可；不要用它限制功能，同一个用户下一次可能在普通标签页里打开网站。

## 实测行为

Windows 上的这项功能由一个默认开启的首选项控制，这也是隐藏其地址栏入口唯一有文档记载的方式。

:::note
Mozilla 的 [Web Apps in Firefox](https://firefox-source-docs.mozilla.org/browser/components/taskbartabs/docs/index.html)
文档说明，该功能由 `browser.taskbarTabs.enabled` 控制，在 Windows 上默认启用，地址栏提供
"Add tab to taskbar" 按钮。
[Firefox 143.0 发行说明](https://www.firefox.com/en-US/firefox/143.0/releasenotes/)
描述了固定到 Windows 任务栏、并保留已安装附加组件的 Web 应用，同时说明该功能不适用于
通过 Microsoft Store 安装的 Firefox。
:::

由于固定窗口保留附加组件（发行说明，2025-09-16），内容拦截器和密码管理器在 Web 应用里继续工作；测试第三方脚本能否加载时，要开启拦截器再测一遍。

## 另请参阅

- [Chrome 与 Android 上的 PWA](/zh/reference/platforms/chrome-android/)
- [iOS 与 Safari 上的 PWA](/zh/reference/platforms/ios-safari/)
- [安装提示](/zh/reference/installation/install-prompt/)
- [安装提示兼容性](/zh/compatibility/install-prompt/)
- [Firefox 143.0 release notes](https://www.firefox.com/en-US/firefox/143.0/releasenotes/)（firefox.com）
- [Use Web Apps with Firefox for Android](https://support.mozilla.org/en-US/kb/use-web-apps-firefox-android)（support.mozilla.org）
- [BeforeInstallPromptEvent](https://developer.mozilla.org/en-US/docs/Web/API/BeforeInstallPromptEvent)（developer.mozilla.org）