如何在javascript中检查浏览器是否有pdf查看器

发布于 2025-01-10 15:51:49 字数 858 浏览 0 评论 0原文

我需要检查浏览器是否能够在 iframe 中显示 pdf。

我试图简单地检查 mimeTypes 或插件,但现在在 mac 上的 Firefox 中,它似乎工作不正常,它显示空数组。

function isPDFSupported(): boolean {
  let hasPDFViewer = false;
  try {
    const pdf =
      navigator.mimeTypes && (navigator.mimeTypes as any)['application/pdf']
        ? (navigator.mimeTypes as any)['application/pdf'].enabledPlugin
        : 0;
    if (pdf) hasPDFViewer = true;
  } catch (e) {
    if ((navigator.mimeTypes as any)['application/pdf'] != null) hasPDFViewer = true;
  }

  return hasPDFViewer;
}

还要检查developer.mozilla页面的mimeTypes https://developer .mozilla.org/en-US/docs/Web/API/Navigator/mimeTypes 它显示 mimeTypes 和插件已被弃用...

您知道其他替代方案吗?或者只是简单地在 IE 下载 pdf 并在其他情况下尝试显示 pdf 时执行?

I need to check if the browser has ability to show pdf in iframe.

I was trying to simple check mimeTypes or plugins but now in Firefox on mac, it doesn't seem to be working fine, it shows empty array.

function isPDFSupported(): boolean {
  let hasPDFViewer = false;
  try {
    const pdf =
      navigator.mimeTypes && (navigator.mimeTypes as any)['application/pdf']
        ? (navigator.mimeTypes as any)['application/pdf'].enabledPlugin
        : 0;
    if (pdf) hasPDFViewer = true;
  } catch (e) {
    if ((navigator.mimeTypes as any)['application/pdf'] != null) hasPDFViewer = true;
  }

  return hasPDFViewer;
}

also check the developer.mozilla page for mimeTypes https://developer.mozilla.org/en-US/docs/Web/API/Navigator/mimeTypes and it shows that mimeTypes and plugin is deprecated...

Do you know some other alternatives? or just simply do if IE download pdf and in other cases try to show pdf?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅笑依然 2025-01-17 15:51:49

我最近遇到了同样的挑战,并找到了一种检查 PDF 查看器插件的方法。

正如 Mozilla 文档 中所述,导航器。 plugins 属性返回一个 PluginArray 对象,其中列出了应用程序中安装的所有插件。

PDF 内联查看插件将在插件名称中包含“PDF 查看器”,例如“Chrome PDF 查看器”或“Microsoft Edge PDF 查看器”。因此,您可以检查是否有任何插件包含此字符串,如下所示:

if ('PDF Viewer' in navigator.plugins) {
  // your code
}

这对我来说效果很好。但是,我需要指出,此方法已被弃用,因此我不确定它仍会受支持多久。

I recently came across the same challenge and found a way to check for a PDF viewer plugin.

As explained in the Mozilla documentation, the navigator.plugins property returns a PluginArray object, which lists all of the plugins that are installed in the application.

PDF inline viewing plugins will contain "PDF viewer" in the plugin name, e.g. "Chrome PDF Viewer" or "Microsoft Edge PDF Viewer". So you can check if any of the plugins contain this string as follows:

if ('PDF Viewer' in navigator.plugins) {
  // your code
}

This worked well for me. However, I need to point out that this method is deprecated so I'm not sure how long it will still be supported.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文