views:

24

answers:

0

Hi, 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 HasProperty 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, &params[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, &params[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