Guest User

Untitled

a guest
Jul 16th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. /**
  2. * Inject js code into a WebView from a file<br>
  3. * Call this method on onPageFinished.
  4. *
  5. * @param fileName <i>String</i> name of the file to read.
  6. * @param webView <i>WebView</i> where to inject javascript code.
  7. *
  8. */
  9. private void injectJSIntoWebView(String fileName, WebView webView) {
  10.  
  11. StringBuilder contentJS = new StringBuilder();
  12.  
  13. try {
  14. /* Input butter */
  15. InputStream in = getAssets().open(fileName);
  16. /* Read buffer */
  17. BufferedReader reader = new BufferedReader(new InputStreamReader(in));
  18. String line;
  19.  
  20. /* While the read line of the reading buffer is not null */
  21. while ((line = reader.readLine()) != null) {
  22. /* Add to StringBuilder object */
  23. contentJS.append(line);
  24. /* Line break */
  25. contentJS.append("\n");
  26. }
  27.  
  28. /* Inject javascript code into webView */
  29. webView.loadUrl("javascript:(" + contentJS.toString() + ")()");
  30.  
  31. /* Close input and reading buffer */
  32. in.close();
  33. reader.close();
  34.  
  35. } catch (IOException e) {
  36. e.printStackTrace();
  37. }
  38. }
Add Comment
Please, Sign In to add comment