Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Execute method in JavascriptInterface after page has finished loading

Posted on 2012-04-02
1
Medium Priority
?
714 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

A short article about problems I had with the new location API and permissions in Marshmallow
The case of the missing phone talks about the way a small electronic gadget (the mobile phone) has penetrated into our lives and has made us addicted to it.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

715 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