BrowserComponent and JavaScript

40 views
Skip to first unread message

Yishai Steinhart

unread,
Jul 12, 2018, 11:45:00 PM7/12/18
to CodenameOne Discussions
Hi

Still running in the Simulator. I have an html file, which I added to it (copies & pasted, not included) a relatively large 3rd party JS library. Wrote a JS function to do call the library and a button to invoke it. On the Chrome all works good. I than load the html file (as text) and set it to be the page in BrowserComponent and make a call to my function. (BrowserComponent is hidden, as I just want to use it as a bridge to the JS SDK). My function get called, but it fails as it does not recognize variables defined in the SDK (which is embedded in the file). Same file, same call. What can be the difference between the way it runs on Chrome vs the BrowserComponent? The SDK might be using Node.js calls and maybe Chrome can handle them and BrowserComponent not? The SDK is about 500KB. Is there a size issue with BrowserComponent ? Any other idea? 

Thanks

-Yishai

Shai Almog

unread,
Jul 13, 2018, 12:42:00 AM7/13/18
to CodenameOne Discussions
Hi,
did you use the onload event to inject the JS code? That's something I always stumble on.

Yishai Steinhart

unread,
Jul 13, 2018, 1:24:18 PM7/13/18
to CodenameOne Discussions
 Hi Shai

I wanted to remove any potential issues, so for this test I pasted the actual js inside the html file.

I attach the html file. Drag it on chrome and press the two buttons and you can see what it does.

On the java side, my call looks like that. When called it fails with "'undefined' is not an object (evaluating 'StellarSdk.Keypair')". The problem is that the StellarSdk variable from the SDK is not defined. Can this be a loading/timimg issue?

Thanks

-Yishai

private void loadSDK ()

{

browser = new BrowserComponent(); 

String indexFile = MiscUtilities.loadTextFile("index.html");

browser.setPage(indexFile, null);

browser.addWebEventListener("onLoad", new ActionListener<ActionEvent> ()

{

public void actionPerformed(ActionEvent evt)

{

libLoaded = true;

}

});

browser.reload();

}

public boolean randomKeyPair ()

{

BrowserComponent.JSRef ref = this.browser.executeAndWait("callback.onSuccess(getRandomKeypair())");

return false;

}

index.html

Shai Almog

unread,
Jul 14, 2018, 12:10:48 AM7/14/18
to CodenameOne Discussions
Hi,
I don't think you need to reload. 

I'm not sure how this is supposed to work. Where is randomKeyPair() invoked?

Did you test on the device or simulator? Notice that the browser on the simulator is a problematic version of webkit which might be broken in weird ways. The browser on the device should be more reliable.

Steve Hannah

unread,
Jul 14, 2018, 8:30:36 PM7/14/18
to codenameone...@googlegroups.com
I just took a look at your code, I don't see how StellarSdk could be defined in the context of your getRandomKeypair() function.  I did a find on the index.html for "StellarSdk" and found it only mentioned inside the minified snippet with "var StellarSdk=..." which means that it's a local variable.  Doesn't look like this gets exported to global scope anywhere, thus why it's undefined.

Steve

--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discussions+unsub...@googlegroups.com.
Visit this group at https://groups.google.com/group/codenameone-discussions.
To view this discussion on the web visit https://groups.google.com/d/msgid/codenameone-discussions/59a5e691-bc09-4a9d-bc34-53f012811f35%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Steve Hannah
Software Developer
Codename One

Yishai Steinhart

unread,
Jul 15, 2018, 9:23:26 PM7/15/18
to CodenameOne Discussions
Thanks Steve

As far as I can see the scope of StellarSdk is global, not local, so it should be visible for all JS. In addition, the same html runs as is in Chrome, which further suggests it is not a local variable. If the browser is broken  in the simulator, this is really bad, as this function is central and with out it, the simulator has little use. Any other options to run JS in the simulator?

Thanks

-Yishai

Shai Almog

unread,
Jul 15, 2018, 11:57:42 PM7/15/18
to CodenameOne Discussions
JS runs in the simulator, when I say "broken" I mean it has issues with some syntaxes as it's an older fork of webkit.
If it works on the device but fails in the simulator it might give us a hint of what's failing.
Reply all
Reply to author
Forward
0 new messages