我们有一些PDF表单在非Adobe PDF阅读器中无法正确显示(即WebKit的内置PDF阅读器无法正确显示一些Adobe专有内容)。我们想检测用户何时未安装Adobe的PDF Reader并给他们一些警告,但是我很难确定2014年该如何做。

this script似乎在2011年开始运作。基本上,它循环遍历navigator.plugins并查找名称为Adobe AcrobatChrome PDF Viewer的插件。

 for(key in navigator.plugins) {
      var plugin = navigator.plugins[key];
      if(plugin.name == "Adobe Acrobat") return plugin;
 }

快闪到今天,Adobe一定已经做了一些更改,因为我已经安装了Adobe Acrobat,但是它似乎不在navigator.plugins中!现在在哪里,如何检测到它?

最佳答案

好的,我已经更新了脚本,并且现在在所有浏览器中都可以正常工作:

<!DOCTYPE HTML>
<html>
    <head>
        <title> New Document </title>
        <script>
        //
        // http://thecodeabode.blogspot.com
        // @author: Ben Kitzelman
        // @license: FreeBSD: (http://opensource.org/licenses/BSD-2-Clause) Do whatever you like with it
        // @updated: 03-03-2013
        //
        var getAcrobatInfo = function() {

            var getBrowserName = function() {
                return this.name = this.name || function() {
                    var userAgent = navigator ? navigator.userAgent.toLowerCase() : "other";

                    if(userAgent.indexOf("chrome") > -1){
                        return "chrome";
                    } else if(userAgent.indexOf("safari") > -1){
                        return "safari";
                    } else if(userAgent.indexOf("msie") > -1 || navigator.appVersion.indexOf('Trident/') > 0){
                        return "ie";
                    } else if(userAgent.indexOf("firefox") > -1){
                        return "firefox";
                    } else {
                        //return "ie";
                        return userAgent;
                    }
                }();
            };

            var getActiveXObject = function(name) {
                try { return new ActiveXObject(name); } catch(e) {}
            };

            var getNavigatorPlugin = function(name) {
                for(key in navigator.plugins) {
                    var plugin = navigator.plugins[key];
                    if(plugin.name == name) return plugin;
                }
            };

            var getPDFPlugin = function() {
                return this.plugin = this.plugin || function() {
                    if(getBrowserName() == 'ie') {
                        //
                        // load the activeX control
                        // AcroPDF.PDF is used by version 7 and later
                        // PDF.PdfCtrl is used by version 6 and earlier
                        return getActiveXObject('AcroPDF.PDF') || getActiveXObject('PDF.PdfCtrl');
                    } else {
                        return getNavigatorPlugin('Adobe Acrobat') || getNavigatorPlugin('Chrome PDF Viewer') || getNavigatorPlugin('WebKit built-in PDF');
                    }
                }();
            };

            var isAcrobatInstalled = function() {
                return !!getPDFPlugin();
            };

            var getAcrobatVersion = function() {
                try {
                    var plugin = getPDFPlugin();

                    if(getBrowserName() == 'ie') {
                        var versions = plugin.GetVersions().split(',');
                        var latest = versions[0].split('=');
                        return parseFloat(latest[1]);
                    }

                    if(plugin.version) return parseInt(plugin.version);
                    return plugin.name
                }
                catch(e) {
                    return null;
                }
            }

            //
            // The returned object
            //
            return {
                browser: getBrowserName(),
                acrobat: isAcrobatInstalled() ? 'installed' : false,
                acrobatVersion: getAcrobatVersion()
            };
        };

        var info = getAcrobatInfo();
        alert(info.browser+ " " + info.acrobat + " " + info.acrobatVersion);
  </script>
 </head>

 <body>

 </body>
</html>

关于Javascript检测是否安装了Adobe Reader,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26764842/

10-12 18:08