title: Inject javascript file to my site with webview in android
body: <p>I want to inject <code>javascript</code> file to my site. My site is a simple <code>html</code> page that is on server. I have injected <code>css</code> file. (<a href="https://stackoverflow.com/questions/30018540/inject-css-to-a-site-with-webview-in-android">with Manish's help</a>) </p>
  
  <p>So I can manage my simple  <code>html</code> site with <code>CSS</code> now. But I want to manage it with <code>javascript</code> too. My <code>jscript.js</code> file is in <code>asset</code> folder. I want to have full access of <code>javascript</code> on my site. (Remember that, it is MY site) . please write the correct codes for me. Thankx.</p>
  
  <p>Here is my <code>MainActivity.java</code> file:</p>
  
  <pre><code>package com.example.z5070.myapplication;
  
  import android.support.v7.app.ActionBarActivity;
  import android.os.Bundle;
  import android.util.Base64;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.webkit.WebSettings;
  import android.webkit.WebView;
  import android.webkit.WebViewClient;
  import java.io.InputStream;
  
  
  public class MainActivity extends ActionBarActivity {
  
  
  
          WebView webView;
  
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              webView = new WebView(this);
              setContentView(webView);
  
  
              webView.getSettings().setJavaScriptEnabled(true);
  
  
              webView.setWebViewClient(new WebViewClient() {
  
                  @Override
                  public void onPageFinished(WebView view, String url) {
  
  
                      injectCSS();
                      super.onPageFinished(view, url);
                  }
              });
  
  
              webView.loadUrl("http://www.example.com/");
          }
  
  
          private void injectCSS() {
              try {
                  InputStream inputStream = getAssets().open("style.css");
                  byte[] buffer = new byte[inputStream.available()];
                  inputStream.read(buffer);
                  inputStream.close();
                  String encoded = Base64.encodeToString(buffer, Base64.NO_WRAP);
                  webView.loadUrl("javascript:(function() {" +
                          "var parent = document.getElementsByTagName('head').item(0);" +
                          "var style = document.createElement('style');" +
                          "style.type = 'text/css';" +
                          "style.innerHTML = window.atob('" + encoded + "');" +
                          "parent.appendChild(style)" +
                          "})()");
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
  
          @Override
          public boolean onCreateOptionsMenu(Menu menu) {
  
              getMenuInflater().inflate(R.menu.menu_main, menu);
              return true;
          }
  
          @Override
          public boolean onOptionsItemSelected(MenuItem item) {
              int id = item.getItemId();
              if (id == R.id.action_settings) {
                  return true;
              }
              return super.onOptionsItemSelected(item);
          }
  
  
          }
  </code></pre>