NPN_Evaluate execute javascript in browser

来源:互联网 发布:丐姐捏脸数据 编辑:程序博客网 时间:2024/06/09 15:25

先上代码:

int16_t CPlugin::HandleEvent (NPP instance,void * aEvent) {NPEvent * event = (NPEvent *)aEvent;switch (event->event) {case WM_PAINT: {if(!m_Window)break;// get the dirty rectangle to update or repaint the whole windowRECT * drc = (RECT *)event->lParam;if(drc)FillRect((HDC)event->wParam, drc, (HBRUSH)(COLOR_ACTIVECAPTION+1));else {RECT rc;rc.bottom = m_Window->y + m_Window->height;rc.left   = m_Window->x;rc.right  = m_Window->x + m_Window->width;rc.top    = m_Window->y;FillRect((HDC)event->wParam, &rc, (HBRUSH)(COLOR_ACTIVECAPTION+1));}break;}case WM_LBUTTONDOWN:Beep(1000,200);  case WM_MOUSEMOVE:  {m_oldX=LOWORD(event->lParam);  m_oldY=HIWORD(event->lParam);  m_newX = m_oldX;m_newY = m_oldY;CPlugin *plugin = (CPlugin *)instance->pdata;NPObject*object=NULL;npnfuncs->getvalue(m_pNPInstance,NPNVWindowNPObject,&object);char X[10];itoa (m_newX,X,10);char Y[10];itoa (m_newY,Y,10);char output[300]="";char *str1 = "document.getElementById('mouse_coordinate').innerHTML = '<h3>x : ";char *str2 = ",y:";char *str3 = "</h3>';";strcat(output,str1);strcat(output,X);strcat(output,str2);strcat(output,Y);strcat(output,str3);NPString str;str.UTF8Characters =output;str.UTF8Length = strlen(str.UTF8Characters);NPVariant result;bool ret = npnfuncs->evaluate(instance,object, &str, &result);npnfuncs->releasevariantvalue(&result);npnfuncs->releaseobject(object);break;}default:return 0;}return 1;}

完成的功能:

1)在插件区域绘制纯色矩形

2)监控鼠标在插件区域点击事件,点击触发一次鸣叫

Beep(1000,200);

3) 监控鼠标在插件区域内点击和移动事件,时刻改变HTml div id = “mouse_coordinate”的内容,为鼠标的坐标



分析函数:

npnfuncs->evaluate(instance,object, &str, &result);
instance 为NPP实体,为NP_HandEvent函数引进来的,不作处理

要注意两点:分别是result参数和object参数。

1)object参数:值的获取:

NPObject*object=NULL;npnfuncs->getvalue(m_pNPInstance,NPNVWindowNPObject,&object);
这里一定是获取的是一个pointer,而不是一个实体。所以在object前面必须得加个&,不然执行evaluate函数的时候会发生:access violation错误。

2)result参数,必须不为NULL,这也就是mozill0-2.0里面npruntime源码不能在chrome里面执行的原因。

官方接口文档中也有提示到:

Parameters

The function has the following parameters:

npp
The NPP indicating which plugin instance's window to evaluate the script in.
npobj
The scope object.
script
The script to evaluate.
result
On return, contains the value returned by the script. Can't be NULL.

引用:https://developer.mozilla.org/en-US/docs/NPN_Evaluate
after testing,it can run on firefox and chrome
原创粉丝点击