Kirix Support Forums

Javascript -> C++ communication

Please post all general questions, comments, bug reports, and any other wxWebConnect feedback here.

Javascript -> C++ communication

Postby peterl on Sat Jul 11, 2009 2:22 pm

hi there, great library!

I was able to call Javascript from C++ with wxWebControl::Execute(), then JS to ActionScript and ActionScript to JS, but am stuck trying to callback my C++ code from JavaScript.

Deriving wxWebControl and adding an event handler I am able to intercept mouse events, but can't seem to intercept any DOM event. From Javascript I used this code to send an event to the document:

var e = document.createEvent("myeventtype");

// bubbling, can't cancel default
e.initEvent("myeventtype", true, true);

e.data = "test";
e.datatype = "string";

// synchronous!
window.dispatchEvent(e);

Do I need to retrieve the document DOM in C++ and add an event handler manually?

Any suggestions as to which type of event I could generate to pass information from JS to C++? If possible I'd like to use a custom event and not hack a reserved event since it may later interfere with a more complex HTML page.

thx,

-- p
Last edited by peterl on Sun Jul 12, 2009 6:39 pm, edited 1 time in total.
peterl
Registered User
 
Posts: 5
Joined: Sat Jul 11, 2009 2:08 pm

Re: Javascript -> C++ communication

Postby Ben on Sun Jul 12, 2009 8:16 am

Hi there,

Great idea you have there. We'll be needing it ourselves soon, too, so I can imagine that something official along these lines will be coded soon. For now, try this:

1) Go to webcontrol.cpp, around like 280, and add an event listener with your js event name
2) Next, add a handler in BrowserChrome::HandleEvent() for your event

See if the break point hits. If it does, you're pretty close. Please let me know how this works out for you. If you have some success, I'm sure we can make a programmatic API or this functionality that won't involve modifying the library's source.

Ben
Ben Williams
Kirix Support Team
User avatar
Ben
Kirix Support Team
 
Posts: 525
Joined: Mon Dec 19, 2005 6:29 am

Re: Javascript -> C++ communication

Postby peterl on Sun Jul 12, 2009 6:38 pm

Hi Ben,

I tried that and a few other hacks but nothing worked: I put a log message in BrowserChrome::HandleEvent() and all I got was existing messages, not the custom one.

Although AddEventListener() doesn't return an error it seems XULRunner won't register any event type that it doesn't already know. Then I found an article about sending custom events. It seems you need to modify the trunk source, although there may be another way:

https://developer.mozilla.org/En/Creati ... _Pass_Data

I also found this Mozilla custom event interface but didn't know where to access it:

http://www.oxymoronical.com/experiments ... extIEvents

Another potential solution would be to create XPCOM extensions though I'm not familiar with them:

https://developer.mozilla.org/en/Creati ... components
http://www.iosart.com/firefox/xpcom/

There is an official "custom event" in DOM Model 3 defined by W3C but it seems no browser has implemented it yet. Its type name is "customEvent" so I'd refrain from using that for future compatibility.

thx,

-- p
peterl
Registered User
 
Posts: 5
Joined: Sat Jul 11, 2009 2:08 pm

Re: Javascript -> C++ communication

Postby peterl on Mon Jul 13, 2009 5:44 pm

I also just found out about js-ctypes, which seems considerably easier to use, but a bitch to build (not part of the standard extensions):

https://wiki.mozilla.org/JSctypes

cheers,

-- p
peterl
Registered User
 
Posts: 5
Joined: Sat Jul 11, 2009 2:08 pm

Re: Javascript -> C++ communication

Postby michaeliola on Sat Nov 14, 2009 11:01 am

hi, great library !

I am trying to run the following page in program testapp
(orginal binary taken from the kirix site)
under Windows Vista:
Code: Select all
<html>
<head>
   <script>
      function test() {
         var e;
         try {
            e = document.createEvent("myeventtype");
         } catch (e) {
            alert("createEvent falied " + e);
            return;
         }   
         e.initEvent("myeventtype", true, true);
         e.data = "test";
         e.datatype = "string";
         window.dispatchEvent(e);      
         alert("qqq");
      }
   </script>
</head>
<body>
<button onClick="test()">test</button>
</body>
</html>


The following message appears:

createEvent falied
[Exception... "Object cannot be created in this context" code: "9" nsresult: "0x80530009 (NS_ERROR_DOM_NOT_SUPPORTED_ERR)"]
location: "file:///C:/webconnect-1.0/webconnect/test.html Line: 7"]
Am I doing something wrong ?
I have also tried to add event listener but with no effect.
Michael
michaeliola
Registered User
 
Posts: 1
Joined: Sat Nov 14, 2009 10:21 am

Return to wxWebConnect Questions, Thoughts & Feedback