Inject Javascript when changing URLs in a WebView Android Studio


I would like to know if anyone could help me. My problem is that I am opening a web page within a web view, now this page has a navigation menu

<aside id="left-panel">
      <li><a href="distribuir_cuentas" title="Distribuir Cuentas"><i class="fa fa-lg fa-fw fa-play"></i><span class="menu-item-parent">Distribuir Cuentas</span></a></li>
      <li><a href="movimientos" title="Movimientos"><i class="fa fa-lg fa-fw fa-credit-card"></i><span class="menu-item-parent">Movimientos</span></a></li>
      <li><a id="reportar_cuenta" href="javascript:void(0)" title="Reportar Cuenta"><i class="fa fa-lg fa-fw fa-slack"></i><span class="menu-item-parent">Reportar fallo cuenta</span></a></li>

In the Page or URL of distributing accounts, there is a table that can not be displayed well a table because it lacks an overflow-x style, this is the code that I have tried for it:

document.getElementById('id_tabla').style.overflowX = 'auto'; 
document.getElementById('id_tabla').style.whiteSpace = 'nowrap';

I would like to know if I can add this Javascript and how to click on the "Distribute Accounts" menu. I clarify, this page is not mine, that's why I want to modify it directly from Javascript when redirecting to the other interface.

I appreciate your help.

asked by David Casadiegos 08.11.2018 в 17:11

1 answer


You can override the onPageFinished () method from WebViewClient  to get the new url:

webView.setWebViewClient(new WebViewClient(){
    public void onPageFinished(WebView view, String url) {
        // TODO Auto-generated method stub
        super.onPageFinished(view, url);
        // Injecta aquí!

Then you can inject javascript code like this:

webView.loadUrl("javascript:(function() {"
+ codigoJS + // codigoJS es el código javascript como string

I hope it works for you. Greetings!


answered by 08.11.2018 в 18:11