Solved

Execute method in JavascriptInterface after page has finished loading

Posted on 2012-04-02
1
706 Views
Last Modified: 2012-04-02
I would like to be able to do some processing on the html and javascript of a page before it is presented to the user of my app. The page uses window.open for all the links and that causes a new browser window, not controlled by my app, to be opened, which destroys to illusion of using an app. This is what I have right now, which I think will work for the most part.

    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

Open in new window


The problem I'll have with it, is that I'm not sure how I would make the page execute LinkFixer.processPage when the page finishes loading. Other than that, I think the code will work. Any suggestions?
0
Comment
Question by:HDM
1 Comment
 

Accepted Solution

by:
HDM earned 0 total points
ID: 37797534
I wound up making a new WebChromeClient and overriding the onProgressChanged method to fix the links after newProgress is 100 or greater.

    browser.setWebChromeClient(new WebChromeClient()
		{
			public void onProgressChanged(WebView webView, int newProgress)
			{
				super.onProgressChanged(webView,newProgress);
				if(newProgress >= 100)
				{
					System.out.println("Done loading");
					browser.loadUrl("javascript:document.getElementsByTagName(\"html\")[0].innerHTML = window.LinkFixer.processPage((document.getElementsByTagName(\"html\")[0].innerHTML))");
				}
			}
		});

Open in new window


Which works exactly how I want it to
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
saving voicemail from mobile phone to pc.. 4 82
eclipse buid path vs tomcat lib path 10 23
fb messenger security and privacy 15 61
why no phone sync 11 28
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Here, we have shared an easy step by step tutorial on how to download instagram images and videos on PC, Android Mobile and iOS mobile.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question