【chrome】谷歌浏览器地址栏右侧“安装”按钮实现

地址栏右侧“安装”按钮

这玩意学名叫PWA
全称:Progressive Web App,是提升 Web App 的体验的一种新方法,能给用户原生应用的体验。

一、PWA安装条件:

在 Chrome 中,渐进式 Web 应用程序必须满足以下条件才能触发 beforeinstallprompt 事件和显示浏览器内安装推广栏

  • 未安装 Web 应用程序
  • 符合用户参与启发式
  • 通过 HTTPS 提供服务
  • 具有一个 Web 应用清单,其中包括:
    • short_namename
    • icons - 必须包含一个 192 像素和一个 512 像素的图标
    • start_url
    • display - 必须是 fullscreenstandaloneminimal-ui
    • 不能有 prefer_related_applications,或值为 false
  • 使用 fetch 处理程序注册服务工作进程

二、实战

1. 新建manifest.json文件

在项目根目录创建manifest.json文件

{
  "short_name": "Weather",
  // 安装的PWA应用名称
  "name": "Weather: Do I need an umbrella?",
  // 安装的PWA应用描述
  "description": "Weather forecast information",
  "icons": [
  	// 不同icon资源调用方式
    {
      "src": "/images/icons-vector.svg",
      "type": "image/svg+xml", // 资源类型:image/svg+xml、image/png、image/png
      "sizes": "512x512" // 资源(图像)大小,得和实际的大小一致
    },
  ],
  "id": "/?source=pwa",
  "start_url": "/?source=pwa",
  "background_color": "#3367D6",
  "display": "standalone",
  "scope": "/",
  "theme_color": "#3367D6",
  "shortcuts": [
    {
      "name": "How's weather tomorrow?",
      "short_name": "Tomorrow",
      "description": "View weather information for tomorrow",
      "url": "/tomorrow?source=pwa",
      "icons": [{ "src": "/images/tomorrow.png", "sizes": "192x192" }]
    }
  ],
  "screenshots": [
    {
      "src": "/images/screenshot2.jpg",
      "type": "image/jpg",
      "sizes": "720x540",
      "form_factor": "wide"
    }
  ]
}

2. 新建一个demo.html文件

<!DOCTYPE html>
<html>
  <head>
  	<!-- 引入manifest.json -->
    <link rel="manifest" href="manifest.json" />
  </head>
  <body>
    <h1>Demo</h1>
  </body>
</html>

3. 启动HTTP服务测试效果

地址栏右侧“安装”按钮安装提示


参考资料:

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陀螺蚁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值