Skip to content

Instantly share code, notes, and snippets.

@ItalyPaleAle
Created March 4, 2015 21:58
Show Gist options
  • Save ItalyPaleAle/778b2f541ba570bc05b0 to your computer and use it in GitHub Desktop.
Save ItalyPaleAle/778b2f541ba570bc05b0 to your computer and use it in GitHub Desktop.
Detect PDF Viewer plugins
/*
hasPDFViewer.js
Version: 1.0.0
Date: 2015-03-04
(C) 2015 Alessandro Segala
License: BSD-2-Clause (http://opensource.org/licenses/BSD-2-Clause)
Original author: Ben Kitzelman (http://thecodeabode.blogspot.ca/2011/01/detect-adobe-reader-plugin.html)
*/
function hasPDFViewer() {
var hasActiveXObject = function(name) {
try {
var obj = new ActiveXObject(name)
if(obj) {
return true
}
}
catch(e) {}
return false
}
var hasNavigatorPlugin = function(name) {
for(key in navigator.plugins) {
var plugin = navigator.plugins[key]
if(plugin.name == name) {
return true
}
}
return false
}
// Does not match IE11+, but IE11+ supports navigator.plugins
var userAgent = navigator ? navigator.userAgent.toLowerCase() : 'other'
if(~(userAgent.indexOf('msie'))) {
// Load the activeX control
// AcroPDF.PDF is used by version 7 and later
// PDF.PdfCtrl is used by version 6 and earlier
return hasActiveXObject('AcroPDF.PDF')
|| hasActiveXObject('PDF.PdfCtrl')
}
else {
return hasNavigatorPlugin('Adobe Acrobat')
|| hasNavigatorPlugin('Chrome PDF Viewer')
|| hasNavigatorPlugin('WebKit built-in PDF')
}
}
@ItalyPaleAle
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment