Executing JavaScript Directly From the plugin

46 views
Skip to first unread message

Pedro Fraca

unread,
Nov 20, 2007, 10:27:16 AM11/20/07
to JNEXT discussion group
Hi, i'm developing a new feature to allow execute javascript code from
the plugin. If you consider interesting to add to JNEXT framework,
please tell me and I give you the code.

Best. Pedro.

Amnon David

unread,
Nov 20, 2007, 3:49:53 PM11/20/07
to jn...@googlegroups.com
Hi Pedro

I think it would be best to only add new functionality if it is
difficult to achieve with the current framework. You can run
Javascript code from a JNEXT plugin quite easily using the existing
framework by deriving a class from JSExt and implementing the
NotifyEvent method (in the same way it is done in the filereader
sample), Then you can invoke NotifyEvent using the JavaScript function
call as the string parameter. This will end up calling the JavaScript
function JNEXT_callback_native2js in the jnext.js file.

So all you have to do to add the ability to call any JavaScript
function is to modify the code to detect a function call in
JNEXT_callback_native2js (you can do this by using some unique
identifier that indicates that this callback is to be used to invoke
JavaScript code) and then use "eval" on the function that is passed to
it.

I hope my understanding of the feature you proposed is correct. Please
let me know if this implementation method suits your purpose.

-Amnon

Amnon David

unread,
Nov 20, 2007, 4:26:35 PM11/20/07
to jn...@googlegroups.com
On second thoughts, it would be better to hide the implementation
details from the plugin that wishes to execute the JavaScript function
by adding the protocol identifier to invoke JavaScript functions in a
new JSExt method in
plugins\common\plugin.cpp & plugin.h e.g:

void JSExt::CallJSFunc( const char* szFunc )
{
string strFunc = szEvent;
string strFileEvent = m_strObjId + " func " + strEvent;
SendPluginEvent( strFileEvent.c_str(), m_pContext );
}

and then modify the code in JNEXT_callback_native2js (in jnext.js) to
detect the "func" identifier and if it exists "eval" the next parameter.

In any case, I'll be happy if you would send me the code if you think
there are other issues that I haven't addressed here or that can be
implemented in an easier way.

All the best,
Amnon

Pedro Fraca

unread,
Nov 21, 2007, 3:11:00 AM11/21/07
to JNEXT discussion group
The Code is here.

pCJs2nCtrl->m_stringQueue.AddTail( strNewEvent );
LPOLECLIENTSITE f;
f =pCJs2nCtrl->GetClientSite();
IOleContainer* pContainer = NULL;
IHTMLDocument2* pDoc = NULL;
f->GetContainer(&pContainer );
if ( pContainer != NULL )
//Obtain a pointer to the IHTMLDocument Interface to access methods
and properties.
pContainer->QueryInterface(IID_IHTMLDocument2,(void**)&pDoc );
//************************
//Obtain a pointer to the HTML Window to execute javascript
IServiceProvider* pSP;
pContainer->QueryInterface(IID_IServiceProvider, (void **)&pSP);
IHTMLWindow2 * pWindow;
//The HTML Window Has methods to execute directly any script
pSP->QueryService(IID_IHTMLWindow2, IID_IHTMLWindow2, (void
**)&pWindow);
VARIANT Return;
HRESULT response;
CString command = "alert('JNEXT');"; //Any JavaScript code or
fuction call, var definition.
BSTR comandBSTR = command.AllocSysString();
response = pWindow-
>execScript(comandBSTR,CComBSTR("javascript"),&Return);

I think, that would be interesting do not make any call from
JavaScript, when you're using this method. With this option the
framework allows user to take two directions, using
JNEXT_callback_native2js or directly calling a JavaScript from the
framework. This code is for ActiveX Framework. If we consider ok, the
NPAPI side will be easy.

Pedro Fraca

unread,
Nov 21, 2007, 3:33:16 AM11/21/07
to JNEXT discussion group
The principal purpose of this new functionality is launch events from
Plugin without add it to the event register. With the next way: This
code is launched from plugin.

var evt = document.createEvent(\"Events\");

evt.initEvent(\"EventTest\", true, true);

dispatchEvent(evt);

This code is added to javascript or html.

addEventListener('EventText',FunctionEvent,false);

With this simple code the javascript function "FunctionEvent" will be
launched when the event was dispathed.

This code only works on Mozilla or Opera, I'm working to find the way
to create custom events on Internet Explorer.

Thomas C

unread,
Dec 21, 2007, 7:36:10 AM12/21/07
to JNEXT discussion group
Hi Pedro

Any chance of getting the complete sample for this? I'm looking for a
way to throw events in my nsapi plugin and let the listener code be in
javascript, and I'm wondering if I should define an idl interface for
the eventlistener or if I can do that in the way you described.
However a full example for mozilla/opera (as you mention) would be
great.

Cheers

Thomas

Pedro Fraca

unread,
Dec 27, 2007, 1:37:22 PM12/27/07
to JNEXT discussion group
Hi, Thomas. I'm now on holidays. On January 3rd, i will write the full
sample.

Regards, Pedro.

On 21 dic, 13:36, Thomas C <thomas.huusom.christen...@gmail.com>
wrote:

Pedro Fraca

unread,
Jan 3, 2008, 4:09:42 AM1/3/08
to JNEXT discussion group
Hi thomas this is the complete code to launch events.

bool DispatchEvent( const string& strEvent, void* pContext )
{
// This implementation is specific to NPAPI plugins.
NPP pNPInstance = (NPP)pContext;
NPString g;
NPObject* pluginObject;
char *script;
//Obtaining the object
if (NPERR_NO_ERROR != NPN_GetValue(pNPInstance,
NPNVPluginElementNPObject, &pluginObject)) {

return NULL;
}
string strAux = "var evt =
document.createEvent('Events');evt.initEvent('" + strEvent +"', true,
true);var f=dispatchEvent(evt);";
script =const_cast<char *> (strAux.c_str());
g.utf8characters = script;
g.utf8length = strlen(script);
NPVariant f;

return NPN_Evaluate(pNPInstance,pluginObject,&g,&f);


}

If you have any doubts please tell us.

Regards, Pedro.
Reply all
Reply to author
Forward
0 new messages