简体   繁体   English

用于检查 PWA 或移动 Web 的 Javascript

[英]Javascript to check if PWA or Mobile Web

I was curious if anyone knew a javascript based method for detecting whether the web experience was being run as a PWA (progressive web app) or it was simply being run as a standard mobile website (with full browser UI).我很好奇是否有人知道一种基于 javascript 的方法来检测网络体验是作为 PWA(渐进式网络应用程序)运行还是只是作为标准移动网站运行(具有完整的浏览器 UI)。

Is there any difference between a PWA that is "installed" versus one that isn't but still has the service worker and/or app cache registered? “已安装”的 PWA 与未安装但仍注册了 Service Worker 和/或应用程序缓存的 PWA 之间有什么区别吗?

If this is for analytical purposes you could set the start URL in the manifest file to include a query string parameter, ex:如果这是出于分析目的,您可以在清单文件中设置起始 URL 以包含查询字符串参数,例如:

"start_url": "./?mode=standalone"

Then in your JavaScript you are able to check for this query string parameter.然后在您的 JavaScript 中,您可以检查此查询字符串参数。

Updated (2020-08-19)更新 (2020-08-19)

Pete LePage wrote a blog on how to setup a custom dimension in Google Analytics using the code below which checks the display mode using window.matchMedia : Pete LePage 写了一篇关于如何使用以下代码在 Google Analytics 中设置自定义维度的博客,该代码使用window.matchMedia检查显示模式:

let displayMode = 'browser';
  const mqStandAlone = '(display-mode: standalone)';
  if (navigator.standalone || window.matchMedia(mqStandAlone).matches) {
    displayMode = 'standalone';
  }
ga('set', 'dimension1', displayMode);

Read more: https://petelepage.com/blog/2020/08/measure-understand-how-installed-pwa-users-differ-from-browser-tab-users/阅读更多: https : //petelepage.com/blog/2020/08/measure-understand-how-installed-pwa-users-differ-from-browser-tab-users/

Updated (2017-01-20):更新 (2017-01-20):

Alternatively you could check in JavaScript using:或者,您可以使用以下方法检查 JavaScript:

if (window.matchMedia('(display-mode: standalone)').matches) {
  console.log("This is running as standalone.");
}

Edit 11 Oct 2019: Added an extra switch to check if the app is launched via TWA - document.referrer.includes('android-app://') 2019 年 10 月 11 日编辑:添加了一个额外的开关来检查应用程序是否通过 TWA 启动 - document.referrer.includes('android-app://')

This works for all - TWA, Chrome & Safari:这适用于所有人 - TWA、Chrome 和 Safari:

const isInStandaloneMode = () =>
      (window.matchMedia('(display-mode: standalone)').matches) || (window.navigator.standalone) || document.referrer.includes('android-app://');

 if (isInStandaloneMode()) {
    console.log("webapp is installed")
}
if (window.matchMedia('(display-mode: standalone)').matches) {
  console.log("This is running as standalone.");
}

This answer is correct but it's worth to mention that PWA could run on plenty of display modes:这个答案是正确的,但值得一提的是,PWA 可以在多种显示模式下运行:

  • fullscreen全屏
  • standalone独立的
  • minimal-ui最小用户界面
  • browser浏览器

If you run your PWA in a 'fullscreen' mode it will return false so additional checks are necessary like:如果您以“全屏”模式运行 PWA,它将返回false因此需要进行额外的检查,例如:

function isPwa() {
    return ["fullscreen", "standalone", "minimal-ui"].some(
        (displayMode) => window.matchMedia('(display-mode: ' + displayMode + ')').matches
    );
}

Note that window.matchMedia check will return true for the 'browser' display mode even when it's not an installed PWA app.请注意,即使不是已安装的 PWA 应用程序, window.matchMedia检查也会为“浏览器”显示模式返回true

Progressive enhancement is more a concept than an specific function or method that involves several technologies.渐进增强更多是一个概念,而不是涉及多种技术的特定功能或方法。 Now progressive web apps are base on service workers which you can verify if the browser support it.现在渐进式网络应用程序基于服务工作者,您可以验证浏览器是否支持它。

// Check for browser support of service worker
if ('serviceWorker' in navigator)

Project lighthouse can help you to detect whether an application is progressive enhanced by performing evaluations of several technologies. Project lighthouse可以通过对多种技术进行评估来帮助您检测应用程序是否是渐进式增强的。 Take a look on it.看看它。

在此处输入图片说明

Hope this help, to clarify.希望这有助于澄清。

在我使用 Microsoft Visual Studio 2017 创建的 PWA 中,以下语句有效:

var isPWA = navigator.userAgent.match(/MSAppHost/i);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM