Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: can a plugin return a array to javascript?

39 views
Skip to first unread message

Dirk

unread,
Sep 1, 2010, 7:35:23 PM9/1/10
to dev-tech...@lists.mozilla.org
Dirk wrote:
> hello,
>
> (me again..) the plugin currently returns binary data as strings to javascript and i escape characters that are out of range by
> turning them into a 4 character "0xXX" representation...
>
> now would it be possible to have the plugin return an javascript array of integers to the script?
>
> current code:
> const char *outString = (const char *) &buffer;
> char *npOutString = (char *) NPN_MemAlloc (strlen (outString) + 1);
> if (!npOutString)
> return false;
> strcpy (npOutString, outString);
> STRINGZ_TO_NPVARIANT (npOutString, *result);
>
>
> i had a look at NPVariant but there doesn't seem to be support for returning arrays..
>
>
> Thanks,
> Dirk

...or should I just use percent-encoding for binary data?


Dirk

Benjamin Smedberg

unread,
Sep 1, 2010, 9:52:45 PM9/1/10
to
On 9/1/10 6:55 PM, Dirk wrote:
> hello,
>
> (me again..) the plugin currently returns binary data as strings to javascript and i escape characters that are out of range by
> turning them into a 4 character "0xXX" representation...
>
> now would it be possible to have the plugin return an javascript array of integers to the script?

Sure. Create a JavaScript Array object (using NPN_Evaluate("[]") or
something equivalent), and then add each element to it.

> i had a look at NPVariant but there doesn't seem to be support for returning arrays..

An array is just a kind of JS object.

--BDS

taxilian

unread,
Sep 2, 2010, 9:09:40 AM9/2/10
to
I would avoid using NPN_Evaluate and instead use NPN_GetValue to get
the window NPObject and then NPN_Invoke to call window.Array(), but
that's just me.

You can then add items to the array by using NPN_Invoke to call push
on the array NPObject.

FireBreath uses this; in fact, you might want to consider using
FireBreath, as it simplifies all of this. http://firebreath.googlecode.com

The FireBreath code for doing this can be found here:
http://code.google.com/p/firebreath/source/browse/src/NpapiPlugin/NpapiBrowserHost.cpp#174

Keep in mind that FireBreath uses an abstraction for this feature
rather than using NPN_* methods directly, but at least the general
concept is there, and if you dig a little you can find what is
actually getting called.

Richard

Dirk

unread,
Sep 2, 2010, 1:29:26 PM9/2/10
to taxilian, dev-tech...@lists.mozilla.org
I have changed my mind... I think having the read() function in JavaScript return an array of integer-8bit-values is not
intuitive enough.. so i went for strings with percent-encoding of binary data.. the resulting javascript code for probing a
Quake III: Arena game server using the UDP plugin looks like this then... using unescape()..

function rcon (ip, port, command)
{
var udp_socket = document.getElementById('udp_socket_plugin');

udp_socket.open (ip, port);

// because of JavaScript binary data must be percent-encoded
udp_socket.write ('%ff%ff%ff%ff' + command);

var s = udp_socket.read ();
udp_socket.close ();

// strip 4 bytes header (also percent-encoded)
s = unescape (s.substring (12));

for (var i = 0; i < s.length; i++)
{
s = s.replace ('\\', '<br>');
s = s.replace ('\\', '=');
}
document.getElementById ('udp_socket_plugin_output').innerHTML = s;
}

it works.. and is small...


Dirk

> _______________________________________________
> dev-tech-plugins mailing list
> dev-tech...@lists.mozilla.org
> https://lists.mozilla.org/listinfo/dev-tech-plugins
>

Dirk

unread,
Sep 3, 2010, 4:28:19 PM9/3/10
to taxilian, dev-tech...@lists.mozilla.org
taxilian wrote:
> I would avoid using NPN_Evaluate and instead use NPN_GetValue to get
> the window NPObject and then NPN_Invoke to call window.Array(), but
> that's just me.
>
> You can then add items to the array by using NPN_Invoke to call push
> on the array NPObject.
>
> FireBreath uses this; in fact, you might want to consider using
> FireBreath, as it simplifies all of this. http://firebreath.googlecode.com
>
> The FireBreath code for doing this can be found here:
> http://code.google.com/p/firebreath/source/browse/src/NpapiPlugin/NpapiBrowserHost.cpp#174
>
> Keep in mind that FireBreath uses an abstraction for this feature
> rather than using NPN_* methods directly, but at least the general
> concept is there, and if you dig a little you can find what is
> actually getting called.
>
> Richard

ok.. i had a look at firebreath and it's abstraction... it made me use google and i found this one instead..

http://stackoverflow.com/questions/1896166/npvariant-to-string-array

so this is how far i have come getting an array from javascript into the npruntime sample

bool
ScriptablePluginObject::Invoke (NPIdentifier name, const NPVariant * args,
uint32_t argCount, NPVariant * result)
{

if (name == sTest_id)
{
NPObject *inObject = args[0].value.objectValue;
NPVariant npvLength;
NPN_GetProperty(mNpp, inObject, NPN_GetStringIdentifier("length"), &npvLength);
for (uint32_t i = 0; i < npvLength.value.intValue; i++)
{
NPVariant curValue;
NPN_GetProperty(mNpp, inObject, NPN_GetIntIdentifier(i), &curValue);
printf ("%s", curValue);
}
return true;
}
return false;
}


it doesn't work

Dirk

Dirk

unread,
Sep 3, 2010, 4:38:47 PM9/3/10
to taxilian, dev-tech...@lists.mozilla.org
Dirk wrote:

> taxilian wrote:
>> I would avoid using NPN_Evaluate and instead use NPN_GetValue to get
>> the window NPObject and then NPN_Invoke to call window.Array(), but
>> that's just me.
>>
>> You can then add items to the array by using NPN_Invoke to call push
>> on the array NPObject.
>>
>> FireBreath uses this; in fact, you might want to consider using
>> FireBreath, as it simplifies all of this. http://firebreath.googlecode.com
>>
>> The FireBreath code for doing this can be found here:
>> http://code.google.com/p/firebreath/source/browse/src/NpapiPlugin/NpapiBrowserHost.cpp#174
>>
>> Keep in mind that FireBreath uses an abstraction for this feature
>> rather than using NPN_* methods directly, but at least the general
>> concept is there, and if you dig a little you can find what is
>> actually getting called.
>>
>> Richard
>
> ok.. i had a look at firebreath and it's abstraction... it made me use google and i found this one instead..
>
> http://stackoverflow.com/questions/1896166/npvariant-to-string-array
>
> so this is how far i have come getting an array from javascript into the npruntime sample
>
> bool
> ScriptablePluginObject::Invoke (NPIdentifier name, const NPVariant * args,
> uint32_t argCount, NPVariant * result)
> {
>
> if (name == sTest_id)
> {
> NPObject *inObject = args[0].value.objectValue;
> NPVariant npvLength;
> NPN_GetProperty(mNpp, inObject, NPN_GetStringIdentifier("length"), &npvLength);
> for (uint32_t i = 0; i < npvLength.value.intValue; i++)
> {
> NPVariant curValue;
> NPN_GetProperty(mNpp, inObject, NPN_GetIntIdentifier(i), &curValue);
> printf ("%s", curValue);
> }
> return true;
> }
> return false;
> }
>
>
> it doesn't work
>
>
>
> Dirk

sorry.. i got demotivated too fast... it *does* work now... i fixed what i did with the printf there... i am going to get the
other way working too now..

and, yes, i have realized that post is from you too... xD


Dirk

0 new messages