11package me .steenman .injectview ;
22
33import android .util .Log ;
4- import android .view .View ;
5- import android .webkit .WebView ;
6- import android .webkit .WebViewClient ;
7- import android .webkit .ValueCallback ;
84
9- import org .apache .cordova .CallbackContext ;
105import org .apache .cordova .CordovaPlugin ;
116import org .apache .cordova .CordovaActivity ;
127
138import org .json .JSONArray ;
14- import org .json .JSONObject ;
159import org .json .JSONException ;
1610
1711import java .io .IOException ;
1812import java .io .InputStream ;
19- import java .lang .reflect . Method ;
13+ import java .lang .StringBuilder ;
2014import java .util .ArrayList ;
2115
2216public class InjectView extends CordovaPlugin {
@@ -58,55 +52,35 @@ private ArrayList<String> getCordovaFiles() {
5852 return scripts ;
5953 }
6054
61- private void injectJavascriptFiles (ArrayList <String > files ) {
62- InjectView injectView = this ;
63-
64- this .cordova .getThreadPool ().execute (new Runnable () {
65- @ Override
66- public void run () {
67- for (String file : files ) {
68- Log .v (TAG , String .format ("Injecting script '%s'" , file ));
69-
70- String content = getFileContents (file );
71- if (content == null ) {
72- Log .v (TAG , String .format ("Could not load script '%s'" , file ));
73- continue ;
74- }
75- if (content .isEmpty ()) {
76- Log .v (TAG , String .format ("No contents found in script '%s'" , file ));
77- return ;
78- }
79- final String script = "//# sourceURL=" + file + "\r \n " + content + "\r \n console.log('Loaded script "
80- + file + "');" ;
81- injectView .activity .runOnUiThread (new Runnable () {
82- @ Override
83- public void run () {
84- View webView = injectView .webView .getEngine ().getView ();
85- ValueCallback <String > resultCallback = null ;
86- try {
87- Method evaluateJavascriptMethod = webView .getClass ().getMethod ("evaluateJavascript" ,
88- new Class [] { String .class ,
89- (Class <ValueCallback <String >>) (Class <?>) ValueCallback .class });
90- evaluateJavascriptMethod .invoke (webView , script , resultCallback );
91- } catch (Exception e ) {
92- Log .v (TAG , String .format (
93- "WARNING: Webview does not support 'evaluateJavascript' method. Webview type: '%s'" ,
94- webView .getClass ().getName ()));
95- injectView .webView .getEngine ().loadUrl ("javascript:" + script , false );
96-
97- if (resultCallback != null ) {
98- resultCallback .onReceiveValue (null );
99- }
100- }
101- }
102- });
55+ private void injectJavascriptFiles (ArrayList <String > filenames ) {
56+ this .cordova .getThreadPool ().execute (() -> {
57+ // Concatenate all scripts into one large script.
58+ StringBuilder script = new StringBuilder ();
59+ for (String filename : filenames ) {
60+ String content = getFileContents (filename );
61+ if (content == null || content .isEmpty ()) {
62+ continue ;
10363 }
64+
65+ script .append (content );
66+ script .append ("\n ;\n " );
10467 }
68+
69+ // Inject scripts by direct evaluation in the WebView.
70+ String expression = script .toString ();
71+ this .activity .runOnUiThread (() -> {
72+ try {
73+ this .webView .getEngine ().evaluateJavascript (expression , null );
74+ } catch (Exception e ) {
75+ Log .e (TAG , "Failed to inject scripts." );
76+ e .printStackTrace ();
77+ }
78+ });
10579 });
10680 }
10781
10882 private String getFileContents (String filename ) {
109- try (InputStream stream = activity .getResources ().getAssets ().open (filename )) {
83+ try (InputStream stream = this . activity .getResources ().getAssets ().open (filename )) {
11084 int size = stream .available ();
11185 byte [] bytes = new byte [size ];
11286 stream .read (bytes );
@@ -115,6 +89,6 @@ private String getFileContents(String filename) {
11589 Log .e (TAG , String .format ("Failed to read file: %s." , filename ));
11690 }
11791
118- return "" ;
92+ return null ;
11993 }
12094}
0 commit comments