Welcome guest, is this your first visit? Create Account now to join.
Results 1 to 2 of 2

This is a discussion on npruntime & addEventListener within the Plugins Development section, part of the Chrome Plugins category: Hi, I am writing a chrome plugin in which I want to register click event, means whenever we click on ...


  1. #1
    Abhay is offline Junior Member
    Join Date
    May 2010
    Posts
    7

    Default npruntime & addEventListener

    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

  2. #2
    jcunicorn is offline Junior Member
    Join Date
    Jun 2010
    Posts
    5

    Default

    I am now having exactly the same problem as you. Has anyone succeeded in loading a NPAPI plugin? Could you provide any demo?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •