Google chrome extension npruntime和addEventListener

Google chrome extension npruntime和addEventListener,google-chrome-extension,Google Chrome Extension,我正在编写一个chrome插件,我想在其中注册click事件,这意味着每当我们单击DOM窗口时,插件中的处理程序都会被调用。为此,我使用CPlugin类。构造函数是从NPP_New/argument/调用的 当我运行浏览器并单击任意位置时,我注意到ScriptablePluginObject的HasProperty和GetProperty函数使用标识符名handleEvent调用。 我不明白如何处理这件事 有人能指引我吗 /////////////CODE///////////////////

我正在编写一个chrome插件,我想在其中注册click事件,这意味着每当我们单击DOM窗口时,插件中的处理程序都会被调用。为此,我使用CPlugin类。构造函数是从NPP_New/argument/调用的

当我运行浏览器并单击任意位置时,我注意到ScriptablePluginObject的HasProperty和GetProperty函数使用标识符名handleEvent调用。 我不明白如何处理这件事

有人能指引我吗

/////////////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///////////
以上两个类都取自谷歌代码。我只是在NPObject上添加事件侦听器。 怎么了?有什么想法吗


-Abhay

您的车已经在赛道上了,但您还需要一些改动:

创建一个全新的NPClass,您可以将此MouseClickeEvent命名为或其他类型。 在MouseClickEvent类的InvokeDefault中实现鼠标事件侦听器功能。 现在使用NPN_CreateObject从MouseClickeEvent实例创建NPObject 将该NPObject作为第二个参数传递给上面提到的addEventListener。 希望有帮助