Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Inject js code into a WebView from a file<br>
- * Call this method on onPageFinished.
- *
- * @param fileName <i>String</i> name of the file to read.
- * @param webView <i>WebView</i> where to inject javascript code.
- *
- */
- private void injectJSIntoWebView(String fileName, WebView webView) {
- StringBuilder contentJS = new StringBuilder();
- try {
- /* Input butter */
- InputStream in = getAssets().open(fileName);
- /* Read buffer */
- BufferedReader reader = new BufferedReader(new InputStreamReader(in));
- String line;
- /* While the read line of the reading buffer is not null */
- while ((line = reader.readLine()) != null) {
- /* Add to StringBuilder object */
- contentJS.append(line);
- /* Line break */
- contentJS.append("\n");
- }
- /* Inject javascript code into webView */
- webView.loadUrl("javascript:(" + contentJS.toString() + ")()");
- /* Close input and reading buffer */
- in.close();
- reader.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
Add Comment
Please, Sign In to add comment