Earlier today on Twitter a user asked an interesting question: How can I tell - via JavaScript - if a particular plugin is installed in a Cordova/PhoneGap application. I responded by asking how you wouldn't know since it is your own app, but then he mentioned that his code base was stand alone and would be used within other projects. (So basically - just JavaScript code that other Cordova/PhoneGap applications would use.)
There are a couple of different ways to handle this. The first, and simplest, is to look for the "hook" the plugin adds. For example, the barcode plugin adds methods to a cordova.plugins.barcodeScanner object. It would be trivial to see if that exists. Cool, problem solved, right?
Well in his case he was using InAppBrowser. This plugin simply modifies window.open so it isn't something you can really (as far as I can see) introspect. I did some more digging and found something interesting.
If you look at the platform build version of your www code, you will notice it includes a cordova_plugins.js file:
If you open it up, you will see a list of any plugins you have installed. I opened up cordova.js (honestly, I don't look at it often, but I should) and saw that cordova_plugins.js was being loaded in dynamically and parsed. My assumption is that this has to happen before deviceready fires so you can safely use any plugins you've got installed. On a whim then I tried the following code inside my deviceready:
var md = cordova.require("cordova/plugin_list").metadata;
The metadata
part came from what I saw in cordova_plugins.js. While the rest of the file has random stuff based on the plugins installed, metadata appears to be just a list of your plugins. I confirmed that this worked well:
So - that's it. I should note that I spoke with Shazron and he mentioned that if you used browserify, it might mess with how the JS file is generated. I'd say use with caution and let me know (via the comments) how it works for you.
Archived Comments
This worked exactly how I wanted.
Thanks for this!
I received a report that it is causing the users device to freeze.
https://twitter.com/RamizKa...
Any ideas on why this might be happening?
Thanks,
Nope. But I'll test. It may be a while though - entering vacation time.
Please forgive my test - this is OT and can be ignored. Nic, if you get an email on this, let me know if the link works.
One more quick test.
Speaking of browserify.. would love to hear when/how that is being used. Effort seems to have dropped off lately.
I only "kinda" know what browserify does. I haven't used it personally.
Unfortunately I couldn't reproduce this. I tested in Genymotion (simulator) and my HTC M8. It did not hang. Best I can suggest is to reproduce it yourself and try to see where in particular it gets hung up.
The user later mentioned this only happened outside the onDeviceReady() or $ionicPlatform.ready() methods. I think this is a non-issue as of now.
Thanks for looking into it.
I'm using Ionic Framework with their Deploy service, which allows you to live-ship an updated JS codebase to users who already have your app installed.
This poses the risk of deploying an JS codebase with an added cordova plug-in, where the app binary on the user's phone is missing the platform-native counterpart of that plugin.
Your method may be a life saver in this regard, as it would allow me to detect if the user's binary does in fact have the plug-in installed. My only question is, do you know for sure that this list is based on what plugins the platform side of the app has installed? Or is it just a list of plug-ins present in the JS codebase?
It should work. I'm not 100% certain. You could test easily though, right? :)
Yeah it definitely lists the plugins that were present at build time, effectively giving you the list of plugins that the binary being run, supports.
Of course - I wasn't worried at all. ;)
Very helpful thanks again Ray!
:-( i wish i had seen this post before
why this isn't documented officially ? :-P damn its a live saver... by mistake its very easy to push an app to the store with some missing or incorrect plugin :-P
This is why you always test. :)
Hi Raymond,
I had a look at the "cordova_plugins.js" and I found that "cordova-plugin-email-composer" was already listed in. So I used simple coding below after the 'deviceready' in order to make sure that it loads at the runtime in phonegap simulater.
/*********
$scope.goto_contactus = function(){
if (window.cordova && cordova.plugins.email) {
console.log('True');
} else {
console.log('False');
}
}
*********/
But the result was 'True' in the browser and it was 'False' at the Phonegap simulater app.
The code inside the "cordova_plugins.js" was as below.
/****************
"id": "cordova-plugin-email-composer.EmailComposer",
"file": "plugins/cordova-plugin-email-composer/www/email_composer.js",
"pluginId": "cordova-plugin-email-composer",
"clobbers": [
"cordova.plugins.email",
"plugin.email"
]
******************/
Do you have any idea, why is the plugin doesn't load when I run it on simulater ?
I already tried some other plugins and those were worked perfectly. But it doesn't work for "cordova-plugin-email-composer"
Thanks
To be clear - the plugin works, just not your code to check for it?
surely... it must used AFTER device ready :)
As soon as i'll have some free time i'll post the code of a service of mine to check plugins at runtime based on the undocumented method described by you :) Thanks as always :)
i got it with ios simulator :)