0
我希望能夠在呈現給我的應用的用戶之前,對頁面的html和javascript做一些處理。該頁面對所有鏈接使用window.open,並導致一個不受我的應用程序控制的新瀏覽器窗口被打開,這破壞了使用應用程序的錯覺。這就是我現在所擁有的,我認爲這在大多數情況下都會起作用。在頁面加載完成後在JavascriptInterface中執行方法
class LinkFixer
{
static HashMap<String,String>pages = new HashMap<String,String>();//static so multiple pages can access it
public void processPage(String html)
{
html = html.replace("window.open","LinkFixer.openInNewTab");
}
public void openInNewTab(String url, String targetName)
{
if(!pages.containsKey(targetName))//targetName is unique for every link
{
pages.put(targetName,url);
//add new tab to my TabHost, which will contain a WebView with page from url loaded
}
//move user to tab
}
}
WebView browser = (WebView)findViewById(R.id.browser);
browser.getSettings().setJavaScriptEnabled(true);
browser.addJavascriptInterface(new LinkFixer(), "LinkFixer");
browser.loadUrl("http://serverIP:port/mobilecontrol.html");
//something to make the browser execute LinkFixer.processPage goes here
我會用它,問題是,我不知道我怎麼會做網頁執行LinkFixer.processPage當網頁加載完成。除此之外,我認爲代碼將起作用。有什麼建議麼?