Solved

Replace function calls in external javascript file from WebView android

Posted on 2012-04-03
1
792 Views
Last Modified: 2012-06-22
An android app I'm working on relies on a web page for most of its functionality. When a user clicks a link that would normally call window.open I would like it to instead call a custom function. That way it stops the app from opening a browser window, which destroys the illusion of using an app. I already have a JavaScriptInterface set up to replace all the instances of window.open it finds in the page. The issue I'm having now is the external javascript file. It has a function very similar to this one

function LoadItem(url,title)
{
    window.open("/items.html?item=" + url + "&title=" + EncodeString(title), "song: " + title);
}

Open in new window


I would like to fix it up so it is this instead:

function LoadItem(url,title)
{
    window.LinkFixer.openInNewTab("/items.html?item=" + url + "&title=" + EncodeString(title), "song: " + title);
}

Open in new window


This is the code I have thus far that **should** be doing this for me.

public String processPage(String html)//called once the page has been loaded
{
    html = html.replace("window.open","window.LinkFixer.openInNewTab").replace("OpenSong","window.LinkFixer.openSong");
    String[] lines = html.split("\n");
    html = "";
    for(int i=0;i<lines.length;i++)
    {
        if(lines[i].contains("<script")&&lines[i].contains("src="))
        {
            lines[i] = getJs(lines[i]);
        }
        html += lines[i];
    }
    return html;
}

private String getJs(String line)//gets the javascript, fixes it so it's safe for the app, then returns it as inline rather than embeded
    {
        String jsFileName = line.replace("\"","'").replace("<script type='text/javascript' ","").replace("src=","").replace(">","").replace("'","").trim();
        //public static String makeGetRequest(String url,boolean oneLine)
        System.out.println(jsFileName);
        String targetUrl = "/BrowserControl/"+jsFileName;
        System.out.println(targetUrl);
        String jsFile = ShowWebInterface.makeGetRequest(targetUrl,false);
        Log.d("OrigFile"+jsFileName,jsFile);
        jsFile = jsFile.replace("window.open","window.LinkFixer.openInNewTab").replace("OpenSong","window.LinkFixer.openSong").replace("function window.LinkFixer.openSong(url, title)","window.LinkFixer.openSong");
        Log.d("NewFile"+jsFileName,jsFile);
        return "<script type=\"text/javascript\">\n"+jsFile;
    }

Open in new window


For the first click, it works. For anything after that, it loads the page in a browser window outside of the control of my app. The page I'm having issues with does use ajax, so I'm fairly sure that is causing some kind of issue. I would think that the browser would still call the onProgressChanged method, but I could be wrong.

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


Is there something I'm missing? Some way to hijack the callback function of the ajax? Or maybe something else that I've overlooked.
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: 37806853
I should have been using a normal WebViewClient for starters. Then I could have overridden the shouldOverrideUrlLoading method. Using that I was able to cut out all those large string replacements and everything works better than it did before.
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jmss example java 2 33
Why doesn't this text field show up on my Applet frame? 2 28
how to root nextbook 10 running marshmallow 1 42
Android Touch & Google API 7 36
In this article we will discuss all things related to StageFright bug, the most vulnerable bug of android devices.
A short article about a problem I had getting the GPS LocationListener working.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video is in connection to the article "The case of a missing mobile phone (https://www.experts-exchange.com/articles/28474/The-Case-of-a-Missing-Mobile-Phone.html)". It will help one to understand clearly the steps to track a lost android phone.

730 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