1

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.

1
  • Depending on utf8fromidentifier is suboptimal, it's better to use something like static const NPIdentifier id = browser->getstringidentifier("foo"); if (methodName == id) { .... Commented Dec 6, 2012 at 19:57

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

excellent... now please mark the answer =] (that's the outline of a checkmark by the question? click on it if this is the answer)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.