如何新增更豐富的安裝使用者介面

應用程式商店為開發人員提供了在安裝前展示應用程式的空間,其中包含螢幕截圖和文字資訊,可協助使用者自行選擇安裝應用程式。更豐富的安裝 UI 提供類似的空間,可讓網頁應用程式開發人員直接在瀏覽器中邀請使用者安裝應用程式。Android 版和電腦版 Chrome 都能使用這個加強型使用者介面。

預設提示

請參閱下方範例,瞭解未提供足夠的背景資訊的預設介面。

電腦版瀏覽器的預設安裝對話方塊。
電腦的預設安裝對話方塊


行動裝置的瀏覽器預設安裝對話方塊。
行動裝置的預設安裝對話方塊

更豐富的安裝使用者介面

如要取得內容豐富的安裝使用者介面對話方塊,而非一般小型的預設提示,請在網頁資訊清單中新增 screenshotsdescription 欄位。請參閱以下的 Squoosh.app 範例:

電腦和行動裝置提供更豐富的安裝使用者介面
更豐富的電腦和行動裝置介面安裝使用者介面。

更豐富的安裝功能使用者介面對話方塊是由網頁資訊清單中 descriptionscreenshots 欄位的內容組成。

如要觸發對話方塊,只需為對應的板型規格新增至少一張螢幕截圖即可,但我們建議您一併新增說明。請參閱下方欄位的詳細說明。

螢幕截圖

螢幕截圖真的是新版安裝使用者介面的「內容更豐富的」部分,因此強烈建議您採用。請在資訊清單中新增 screenshots 成員,該成員的陣列需要至少一張圖片,而 Chrome 最多會顯示八張。範例如下所示。

 "screenshots": [
    {
     "src": "source/image1.gif",
      "sizes": "640x320",
      "type": "image/gif",
      "form_factor": "wide",
      "label": "Wonder Widgets"
    }
]

螢幕截圖必須符合下列條件:

  • 寬度和高度不得小於 320 像素,且不得超過 3,840 像素。
  • 長邊的尺寸長度不得超過 2.3 倍。
  • 所有板型規格值相同的螢幕截圖都必須有相同的顯示比例
  • 僅支援 JPEG 和 PNG 圖片格式。
  • 只會顯示八張螢幕截圖。如果加入更多指令碼,使用者代理程式會直接忽略這些內容。

此外,您還需要圖片的大小和類型,才能正確顯示圖片。查看這個示範

form_factor 可向瀏覽器表明螢幕截圖應顯示在電腦 (wide) 或行動裝置環境 (narrow)。

說明

description 成員在安裝提示中描述應用程式,以邀請使用者保留應用程式。

即使沒有 description,對話方塊仍會顯示,但建議您使用。只有在 7 行文字 (大約 324 個字元) 後,才有資格加入超過 7 行文字,較長的說明會截斷,附加刪節號 (如這個範例所示)。

{
…
"description": "Compress and compare images with different codecs
right in your browser."
}
已新增說明
已新增說明。
無法完整提供完整說明。
較長的說明會遭到截斷。

說明會顯示在安裝提示頂端。

您可能已經注意到,安裝對話方塊也會列出應用程式的來源。如果來源太長,無法配合 UI 內容,系統會將其截斷。這也稱為省略符號,會做為保護使用者的安全措施

其他資訊

操作示範

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 add Richer Install UI to your web app</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 add Richer Install UI to your web app</h1>
    <ol>
      <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>
      <li>
        When you click on install a dialog similar to the ones from app stores
        will be displayed.
      </li>
      <li>
        The dialog includes the `description` and `screenshots` set in the app
        manifest.
      </li>
      <li>
        Screenshots should be different depending if the app is being installed
        on a mobile or desktop device, according to the `form_factor` value set
        for the screenshots on the manifest
      </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();
  });
}