首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >npruntime & addEventListener

npruntime & addEventListener
EN

Stack Overflow用户
提问于 2010-05-19 13:21:11
回答 1查看 613关注 0票数 0

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

当我运行浏览器并单击任何地方时,我注意到ScriptablePluginObject的HasProperty和GetProperty函数使用标识符名"handleEvent“调用。我不知道怎么处理这件事。

有人能指点我吗?

代码语言:javascript
复制
/////////////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///////////

以上两个类都是从google代码中提取的。我只是在NPObject上添加事件侦听器。这是怎么回事?有什么想法吗?

-Abhay

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-05-02 21:16:51

你在轨道上,但你需要更多的改变:

sort.

  • Implement

  • 创建了一个全新的NPClass,您可以在MouseClickEvent类的InvokeDefault中将这个MouseClickEvent命名为MouseClickEvent或某些NPClass鼠标事件侦听器功能。现在,

  • NPN_CreateObject一起创建NPObjectNPObject作为第二个参数传递给上面提到的addEventListener。H 216G 217

希望这能帮上忙!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2865820

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档