JAVASCRIPT   35

acrobat_detection.js

Guest on 10th October 2021 06:48:05 AM

  1. //
  2. // http://thecodeabode.blogspot.com
  3. // @author: Ben Kitzelman
  4. // @license:  FreeBSD: (http://opensource.org/licenses/BSD-2-Clause) Do whatever you like with it
  5. // @updated:
  6. //
  7. var getAcrobatInfo = function() {
  8.  
  9.   var getBrowserName = function() {
  10.     return this.name = this.name || function() {
  11.       var userAgent = navigator ? navigator.userAgent.toLowerCase() : "other";
  12.  
  13.       if(userAgent.indexOf("chrome") > -1)        return "chrome";
  14.       else if(userAgent.indexOf("safari") > -1)   return "safari";
  15.       else if(userAgent.indexOf("msie") > -1)     return "ie";
  16.       else if(userAgent.indexOf("firefox") > -1)  return "firefox";
  17.       return userAgent;
  18.     }();
  19.   };
  20.  
  21.   var getActiveXObject = function(name) {
  22.     try { return new ActiveXObject(name); } catch(e) {}
  23.   };
  24.  
  25.   var getNavigatorPlugin = function(name) {
  26.     for(key in navigator.plugins) {
  27.       var plugin = navigator.plugins[key];
  28.       if(plugin.name == name) return plugin;
  29.     }
  30.   };
  31.  
  32.   var getPDFPlugin = function() {
  33.     return this.plugin = this.plugin || function() {
  34.       if(getBrowserName() == 'ie') {
  35.         //
  36.         // load the activeX control
  37.         // AcroPDF.PDF is used by version 7 and later
  38.         // PDF.PdfCtrl is used by version 6 and earlier
  39.         return getActiveXObject('AcroPDF.PDF') || getActiveXObject('PDF.PdfCtrl');
  40.       }
  41.       else {
  42.         return getNavigatorPlugin('Adobe Acrobat') || getNavigatorPlugin('Chrome PDF Viewer') || getNavigatorPlugin('WebKit built-in PDF');
  43.       }
  44.     }();
  45.   };
  46.  
  47.   var isAcrobatInstalled = function() {
  48.     return !!getPDFPlugin();
  49.   };
  50.  
  51.   var getAcrobatVersion = function() {
  52.     try {
  53.       var plugin = getPDFPlugin();
  54.  
  55.       if(getBrowserName() == 'ie') {
  56.         var versions = plugin.GetVersions().split(',');
  57.         var latest   = versions[0].split('=');
  58.         return parseFloat(latest[1]);
  59.       }
  60.  
  61.       if(plugin.version) return parseInt(plugin.version);
  62.       return plugin.name
  63.     }
  64.     catch(e) {
  65.       return null;
  66.     }
  67.   }
  68.  
  69.   //
  70.   // The returned object
  71.   //
  72.   return {
  73.     browser:        getBrowserName(),
  74.     acrobat:        isAcrobatInstalled() ? 'installed' : false,
  75.     acrobatVersion: getAcrobatVersion()
  76.   };
  77. };

Raw Paste


Login or Register to edit or fork this paste. It's free.