javascript
java SWT Browser实现浏览器功能并运行JavaScript代码
一、創(chuàng)建簡單的瀏覽器
import org.eclipse.swt.*; import org.eclipse.swt.browser.*; import org.eclipse.swt.layout.*; import org.eclipse.swt.widgets.*;public class Myswt3 {public static void main(String [] args) {Display display = new Display();final Shell shell = new Shell(display);shell.setLayout(new FillLayout());final Browser browser;try {browser = new Browser(shell, SWT.NONE);} catch (SWTError e) {System.out.println("Could not instantiate Browser: " + e.getMessage());display.dispose();return;}browser.setUrl("www.baidu.com"); //百度shell.open();while (!shell.isDisposed()) {if (!display.readAndDispatch()) display.sleep();}display.dispose(); }上面的代碼,除了browser.setURL(“www.baidu.com”)這句之外,其他都是必備的套路代碼,可以直接粘貼復(fù)制。
我簡要介紹一下:
1.Display 用于創(chuàng)建可視化的瀏覽器,它相當(dāng)于所有組件的父組件。
2.shell 作用應(yīng)該類似于窗體,它的parent是display
3.browser的try{}catch{}塊用于創(chuàng)建瀏覽器,增加健壯性,其中SWT.NONE是關(guān)鍵語句,這表示默認(rèn)調(diào)用系統(tǒng)的瀏覽器內(nèi)核。不過也可以更改參數(shù),比如SWT.Mozilla,調(diào)用火狐內(nèi)核,但是這樣還需要一些其他設(shè)置,比較麻煩,所以姑且用IE內(nèi)核吧。
4.最后幾行是加載和開始以及關(guān)閉釋放內(nèi)存的操作,全部都是套路。
二、運(yùn)行javaScript代碼
final String SCRIPT = "document.write('hello world')";browser.addProgressListener(ProgressListener.completedAdapter(event -> {browser.execute(SCRIPT);}));將上面兩句添加進(jìn)去,SCRIPT為javaScript代碼,你可以寫的很簡單,也可以寫的很復(fù)雜。
三、高級功能
這里面有一個(gè)很強(qiáng)大的功能是可以實(shí)現(xiàn)Html和java的交互,利用Javascript,可以將Html中的一些參數(shù)傳遞出來,在java的程序中進(jìn)行運(yùn)算,請看具體的實(shí)例
import org.eclipse.swt.*; import org.eclipse.swt.browser.*; import org.eclipse.swt.layout.*; import org.eclipse.swt.widgets.*;public class Myswt3 {public static void main(String [] args) {final String SCRIPT ="document.onmousedown = function(e) {if (!e) {e = window.event;} if (e) {mouseDown(e.clientX, e.clientY);}}";Display display = new Display();final Shell shell = new Shell(display);shell.setLayout(new FillLayout());final Browser browser;try {browser = new Browser(shell, SWT.NONE);} catch (SWTError e) {System.out.println("Could not instantiate Browser: " + e.getMessage());display.dispose();return;}browser.addProgressListener(ProgressListener.completedAdapter(event -> {final BrowserFunction function = new CustomFunction(browser, "mouseDown");browser.execute(SCRIPT);}));browser.setUrl("www.baidu.com");shell.open();while (!shell.isDisposed()) {if (!display.readAndDispatch()) display.sleep();}display.dispose(); }static class CustomFunction extends BrowserFunction {CustomFunction (Browser browser, String name) {super (browser, name);}@Overridepublic Object function (Object[] arguments) {System.out.println ("mouseDown: " + ((Number)arguments[0]).intValue() + "," + ((Number)arguments[1]).intValue());return null;} }}我來具體的解釋一下:
首先,在javaScript中定義了一個(gè)mousedown函數(shù),他有兩個(gè)參數(shù)就是x,y坐標(biāo),但是在代碼中沒有具體寫這個(gè)函數(shù)的代碼,只是聲明要用到這樣一個(gè)函數(shù),而這個(gè)函數(shù)可以在java代碼中進(jìn)行完善。
定義一個(gè)CustomFunction類繼承BrowserFunction類,在類中重寫 public Object function()函數(shù),而這個(gè)函數(shù)體就是mousedown的函數(shù)體
用于連接的語句就是:BrowserFunction function = new CustomFunction(browser, “mouseDown”);
總體來說就是,
1.javaScript中只簡單聲明函數(shù)和參數(shù)
2.以這個(gè)函數(shù)的名稱創(chuàng)建一個(gè)BrowserFunction對象
3.覆寫B(tài)rowserFunction中的public Object function 函數(shù),object arguments 是你自己定義的函數(shù)的要傳入的參數(shù),比如上例arguments[0],就時(shí)clientX,arguments[1]就是clientY,然后在內(nèi)部具體的實(shí)現(xiàn)這個(gè)函數(shù),最后加上return。
通過這個(gè)過程,將網(wǎng)頁中的clientX,和clientY傳到了java代碼中進(jìn)行了操作。
所以我們可以利用這個(gè)方法來實(shí)現(xiàn)更多的功能。
四、繼續(xù)擴(kuò)展
除了上面的方法進(jìn)行交互外還有一種簡單的方法,可以用更短的語句實(shí)現(xiàn)更多的功能。
browser.evaluate("return document.getElementById('myid').childNodes[0].nodeValue;")可以使用evaluate函數(shù)直接返回值,其中return document.getElementById(‘myid’).childNodes[0].nodeValue;是JavaScript代碼。
總結(jié)
以上是生活随笔為你收集整理的java SWT Browser实现浏览器功能并运行JavaScript代码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: history模式监听_面试题:VueR
- 下一篇: Spring上传时报Invalid CS