I am writing a safari plugin with NPAPI. How can I return an integer from NPAPI plugin(Not using FireBreath) to JavaScript? javascript:
<html>
<head>
<script>
function run() {
var plugin = document.getElementById("pluginId");
var number = plugin.getBrowserName();
alert(number);
}
</script>
</head>
<body >
<embed width="0" height="0" type="test/x-open-with-default-plugin" id="pluginId">
<button onclick="run()">run</button>
</body>
</html>
plugin code:
bool plugin_invoke(NPObject *obj, NPIdentifier methodName, const NPVariant *args, uint32_t argCount, NPVariant *result) {
// Make sure the method called is "open".
NPUTF8 *name = browser->utf8fromidentifier(methodName);
if(strcmp(name, plugin_method_name_getBrowserName) == 0) {
//what can i do here?
}
return true;
}
How to return a number from plugin.getBrowserName()?
Plz help!
I am find this thread:Return an integer/String from NPAPI plugin to JavaScript(Not using FireBreath), but i don't know where are these code
char* npOutString = (char *)pNetscapefn->memalloc(strlen(StringVariable) + 1);
if (!npOutString) return false; strcpy(npOutString, StringVariable);
STRINGZ_TO_NPVARIANT(npOutString, *result);
put.
Have you looked at http://npapi.com/tutorial3?
The return value goes in the NPVariant* result. Go look at the docs for NPVariant and you'll see there is a type and then a union for different types of data. The string code you're talking about would go in place of your "//what can i do here?" comment. To return an integer, you'd do this:
bool plugin_invoke(NPObject *obj, NPIdentifier methodName, const NPVariant *args, uint32_t argCount, NPVariant *result) {
// Make sure the method called is "open".
NPUTF8 *name = browser->utf8fromidentifier(methodName);
if(strcmp(name, plugin_method_name_getBrowserName) == 0) {
result->type = NPVariantType_Int32;
result->intValue = 42;
}
return true;
You can also use the *_TO_NPVARIANT macros (documented at the link above for NPVariant docs) like so:
bool plugin_invoke(NPObject *obj, NPIdentifier methodName, const NPVariant *args, uint32_t argCount, NPVariant *result) {
// Make sure the method called is "open".
NPUTF8 *name = browser->utf8fromidentifier(methodName);
if(strcmp(name, plugin_method_name_getBrowserName) == 0) {
INT32_TO_NPVARIANT(42, *result);
}
return true;
If you look at the source for the INT32_TO_NPVARIANT macro you'll see it just does the same thing I did above, so the two are equivilent.