Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Rhino and Java browser

127 views
Skip to first unread message

Igor Bukanov

unread,
Jun 9, 2004, 9:57:23 AM6/9/04
to Carlos Barrera
Carlos Barrera wrote:
> Hello
>
> I´d like to known if you can help me with information about How
> integrated
> javacripts support to a simple browser using Rhino.
>

[ This is also posted to netscape.public.mozilla.jseng newsgroup ]

It is a long shot but if you need to display simple html/JavaScript
mixture that you fully control, you may start with the following:

1. Create java class that would represent your window object for each frame:

public class MyWindow
{
org.w3c.dom.Document document;

MyWindow(org.w3c.dom.Document document)
{
this.document = document;
}

public void alert(String message)
{
System.out.println(message);
}

public org.w3c.dom.Document getDocument()
{
return document;
}
}

2. Create Java class for you navigator object:

public class MyNavigator
{
public String getAppName()
{
return "My Browser";
}

// other properties...
}


2. Initialize scope for the standard libraries and add navigator object
to it:

Scriptable libraryScope;

Context cx = Context.enter();
try {
libraryScope = cx.initStandardObjects();
MyNavigator n = new MyNavigator();
ScriptableObject.putProperty(libraryScope, "navigator",
Context.toObject(n, libraryScope));
} finally {
Context.exit();
}

3. For each document that can potentially contain scripts before
document parsing create DOM document object and then create MyWindow
instance and create top level JavaScript scope based on it with properly
initialized prototype chain:

Scriptable documentScope;

MyWindow window = new MyWindow(domDocument);

Context cx = Context.enter();
try {
Scriptable windowJS = Context.toObject(window, libraryScope);
windowJS.setParent(null);
windowJS.setPrototype(libraryScope);
documentScope = cx.newObject(windowJS);
documentScope.setParent(null);
documentScope.setPrototype(windowJS);
ScriptableObject.putProperty(documentScope, "window",
documentScope);
ScriptableObject.putProperty(documentScope, "self",
documentScope);
} finally {
Context.exit();
}

In this way you creates documentScope that will be used to store
script-specific variables that contain 2 properties window and self
which can be used as an alias for "this" during script execution which
is the defacto-standard since NetscapeNavigator 2.0.

The prototype chain of this scope points to JS wrapper of MyWindow which
in turns points to libraryScope with all standard objects. In this way
all methods defined in window are visible to scripts.

4. During DOM building for each script tag extract its JS source and
execute it in the following way:

String script_text = ...;

Context cx = Context.enter();
try {
// Force pure interpretation mode to avoid class generation.
// This is the fastest way to run vast majority of scripts
cx.setOptimizationLevel(-1);

cx.evaluateString(documentScope, script_text, "script_url",
number_of_the_line_with_script_tag_or_0, null);

} finally {
Context.exit();
}


With this setup you should be able to run scripts like:

<div id = "mydiv">
...
</div>
<script>
alert(navigator.appName);
var elem = document.getElementById("mydiv");
alert("div="+elem);
....
</script>

5. Add the rest of necessary properties ...


If you are going to evaluate untrusted scripts, do NOT forget to setup
proper security (SecurityController is your friend here).


Regards, Igor

nicokr...@googlemail.com

unread,
Jun 10, 2012, 11:50:05 AM6/10/12
to Carlos Barrera
> ...
> </div>
> <script>
> alert(navigator.appName);
> var elem = document.getElementById("mydiv");
> alert("div="+elem);
> ....
> </script>
>
>
>
> 5. Add the rest of necessary properties ...
>
>
> If you are going to evaluate untrusted scripts, do NOT forget to setup
> proper security (SecurityController is your friend here).
>
>
> Regards, Igor

Thanx Igor,

this is a decent hint to start playin' around a lot!

I think it would be nice for all the people who read this (including myself) if there was a minimal working source example. i get it together by myself. i actually use the HtmlUnit code as a basis to save time and tests. i plan to make that minimal version easily extendable. when it's done, i'll post it here

regards,
Nico
0 new messages