r_tex
r_tex

Reputation: 77

How to get Plugin Object in nested javascript functions NPAPI

Thanks in advance !!!!!

var pluginObject = null;
function init()
{
    var pluginObject = document.getElementById('pluginObj'); //This is  object for my Plugin.
    pluginObject.onstartevent = handleEvent(); // This is working

    //now i am calling one function in plugin that will return NPObject  using invoke default
    pluginObject.startEvent(function(e) {
        e.onstartEvent = handleEvent(); //This is not working ......how to make it work  
    });
}
function handleEvent(e)
{
    if(e)
    {
        alert(e);
    }
}
<body onload = "init()" > </body>

Here i am calling init function from body and then creating plugin object and then calling one property onstartEvent that i am taking care inside plugin. Problem is "e.onstartEvent" how to capture this object and invoke here . i am not getting it . For PluginObject i am using NPNVPlUGINOBJECT and then checking property "onstartEvent" if it is present then checking for has method and finally invoke on the PLUGIN object.

//My Plugin Code is as follows :

bool ScriptablePluginObject::Invoke(NPIdentifier name, const NPVariant *args, uint32_t argCount, NPVariant *result)
{
    char *pFunc = NPN_UTF8FromIdentifier(name);
    if(!strcmp("startEvent",pFunc))
    {
        NPObject * argsVal = args[0].value.objectValue;  
        NPVariant valueToSend;
        NPVariant returnValue;
        valueToSend.type = NPVariantType_Object;
        valueToSend.value.objectValue = this; 
        NPN_InvokeDefault(mNpp,argsVal,&valueToSend,1,&returnValue); 
        return true;
    }
    return false;
}

Now in HasProperty checking in the current class property named of "onstartEvent"

bool ScriptablePluginObject::HasProperty(NPIdentifier name)
{
    char *pProp = NPN_UTF8FromIdentifier(name);
    //Check which Properties are available
    if( !strcmp( "onstartEvent", pProp ) )
    {
        return true;
    }
    return false;
}

Now in GetProperty i am finally checking and returning some integer value to the //handleEvent function

bool ScriptablePluginObject::GetProperty(NPIdentifier name, NPVariant *result)
{
    VOID_TO_NPVARIANT(*result);
    char *pProp = NPN_UTF8FromIdentifier(name);
    //onstartEvent

    if(!strcmp("onstartEvent", pProp))
    {
        //Calling e.onstartEvent will come here in GetProperty
        NPObject * pluginObj = NULL;   
        NPN_GetValue(mNpp,NPNVPluginElementNPObject,&pluginObj);
        NPIdentifier id = NPN_GetStringIdentifier("onstartEvent");
        bool val  = NPN_HasMethod(mNpp,pluginObj,id);
        NPVariant value;
        NPVariant retVal;
        value.type = NPVariantType_Int32; //now returning some value if val is true.
        value.value.intValue = 20;
        if(val==true)
        {
            //Finally returning value to the handle Event function in JavaScript
            NPN_Invoke(mNpp,pluginObj,id,&value,1,&retVal); 
        }
        return true;
    }
    return true;
}

Upvotes: 0

Views: 894

Answers (1)

taxilian
taxilian

Reputation: 14324

I'm not sure I really follow what you're trying to do; from the looks of things, though, when you call the method startEvent on your plugin you pass it a function (NPObject) that will get invoked (with InvokeDefault) with a single argument "e"

You then want "e" to be the plugin? if that's the case, then all you need to do is wrap the NPObject for the plugin in a NPVariant and send it as the first parameter to the function call with InvokeDefault.


Edit:

So you have posted your NPAPI code; you have the following line executing in your callback:

e.onstartEvent = handleEvent();

Did you mean to do this? You're executing handleEvent and assigning the result to "onstartEvent" on the plugin DOM element. Did you mean to assign the function to "onstartEvent", perhaps? In that case you shouldn't have the () at the end; that causes the function to be called (and without a valid value for e).

Further, you're trying to invoke "onstartEvent" on the DOM element, but you'd be much better off using GetProperty to get the function's NPObject and then InvokeDefault on that; of course, since you're never getting the property GetProperty isn't ever called and thus your code for that isn't ever running anyway.

Oh, and since you return true from HasProperty for "onstartEvent" you can't set the function on the DOM element's property of the same name; the DOM element can claim the property or your plugin can, but you're trying to use it both ways, which won't work. Thus, since you don't have a SetProperty your assignment will fail just after your function is called with no arguments.

Hope that helps

Upvotes: 1

Related Questions