Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Highlighted
Regular Contributor
Posts: 53
Registered: ‎08-27-2013
My Device: none
My Carrier: none
Accepted Solution

BlackBerry JRE 5.0.0 BrowserField has no ScriptEngine

Hi,

 

Ive got a BrowserField:

BrowserFieldConfig config = new BrowserFieldConfig();
config.setProperty(BrowserFieldConfig.JAVASCRIPT_ENABLED, Boolean.TRUE);
this.browser = new BrowserField(config);

 

which returns null on:

this.browser.getScriptEngine();

 

Which prevents me from running any scripts from within the Java code, because browser.executeScript("somejavascript"); throws an IllegalStateException which states that the ScriptEngine does not exist. Which is kind of funny, because when I load JQuery its able to compile the script and have the BrowserFieldDebugger tell me, that JSON is undefined. Which is exactly why I want to run a script from inside the Java code in the first place. Well, I need to call javascript functions later on as well so I would have needed this functionality one way or the other. Btw.: browser.displayContent("javascript:somejavascript", someurl) seems to generate some kind of error page.

 

Testing on a 9700, building against BlackBerry JRE 5.0.0

 

Id like to have this App running by next thursday, so a quick (and helpful) response would be appreciated (if you need more information, ask away).

Thanks in advance.

Regular Contributor
Posts: 53
Registered: ‎08-27-2013
My Device: none
My Carrier: none

Re: BlackBerry JRE 5.0.0 BrowserField has no ScriptEngine

I did some more tests to see if the ScriptEngine gets initialized at some later point, maybe when the first Javascript file gets loaded. I did so using another Runnable:

ScriptEngine scriptEngine = mdBrowserField.getScriptEngine();
invokeLater(new Runnable() {
public void run() {
ScriptEngine scriptEngine = mdBrowserField.getScriptEngine();
int i = 0;
}

}, 500, scriptEngine == null);

 

Now the strange thing is, even though the run method gets executed after the request for the first HTML-File has been processed, the BrowserFieldDebugger does'nt give me any output, instead the run method gets executed over and over with the BrowserField doing nothing in the meantime and the scriptEngine field inside the run method being null everytime (the int = 0 is only there so i can set a breakpoint...).


P.S.: When i don't set breakpoints and just let it run for a few seconds, the scriptEngine will be there when i set a breakpoint in the run method. All this still seems a little strange to me, is there some way to ensure the ScriptEngine is available from the point the BrowserField gets instantiated? I did find a Javascript library that solves the JSON is undefined problem, but Object.defineProperty is undefined as well, and probably a lot more things, and id rather fix those issues in the java code than to end up with an infinite number of new JS libs.

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: BlackBerry JRE 5.0.0 BrowserField has no ScriptEngine

[ Edited ]

Sorry not an area of expertise, but I have some thoughts;

 

This code

invokeLater(new Runnable() { public void run() {

// thing to run

}, 500, flag);

 

will keep repeating if flag is true initially (it is not re-evaluated).  So I think you actually wanted to code something like:

 

Runnable testScriptEngine = new Runnable() {

public void run() {

ScriptEngine scriptEngine = mdBrowserField.getScriptEngine();

if ( scriptEngine == null ) {

invokeLater(this, 500, false);

} else {

Dialog.alert("Script Engine found");

}

};

invokeLater(testScriptEngine, 500, false);

 

If I was looking for something that would be triggered when the browserField was 'working', I would look at documentLoaded() method of the BrowserField Listener:

http://supportforums.blackberry.com/t5/Java-Development/BrowserField-Sample-Code-Using-the-BrowserFi...

 

I'm guessing there is significant overhead in using a Script Engine, so I can see a reason for not creating a Script Engine until you need one - so it does not surprise me that one is not created at BrowserField instantiation. 

Regular Contributor
Posts: 53
Registered: ‎08-27-2013
My Device: none
My Carrier: none

Re: BlackBerry JRE 5.0.0 BrowserField has no ScriptEngine

Thanks a lot!

BrowserFieldListener.documentCreated(BrowserField browserField, ScriptEngine scriptEngine, Document document) is exactly what I was looking for.