Skip to content

Commit f032b7f

Browse files
v0.2.1, fixed InAppBrowser.injectScriptCode() method when there is not a return value, added InAppBrowser.onConsoleMessage() method to manage console messages #5
1 parent db65f7a commit f032b7f

24 files changed

+692
-551
lines changed

.idea/libraries/Flutter_Plugins.xml

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

+232-429
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.2.1
2+
3+
- added InAppBrowser.onConsoleMessage() method to manage console messages
4+
- fixed InAppBrowser.injectScriptCode() method when there is not a return value
5+
16
## 0.2.0
27

38
- added support of Chrome CustomTabs for Android

README.md

+24
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ class MyInAppBrowser extends InAppBrowser {
4141
print("\n\nStopped $url\n\n");
4242
// print body html
4343
print(await this.injectScriptCode("document.body.innerHTML"));
44+
45+
// console messages
46+
await this.injectScriptCode("console.log({'testObject': 5});"); // the message will be: [object Object]
47+
await this.injectScriptCode("console.log('testObjectStringify', JSON.stringify({'testObject': 5}));"); // the message will be: testObjectStringify {"testObject": 5}
48+
await this.injectScriptCode("console.error('testError', false);"); // the message will be: testError false
4449
4550
// add jquery library and custom javascript
4651
await this.injectScriptFile("https://code.jquery.com/jquery-3.3.1.min.js");
@@ -73,6 +78,17 @@ class MyInAppBrowser extends InAppBrowser {
7378
this.loadUrl(url);
7479
}
7580
81+
@override
82+
void onConsoleMessage(ConsoleMessage consoleMessage) {
83+
print("""
84+
console output:
85+
sourceURL: ${consoleMessage.sourceURL}
86+
lineNumber: ${consoleMessage.lineNumber}
87+
message: ${consoleMessage.message}
88+
messageLevel: ${consoleMessage.messageLevel}
89+
""");
90+
}
91+
7692
}
7793
7894
MyInAppBrowser inAppBrowser = new MyInAppBrowser();
@@ -228,6 +244,14 @@ Event fires when the `InAppBrowser` window is closed.
228244
}
229245
```
230246

247+
Event fires when the `InAppBrowser` webview receives a `ConsoleMessage`.
248+
```dart
249+
@override
250+
void onConsoleMessage(ConsoleMessage consoleMessage) {
251+
252+
}
253+
```
254+
231255
Give the host application a chance to take control when a URL is about to be loaded in the current WebView.
232256
In order to be able to listen this event, you need to set `useShouldOverrideUrlLoading` option to `true`.
233257
```dart

android/.idea/.name

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
492 Bytes
Binary file not shown.

android/.idea/codeStyles/Project.xml

+29
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

android/.idea/gradle.xml

+17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

android/.idea/misc.xml

+38
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

android/.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

android/.idea/runConfigurations.xml

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
53.1 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
4+
zipStoreBase=GRADLE_USER_HOME
5+
zipStorePath=wrapper/dists

android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppBrowserFlutterPlugin.java

+11-6
Original file line numberDiff line numberDiff line change
@@ -285,15 +285,20 @@ public void onReceiveValue(String s) {
285285

286286
try {
287287
String msg;
288-
msg = reader.nextString();
288+
if (reader.peek() == JsonToken.STRING) {
289+
msg = reader.nextString();
289290

290-
JsonReader reader2 = new JsonReader(new StringReader(msg));
291-
reader2.setLenient(true);
291+
JsonReader reader2 = new JsonReader(new StringReader(msg));
292+
reader2.setLenient(true);
292293

293-
if (reader2.peek() == JsonToken.STRING)
294-
msg = reader2.nextString();
294+
if (reader2.peek() == JsonToken.STRING)
295+
msg = reader2.nextString();
295296

296-
result.success(msg);
297+
result.success(msg);
298+
}
299+
else {
300+
result.success("");
301+
}
297302

298303
} catch (IOException e) {
299304
Log.e(LOG_TAG, "IOException", e);

android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppBrowserWebChromeClient.java

+17
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@
44
import android.graphics.Bitmap;
55
import android.net.Uri;
66
import android.os.Build;
7+
import android.util.Log;
78
import android.view.View;
9+
import android.webkit.ConsoleMessage;
810
import android.webkit.ValueCallback;
911
import android.webkit.WebChromeClient;
1012
import android.webkit.WebView;
1113

14+
import java.util.HashMap;
15+
import java.util.Map;
16+
1217
public class InAppBrowserWebChromeClient extends WebChromeClient {
1318

1419
protected static final String LOG_TAG = "IABWebChromeClient";
@@ -22,6 +27,18 @@ public InAppBrowserWebChromeClient(WebViewActivity activity) {
2227
this.activity = activity;
2328
}
2429

30+
@Override
31+
public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
32+
Map<String, Object> obj = new HashMap<>();
33+
obj.put("uuid", activity.uuid);
34+
obj.put("sourceURL", consoleMessage.sourceId());
35+
obj.put("lineNumber", consoleMessage.lineNumber());
36+
obj.put("message", consoleMessage.message());
37+
obj.put("messageLevel", consoleMessage.messageLevel().toString());
38+
InAppBrowserFlutterPlugin.channel.invokeMethod("onConsoleMessage", obj);
39+
return true;
40+
}
41+
2542
@Override
2643
public void onProgressChanged(WebView view, int progress) {
2744
if (activity.progressBar != null) {

android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppBrowserWebViewClient.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.graphics.Bitmap;
55
import android.net.Uri;
66
import android.net.http.SslError;
7+
import android.os.Build;
78
import android.util.Log;
89
import android.webkit.CookieManager;
910
import android.webkit.CookieSyncManager;
@@ -124,7 +125,7 @@ public void onPageFinished(WebView view, String url) {
124125
activity.isLoading = false;
125126

126127
// CB-10395 InAppBrowserFlutterPlugin's WebView not storing cookies reliable to local device storage
127-
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
128+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
128129
CookieManager.getInstance().flush();
129130
} else {
130131
CookieSyncManager.getInstance().sync();
@@ -134,6 +135,10 @@ public void onPageFinished(WebView view, String url) {
134135
view.clearFocus();
135136
view.requestFocus();
136137

138+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
139+
view.evaluateJavascript(activity.jsConsoleLogScript, null);
140+
}
141+
137142
Map<String, Object> obj = new HashMap<>();
138143
obj.put("uuid", activity.uuid);
139144
obj.put("url", url);

android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/WebViewActivity.java

+26
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,32 @@ public class WebViewActivity extends AppCompatActivity {
3838
public boolean isLoading = false;
3939
public boolean isHidden = false;
4040

41+
static final String jsConsoleLogScript = "(function() {\n"+
42+
" var oldLogs = {\n"+
43+
" 'log': console.log,\n"+
44+
" 'debug': console.debug,\n"+
45+
" 'error': console.error,\n"+
46+
" 'info': console.info,\n"+
47+
" 'warn': console.warn\n"+
48+
" };\n"+
49+
" for (var k in oldLogs) {\n"+
50+
" (function(oldLog) {\n"+
51+
" console[oldLog] = function() {\n"+
52+
" var message = ''\n"+
53+
" for (var i in arguments) {\n"+
54+
" if (message == '') {\n"+
55+
" message += arguments[i];\n"+
56+
" }\n"+
57+
" else {\n"+
58+
" message += ' ' + arguments[i];\n"+
59+
" }\n"+
60+
" }\n"+
61+
" oldLogs[oldLog].call(console, message);\n"+
62+
" }\n"+
63+
" })(k);\n"+
64+
" }\n"+
65+
"})();";
66+
4167
@Override
4268
protected void onCreate(Bundle savedInstanceState) {
4369
super.onCreate(savedInstanceState);

example/android/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ buildscript {
55
}
66

77
dependencies {
8-
classpath 'com.android.tools.build:gradle:3.1.4'
8+
classpath 'com.android.tools.build:gradle:3.2.0'
99
}
1010
}
1111

Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Fri Jun 23 08:50:38 CEST 2017
1+
#Fri Oct 05 14:08:48 CEST 2018
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip

example/lib/main.dart

+32-9
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,19 @@ class MyInAppBrowser extends InAppBrowser {
1212
@override
1313
Future onLoadStop(String url) async {
1414
print("\n\nStopped $url\n\n");
15+
16+
// // javascript error
17+
// await this.injectScriptCode("console.log({'testJavaScriptError': 5}));");
18+
//
19+
// await this.injectScriptCode("console.log({'testObject': 5});");
20+
// await this.injectScriptCode("console.warn('testWarn',null);");
21+
// await this.injectScriptCode("console.log('testObjectStringify', JSON.stringify({'asd': 5}));");
22+
// await this.injectScriptCode("console.info('testInfo', 6);");
23+
// await this.injectScriptCode("console.error('testError', false);");
24+
// await this.injectScriptCode("console.debug('testDebug', true);");
1525
// print(await this.injectScriptCode("document.body.innerHTML"));
26+
// print(await this.injectScriptCode("null"));
27+
// print(await this.injectScriptCode("undefined"));
1628
// print(await this.injectScriptCode("3"));
1729
// print(await this.injectScriptCode("""
1830
// function asd (a,b) {
@@ -58,6 +70,17 @@ class MyInAppBrowser extends InAppBrowser {
5870
print("\n\n override $url\n\n");
5971
this.loadUrl(url);
6072
}
73+
74+
@override
75+
void onConsoleMessage(ConsoleMessage consoleMessage) {
76+
print("""
77+
console output:
78+
sourceURL: ${consoleMessage.sourceURL}
79+
lineNumber: ${consoleMessage.lineNumber}
80+
message: ${consoleMessage.message}
81+
messageLevel: ${consoleMessage.messageLevel}
82+
""");
83+
}
6184
}
6285

6386
MyInAppBrowser inAppBrowserFallback = new MyInAppBrowser();
@@ -107,15 +130,15 @@ class _MyAppState extends State<MyApp> {
107130
),
108131
body: new Center(
109132
child: new RaisedButton(onPressed: () {
110-
chromeSafariBrowser.open("https://flutter.io/");
111-
// inAppBrowserFallback.open("https://flutter.io/", options: {
112-
// //"hidden": true,
113-
// //"toolbarTopFixedTitle": "Fixed title",
114-
// //"useShouldOverrideUrlLoading": true
115-
// //"hideUrlBar": true,
116-
// //"toolbarTop": false,
117-
// //"toolbarBottom": false
118-
// });
133+
//chromeSafariBrowser.open("https://flutter.io/");
134+
inAppBrowserFallback.open("https://flutter.io/", options: {
135+
//"hidden": true,
136+
//"toolbarTopFixedTitle": "Fixed title",
137+
//"useShouldOverrideUrlLoading": true
138+
//"hideUrlBar": true,
139+
//"toolbarTop": false,
140+
//"toolbarBottom": false
141+
});
119142

120143
},
121144
child: Text("Open InAppBrowser")

0 commit comments

Comments
 (0)