2014-11-05 42 views
5

Abbiamo alcuni moduli PDF che non vengono visualizzati correttamente nei lettori PDF non Adobe (ad esempio, il lettore PDF incorporato di WebKit non visualizza correttamente alcuni oggetti Adobe proprietari). Vogliamo rilevare quando gli utenti non dispongono di lettore di PDFdi Adobe installato e dare loro un piccolo avvertimento, ma sto avendo difficoltà a capire come farlo nel 2014.Javascript Rileva se Adobe Reader è installato

Sembra this script lavorato nel 2011 Fondamentalmente scorre attraverso navigator.plugins e cerca plugin con il nome Adobe Acrobat o Chrome PDF Viewer.

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

Flash in avanti ad oggi, Adobe deve aver cambiato qualcosa, perché ho installato Adobe Acrobat, ma non sembra essere in navigator.plugins! Dove si trova ora e come lo si rileva?

+0

ho testato lo script qui e funziona perfettamente bene sia in Firefox e Chrome. Tuttavia, a me non sembra piacere. – icecub

+0

Hai una vecchia versione di 'Adobe Reader'? Non compare nemmeno nella lista dei plugin sul mio computer. – brentonstrine

+0

Sì. Ci sto lavorando per IE. Sembra che lo script non rilevi il browser IE. Oh e ho installato Adobe Reader XI – icecub

risposta

10

Ok ho aggiornato la sceneggiatura e che sta funzionando benissimo in tutti i browser oggi:

<!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> 
+1

Grazie - sul lato non Explorer, ho scoperto che mentre Adobe non installa Acrobat in Chrome, lo fa in Firefox, ma lo script non lo stava rilevando perché era facendo 'if (plugin.name == name)', ma dal momento che il nome è 'Adobe Acrobat NPAPI Plug-In, Versione 11.0.09', che stava per apparire falso. La riga dovrebbe probabilmente essere cambiata in 'if (plugin.name.indexOf (name)> - 1)'. Non so ancora perché il plugin non sia in Chrome. – brentonstrine

+1

Beh, non ho mai fatto nulla di strano con il mio Chrome o Adobe Reader e lo script sopra lo rileva perfettamente in Chrome. Quindi trovo molto strano che non funzioni per te. – icecub

+0

Ah, pensavo non fosse solo io perché tutti gli altri in ufficio avevano lo stesso risultato. Dobbiamo aver fatto la stessa cosa strana a Chrome! – brentonstrine