如何创建应用快捷方式

François Beaufort
François Beaufort

应用快捷方式可帮助用户在您的 Web 应用中快速启动常见或推荐的任务。从显示应用图标的任何位置轻松访问这些任务可提高用户的工作效率以及用户与 Web 应用的互动度。

现代方式

在 Web 应用清单中定义应用快捷方式

在用户桌面上,右键点击任务栏 (Windows) 或 Dock (macOS) 中的应用图标,或在 Android 设备上轻触并按住应用的启动器图标,即可调用应用快捷方式菜单。

Android 设备上一个打开的应用快捷方式菜单。
在 Android 上打开的应用快捷方式菜单
Windows 上已打开的应用快捷方式菜单。
在 Windows 上打开的应用快捷方式菜单

系统仅为已安装的渐进式 Web 应用显示应用快捷方式菜单。如需了解可安装性要求,请参阅学习 PWA 模块中的安装

每个应用快捷方式都表示一个用户 intent,每个 intent 都与您的 Web 应用范围内的一个网址相关联。当用户激活应用快捷方式时,即会打开该网址。

您可以视需要在 Web 应用清单shortcuts 数组成员中声明应用快捷方式。以下是一个可能的 Web 应用清单示例。

{
  "name": "Player FM",
  "start_url": "https://player.fm?utm_source=homescreen",
  "shortcuts": [
    {
      "name": "Open Play Later",
      "short_name": "Play Later",
      "description": "View the list of podcasts you saved for later",
      "url": "/play-later?utm_source=homescreen",
      "icons": [{ "src": "/icons/play-later.png", "sizes": "192x192" }]
    },
    {
      "name": "View Subscriptions",
      "short_name": "Subscriptions",
      "description": "View the list of podcasts you listen to",
      "url": "/subscriptions?utm_source=homescreen",
      "icons": [{ "src": "/icons/subscriptions.png", "sizes": "192x192" }]
    }
  ]
}

浏览器支持

  • 96
  • 96
  • x
  • 17.4

来源

经典方法

如果尚未安装此应用,您可以建议用户从您的网页中拖动一些链接,然后在浏览器书签栏中释放这些链接。这样一来,用户便可在您的 Web 应用中快速启动常见或推荐的任务。

深入阅读

演示

HTML

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="color-scheme" content="dark light" />
    <link rel="manifest" href="manifest.json" />
    <title>How to create app shortcuts</title>
    <!-- TODO: Devsite - Removed inline handlers -->
    <!-- <script>
      if ('serviceWorker' in navigator) {
        window.addEventListener('load', () => {
          navigator.serviceWorker.register('sw.js');
        });
      }
    </script>
    <script type="module" src="script.js"></script> -->
  </head>
  <body>
    <h1>How to create app shortcuts</h1>
    <ol>
      <li>
        You can drag these <a href="blue.html">blue page</a> or
        <a href="red.html">red page</a> links to the bookmarks bar
        and access them later.
      </li>
      <li>
        Install the app by clicking the button below. After the installation,
        the button is disabled.
        <p>
          <button disabled type="button">Install</button>
        </p>
      </li>
    </ol>
  </body>
</html>

JS


        // The install button.
const installButton = document.querySelector('button');

// Only relevant for browsers that support installation.
if ('BeforeInstallPromptEvent' in window) {
  // Variable to stash the `BeforeInstallPromptEvent`.
  let installEvent = null;

  // Function that will be run when the app is installed.
  const onInstall = () => {
    // Disable the install button.
    installButton.disabled = true;
    // No longer needed.
    installEvent = null;
  };

  window.addEventListener('beforeinstallprompt', (event) => {
    // Do not show the install prompt quite yet.
    event.preventDefault();
    // Stash the `BeforeInstallPromptEvent` for later.
    installEvent = event;
    // Enable the install button.
    installButton.disabled = false;
  });

  installButton.addEventListener('click', async () => {
    // If there is no stashed `BeforeInstallPromptEvent`, return.
    if (!installEvent) {
      return;
    }
    // Use the stashed `BeforeInstallPromptEvent` to prompt the user.
    installEvent.prompt();
    const result = await installEvent.userChoice;
    // If the user installs the app, run `onInstall()`.
    if (result.outcome === 'accepted') {
      onInstall();
    }
  });

  // The user can decide to ignore the install button
  // and just use the browser prompt directly. In this case
  // likewise run `onInstall()`.
  window.addEventListener('appinstalled', () => {
    onInstall();
  });
}