Solved

Execute method in JavascriptInterface after page has finished loading

Posted on 2012-04-02
1
709 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
"Check Failed!Check Your OTA Servier Argent" 6 331
Android development question 2 54
sql import cannot be resolved jsp 3 49
Java basic valueOf question 1 29
Often, people trade privacy and security for convenience. However in today's concrete jungle, this is an extremely foolish decision considering the vast amount of technologies being used against consumer interest. First off, I won't waste any time e…
Working settings for French ISP Orange "Prêt à Surfer" SIM cards for data connections only. Can't be found anywhere else !
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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…

832 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