如何建立應用程式捷徑

François Beaufort
François Beaufort

應用程式捷徑可協助使用者在您的網頁應用程式中快速啟動常見或推薦工作。只要顯示應用程式圖示,就能從任何地方輕鬆存取這些工作,不僅提升使用者的工作效率,也增進使用者對網頁應用程式的參與度。

現代做法

在網頁應用程式資訊清單中定義應用程式捷徑

在使用者桌面的工作列 (Windows) 或固定 (macOS) 中的應用程式圖示上按一下滑鼠右鍵,或是在 Android 上按住應用程式的啟動器圖示,即可開啟應用程式捷徑選單。

Android 裝置上已開啟的應用程式捷徑選單。
Android 裝置上已開啟應用程式捷徑選單
Windows 已開啟應用程式捷徑選單。
已在 Windows 中開啟應用程式捷徑選單

只有已安裝的漸進式網頁應用程式才會顯示應用程式捷徑選單。如要瞭解可安裝性規定,請參閱「瞭解 PWA」模組中的「安裝」一節。

每個應用程式捷徑都會代表使用者意圖,每個意圖與網頁應用程式範圍內的網址相關聯。當使用者啟用應用程式捷徑時,即會開啟該網址。

您可以選擇在網頁應用程式資訊清單shortcuts 陣列成員中宣告應用程式捷徑。以下是可能的網頁應用程式資訊清單範例。

{
  "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

資料來源

經典風格

如果應用程式尚未安裝,您可以建議使用者從網頁中拖曳一些連結,並在瀏覽器書籤列中放開。如此一來,他們就可以在您的網路應用程式中快速開始常用或建議的工作。

其他資訊

操作示範

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();
  });
}