Re: How to get the rendered dom element's coordinate infos in JAVAXPCOM

网事随风 <[email protected]> Mon, 19 May 2008 19:25:17 -0700 (PDT)
Newsgroups gmane.comp.mozilla.devel.java
Organization http://groups.google.com
Message-ID <ee65604f-bebd-4494-9a4d-44c79cb767c2__34143.5853195891$1211250698$gmane$org@t12g2000prg.googlegroups.com>
On 5月20日, 上午10时18分, 网事随风 <[email protected]> wrote:
> I want to know the exact position (X, Y, width and height) of dom
> element of a HTML. How can I achive this using JAVAXPCOM?

I provide a sample code snippet below, which can output some element's
position info by executing JavaScript. How can I achieve the same
goal  using pure Java code in JAVAXPCOM?



public class Test {
	public static void main(String [] args) {
		final String html = "<html><title>Snippet</title><body><p
id='myid'>Best Friends</p><p id='myid2'>Cat and Dog</p></body></
html>";
		Display display = new Display();
		final Shell shell = new Shell(display);
		shell.setLayout(new FillLayout());
		final Browser browser;
		try {
			browser = new Browser(shell, SWT.MOZILLA);
		} catch (SWTError e) {
			System.out.println("Could not instantiate Browser: " +
e.getMessage());
			return;
		}
		browser.addStatusTextListener(new StatusTextListener() {
			public void changed(StatusTextEvent event) {
				browser.setData("query", event.text);
			}
		});
		browser.addProgressListener(new ProgressListener() {
			public void changed(ProgressEvent event) {
			}
			public void completed(ProgressEvent event) {

				boolean result = browser.execute("window.status =
document.getBoxObjectFor( document.getElementById('myid') ).screenX;");
				if (!result) {
					/* Script may fail or may not be supported on certain platforms.
*/
					System.out.println("Script was not executed.");
					return;
				}
				String value = (String)browser.getData("query");
				System.out.println("Node value: "+value);

			}
		});
		/* Load an HTML document */
		browser.setText(html);

		//shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}
}

_______________________________________________
dev-tech-java mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-java