npruntime & addEventListener
I am writing a chrome plugin in which I want to register click event, means whenever we click on DOM window, the handler inside plugin will be called. For that I am using CPlugin class. The constructor is called from NPP_New(/argument/).
When I run the browser and click anywhere, I noticed that ScriptablePluginObject's HasPropert开发者_如何学编程y and GetProperty function called with identifier name "handleEvent". I don't understand how to handle the event.
Can anyone guide me please?
/////////////CODE///////////////////
static NPIdentifier sFunction_id;
// Called from NPP_New()
CPlugin::CPlugin(NPP pNPInstance) :
    m_pNPInstance(pNPInstance),
    m_pNPStream(NULL),
    m_bInitialized(FALSE),
    m_pScriptableObject(NULL)
{
    bool boRet;
    NPError rv;
    char szText[300];
    LogMessage("CPlugin::CPlugin::Enter");
    sFunction_id = NPN_GetStringIdentifier("handleEvent");
    rv = NPN_GetValue(m_pNPInstance, NPNVWindowNPObject, &sWindowObj);
    if (NPERR_NO_ERROR != rv)
    {
        LogMessage("CPlugin::CPlugin::NPN_GetValue() failed.");
    }
    NPObject *scriptObj = NPN_CreateObject(m_pNPInstance, GET_NPOBJECT_CLASS(ScriptablePluginObject));
    if (!scriptObj)
    {
        LogMessage("CPlugin::CPlugin::NPN_CreateObject failed");
    }
    NPVariant params[3]; 
    // arg0: event type 
    STRINGZ_TO_NPVARIANT("click", params[0]);
    // arg1: listener 
    params[1].type = NPVariantType_Object; 
    params[1].value.objectValue = scriptObj;
    // arg2: useCapture 
    params[2].type = NPVariantType_Bool; 
    params[2].value.boolValue = true;
    NPIdentifier addEventListener_id =  NPN_GetStringIdentifier("addEventListener"); 
    NPVariant result_add; 
    // windowObject.addEventListener("click", listener, false); 
    if (!NPN_Invoke(m_pNPInstance, sWindowObj, addEventListener_id, ¶ms[0], 3, &result_add))
    {
        LogMessage("CPlugin::CPlugin::NPN_Invoke for addEventListener failed");
    }
    NPIdentifier removeEventListener_id = NPN_GetStringIdentifier("removeEventListener");
    NPVariant result_remove;
    // windowObject.removeEventListener("click", listener, false); 
    if (!NPN_Invoke(m_pNPInstance, sWindowObj, removeEventListener_id, ¶ms[0], 3, &result_remove))
    {
        LogMessage("CPlugin::CPlugin::NPN_Invoke for removeEventListener failed");
    }
    NPN_ReleaseVariantValue(&result_add);
    NPN_ReleaseVariantValue(&result_remove);
    NPN_ReleaseObject(scriptObj);
    const char *ua = "This is test plugin";//NPN_UserAgent(m_pNPInstance);
    strcpy(m_String, ua);
    LogMessage("CPlugin::CPlugin::Exit");
}
// In HasProperty and GetProperty, nothing has been done.
bool
ScriptablePluginObject::HasProperty(NPIdentifier name)
{
    LogMessage("ScriptablePluginObject::HasProperty");
    char *nam = NPN_UTF8FromIdentifier(name);
    LogMessage(nam);
    NPN_MemFree(nam);
    return true;
}
bool
ScriptablePluginObject::GetProperty(NPIdentifier name, NPVariant *result)
{
    LogMessage("ScriptablePluginObject::GetProperty");
    char *nam = NPN_UTF8FromIdentifier(name);
    LogMessage(nam);
    NPN_MemFree(nam);
    return true;
}
///////////CODE///////////
Both of the above classes are taken from google code. I am only adding event listener on NPObject. What is wrong with it? Any idea?
-Abhay
Your on the track, but you require some more changes:
- Create a brand new NPClass, you can name thisMouseClickEventor some sort.
- Implement your mouse event listener functionality in the InvokeDefaultof theMouseClickEventclass.
- Now with NPN_CreateObjectcreate yourNPObjectfrom the instance ofMouseClickEvent
- You pass that NPObjectas a second parameter to the addEventListener you mentioned above.
Hope that helps!
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论