inject js file to webview

12,153

Solution 1

You can use js method to load js file and pass url as a parameter from java

Here is the full working example

[Activity.java]

public class MainActivity extends Activity {
WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mWebView = (WebView) findViewById(R.id.webview);
    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    mWebView.addJavascriptInterface(new JavaScriptInterface(), "jsinterface");
    mWebView.loadUrl("file:///android_asset/sq.html");

}

final class JavaScriptInterface {
    JavaScriptInterface() {
    }

    @JavascriptInterface 
    public void windowLoaded() {
        mWebView.loadUrl("javascript:loadScript('test.js', 'testing')");
        Log.i("browser", "browser loaded");
    }
  }
}

[sq.html]

<html>
<head>
<script>
onload = function () {
    window.jsinterface.windowLoaded();
}


//JS
function loadScript(url, callback)
{
    console.log("loading script " + url);
    // adding the script tag to the head as suggested before
    var head = document.getElementsByTagName('head')[0];
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = url;

    // then bind the event to the callback function 
    // there are several events for cross browser compatibility
    //script.onreadystatechange = callback;
    script.onload = callback;

    // fire the loading
    head.appendChild(script);
}

</script>
</head>
<body>replace this
</body>
</html>

[test.js] inside assets folder

document.body.innerHTML = "<p>testing</p>";

on successful execution webview will show testing by loading test.js dynamically.

Solution 2

Here is how i ended up doing it. I used the Content:// protocol and set up a contentprovider to handle returning a file descriptor to the system

Here is my fileContentProvider:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;


import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import android.util.Log;

public class FileContentProvider extends ContentProvider {
    @Override
    public ParcelFileDescriptor openFile(Uri uri, String mode) {

        Log.d("FileContentProvider","fetching: " + uri);

        ParcelFileDescriptor parcel = null;

        String fileNameRequested = uri.getLastPathSegment();
        String[] name=fileNameRequested.split("\\.");
        String prefix=name[0];
        String suffix=name[1];
       // String path = getContext().getFilesDir().getAbsolutePath() + "/" + uri.getPath();
        //String path=file:///android_asset/"+Consts.FILE_JAVASCRIPT+"

/*check if this is a javascript file*/

        if(suffix.equalsIgnoreCase("js")){
        InputStream is = null;
        try {
            is = getContext().getAssets().open("www/"+Consts.FILE_JAVASCRIPT);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }


        File file = stream2file(is,prefix,suffix);
        try {
            parcel = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
        } catch (FileNotFoundException e) {
            Log.e("FileContentProvider", "uri " + uri.toString(), e);
        }
        }
        return parcel;
    }

    /*converts an inputstream to a temp file*/

    public File stream2file (InputStream in,String prefix,String suffix) {
        File tempFile = null;
        try {
            tempFile = File.createTempFile(prefix, suffix);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        tempFile.deleteOnExit();

            FileOutputStream out = null;
            try {
                out = new FileOutputStream(tempFile);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 

            try {
                IOUtils.copy(in, out);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        return tempFile;
    }


    @Override
    public boolean onCreate() {
        return true;
    }

    @Override
    public int delete(Uri uri, String s, String[] as) {
        throw new UnsupportedOperationException("Not supported by this provider");
    }

    @Override
    public String getType(Uri uri) {
        throw new UnsupportedOperationException("Not supported by this provider");
    }

    @Override
    public Uri insert(Uri uri, ContentValues contentvalues) {
        throw new UnsupportedOperationException("Not supported by this provider");
    }

    @Override
    public Cursor query(Uri uri, String[] as, String s, String[] as1, String s1) {
        throw new UnsupportedOperationException("Not supported by this provider");
    }

    @Override
    public int update(Uri uri, ContentValues contentvalues, String s, String[] as) {
        throw new UnsupportedOperationException("Not supported by this provider");
    }
}

in the manifest i defined the provider:

<provider android:name="com.example.mypackage.FileContentProvider"
          android:authorities="com.example.fileprovider"
        />

Here is the javascript o inject into the webview:

webView.loadUrl("javascript:(function() { "

           + "var script=document.createElement('script'); "
           + " script.setAttribute('type','text/javascript'); "
           + " script.setAttribute('src', 'content://com.example.fileprovider/myjavascriptfile.js'); "
      /*      + " script.onload = function(){ "
           + "     test(); "
           + " }; "
      */     + "document.body.appendChild(script); "
           + "})();");

and here is the myjavascriptfile.js (as an example):

   function changeBackground(color) {
   document.body.style.backgroundColor = color;

}

Share:
12,153
waldemar
Author by

waldemar

Updated on June 17, 2022

Comments

  • waldemar
    waldemar about 2 years

    I need to inject some javaScript files into loading page.

    my code:

     @Override
                public void doUpdateVisitedHistory(WebView view, String url, boolean isReload) {
                    super.doUpdateVisitedHistory(view, url, isReload);    //To change body of overridden methods use File | Settings | File Templates
    
                    mWebView.loadUrl("javascript:(function(){var script = document.createElement('script');script.setAttribute('src', 'file:///android_asset/jquery.js'); script.setAttribute('type', 'text/javascript'); document.body.appendChild(script)})();");
                    mWebView.loadUrl("javascript:(function(){var script = document.createElement('script');script.setAttribute('src', 'file:///android_asset/rangy-core.js'); script.setAttribute('type', 'text/javascript'); document.body.appendChild(script)})();");
                    mWebView.loadUrl("javascript:(function(){var script = document.createElement('script');script.setAttribute('src', 'file:///android_asset/rangy-serializer.js'); script.setAttribute('type', 'text/javascript'); document.body.appendChild(script)})();");
                    mWebView.loadUrl("javascript:(function(){var script = document.createElement('script');script.setAttribute('src', 'file:///android_asset/android.selection.js'); script.setAttribute('type', 'text/javascript'); document.body.appendChild(script)})();");// .
                }
    

    but it's not work =(

    Change code to

    mWebView.loadUrl("javascript:function loadScript(url, callback)" +
                        "{" +
                        "    var head = document.getElementsByTagName('head')[0];" +
                        "    var script = document.createElement('script');" +
                        "    script.type = 'text/javascript';" +
                        "    script.src = url;" +
                        "    script.onreadystatechange = callback;" +
                        "    script.onload = callback;" +
                        "    head.appendChild(script);" +
                        "}");
                mWebView.loadUrl("javascript:loadScript('file:///android_asset/jquery.js','callback')");
                mWebView.loadUrl("javascript:loadScript('file:///android_asset/rangy-core.js','callback')");
                mWebView.loadUrl("javascript:loadScript('file:///android_asset/rangy-serializer.js','callback')");
                mWebView.loadUrl("javascript:loadScript('file:///android_asset/android.selection.js','callback')");
    

    Now I have Error : 06-11 10:24:15.432: ERROR/Web Console(7962): Not allowed to load local resource: file:///android_asset/jquery.js at null:0

  • waldemar
    waldemar about 11 years
    ERROR/Web Console(7841): Not allowed to load local resource: file:///android_asset/jquery.js at null:0
  • Sandeep Manne
    Sandeep Manne about 11 years
    try just mWebView.loadUrl("javascript:loadScript('jquery.js','callbac‌​k'));
  • waldemar
    waldemar about 11 years
    Its good idea but I have to load pages from internet. I tried to load local file and change body by preloaded html from internet page but I had some problem with this method - history is not updated, because pages not changed, only content; i had encoding problem with non english pages and i couldn't to fix it.
  • waldemar
    waldemar about 11 years
    about mWebView.loadUrl("javascript:loadScript('jquery.js','callbac‌​k')); I tried this but it not working for me too. Error not wrote but functions from js files not called
  • Jebik
    Jebik almost 11 years
    web.getSettings().setAllowUniversalAccessFromFileURLs(true) have you add this to your webview?
  • Prashant Borde
    Prashant Borde over 8 years
    this does not work for website that restrict domains from which script can be loaded as part of their Content Security Policy.