Created
September 17, 2014 13:39
-
-
Save Rob--W/5c4e017e22a47bf55572 to your computer and use it in GitHub Desktop.
Detect whether PDF.js extension is installed in Google Chrome, Chromium or Opera.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Detects whether the PDF.js Chromium extension is installed in the browser. | |
* Example: isPDFJSExtensionInstalled(function(result) { alert('Is PDF.js installed? ' + result);}); | |
* | |
* @param {function(boolean)} callback - Called when the detection completed. | |
* | |
* Author: Rob Wu <[email protected]> (https://robwu.nl) | |
* Public domain - all rights waived. | |
*/ | |
function isPDFJSExtensionInstalled(callback) { | |
if (!window.chrome) { | |
// Not a Chromium browser. | |
setTimeout(callback, 0, false); | |
return; | |
} | |
if (chrome.webstore) { | |
// Chrome or Chromium | |
_isPDFJSExtensionInstalled('oemmndcbldboiebfnladdacbdfmadadm', callback); | |
} else { | |
// Opera? First detect PDF.js from Opera's addon gallery. | |
// If not available, try to detect PDF.js from the Chrome Web Store | |
_isPDFJSExtensionInstalled('encfpfilknmenlmjemepncnlbbjlabkc', function(result) { | |
if (result) callback(true); | |
else _isPDFJSExtensionInstalled('oemmndcbldboiebfnladdacbdfmadadm', callback); | |
}); | |
} | |
function _isPDFJSExtensionInstalled(id, callback) { | |
var x = new XMLHttpRequest(); | |
x.open('GET', 'chrome-extension://' + id + '/content/web/viewer.html'); | |
x.onload = function() { callback(true); }; | |
x.onerror = function() { callback(false); }; | |
x.send(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
it seems that chrome has stopped this way to detect.