Skip to content

Commit

Permalink
v0.2.1, fixed InAppBrowser.injectScriptCode() method when there is no…
Browse files Browse the repository at this point in the history
…t a return value, added InAppBrowser.onConsoleMessage() method to manage console messages #5
  • Loading branch information
pichillilorenzo committed Oct 9, 2018
1 parent db65f7a commit f032b7f
Show file tree
Hide file tree
Showing 24 changed files with 692 additions and 551 deletions.
2 changes: 1 addition & 1 deletion .idea/libraries/Flutter_Plugins.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

661 changes: 232 additions & 429 deletions .idea/workspace.xml

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.2.1

- added InAppBrowser.onConsoleMessage() method to manage console messages
- fixed InAppBrowser.injectScriptCode() method when there is not a return value

## 0.2.0

- added support of Chrome CustomTabs for Android
Expand Down
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ class MyInAppBrowser extends InAppBrowser {
print("\n\nStopped $url\n\n");
// print body html
print(await this.injectScriptCode("document.body.innerHTML"));
// console messages
await this.injectScriptCode("console.log({'testObject': 5});"); // the message will be: [object Object]
await this.injectScriptCode("console.log('testObjectStringify', JSON.stringify({'testObject': 5}));"); // the message will be: testObjectStringify {"testObject": 5}
await this.injectScriptCode("console.error('testError', false);"); // the message will be: testError false
// add jquery library and custom javascript
await this.injectScriptFile("https://code.jquery.com/jquery-3.3.1.min.js");
Expand Down Expand Up @@ -73,6 +78,17 @@ class MyInAppBrowser extends InAppBrowser {
this.loadUrl(url);
}
@override
void onConsoleMessage(ConsoleMessage consoleMessage) {
print("""
console output:
sourceURL: ${consoleMessage.sourceURL}
lineNumber: ${consoleMessage.lineNumber}
message: ${consoleMessage.message}
messageLevel: ${consoleMessage.messageLevel}
""");
}
}
MyInAppBrowser inAppBrowser = new MyInAppBrowser();
Expand Down Expand Up @@ -228,6 +244,14 @@ Event fires when the `InAppBrowser` window is closed.
}
```

Event fires when the `InAppBrowser` webview receives a `ConsoleMessage`.
```dart
@override
void onConsoleMessage(ConsoleMessage consoleMessage) {
}
```

Give the host application a chance to take control when a URL is about to be loaded in the current WebView.
In order to be able to listen this event, you need to set `useShouldOverrideUrlLoading` option to `true`.
```dart
Expand Down
1 change: 1 addition & 0 deletions android/.idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added android/.idea/caches/build_file_checksums.ser
Binary file not shown.
29 changes: 29 additions & 0 deletions android/.idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions android/.idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions android/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions android/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions android/.idea/runConfigurations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added android/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
5 changes: 5 additions & 0 deletions android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
Expand Up @@ -285,15 +285,20 @@ public void onReceiveValue(String s) {

try {
String msg;
msg = reader.nextString();
if (reader.peek() == JsonToken.STRING) {
msg = reader.nextString();

JsonReader reader2 = new JsonReader(new StringReader(msg));
reader2.setLenient(true);
JsonReader reader2 = new JsonReader(new StringReader(msg));
reader2.setLenient(true);

if (reader2.peek() == JsonToken.STRING)
msg = reader2.nextString();
if (reader2.peek() == JsonToken.STRING)
msg = reader2.nextString();

result.success(msg);
result.success(msg);
}
else {
result.success("");
}

} catch (IOException e) {
Log.e(LOG_TAG, "IOException", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.util.Log;
import android.view.View;
import android.webkit.ConsoleMessage;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;

import java.util.HashMap;
import java.util.Map;

public class InAppBrowserWebChromeClient extends WebChromeClient {

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

@Override
public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Map<String, Object> obj = new HashMap<>();
obj.put("uuid", activity.uuid);
obj.put("sourceURL", consoleMessage.sourceId());
obj.put("lineNumber", consoleMessage.lineNumber());
obj.put("message", consoleMessage.message());
obj.put("messageLevel", consoleMessage.messageLevel().toString());
InAppBrowserFlutterPlugin.channel.invokeMethod("onConsoleMessage", obj);
return true;
}

@Override
public void onProgressChanged(WebView view, int progress) {
if (activity.progressBar != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.graphics.Bitmap;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Build;
import android.util.Log;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
Expand Down Expand Up @@ -124,7 +125,7 @@ public void onPageFinished(WebView view, String url) {
activity.isLoading = false;

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

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
view.evaluateJavascript(activity.jsConsoleLogScript, null);
}

Map<String, Object> obj = new HashMap<>();
obj.put("uuid", activity.uuid);
obj.put("url", url);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,32 @@ public class WebViewActivity extends AppCompatActivity {
public boolean isLoading = false;
public boolean isHidden = false;

static final String jsConsoleLogScript = "(function() {\n"+
" var oldLogs = {\n"+
" 'log': console.log,\n"+
" 'debug': console.debug,\n"+
" 'error': console.error,\n"+
" 'info': console.info,\n"+
" 'warn': console.warn\n"+
" };\n"+
" for (var k in oldLogs) {\n"+
" (function(oldLog) {\n"+
" console[oldLog] = function() {\n"+
" var message = ''\n"+
" for (var i in arguments) {\n"+
" if (message == '') {\n"+
" message += arguments[i];\n"+
" }\n"+
" else {\n"+
" message += ' ' + arguments[i];\n"+
" }\n"+
" }\n"+
" oldLogs[oldLog].call(console, message);\n"+
" }\n"+
" })(k);\n"+
" }\n"+
"})();";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down
2 changes: 1 addition & 1 deletion example/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:3.1.4'
classpath 'com.android.tools.build:gradle:3.2.0'
}
}

Expand Down
4 changes: 2 additions & 2 deletions example/android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Fri Jun 23 08:50:38 CEST 2017
#Fri Oct 05 14:08:48 CEST 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
41 changes: 32 additions & 9 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,19 @@ class MyInAppBrowser extends InAppBrowser {
@override
Future onLoadStop(String url) async {
print("\n\nStopped $url\n\n");

// // javascript error
// await this.injectScriptCode("console.log({'testJavaScriptError': 5}));");
//
// await this.injectScriptCode("console.log({'testObject': 5});");
// await this.injectScriptCode("console.warn('testWarn',null);");
// await this.injectScriptCode("console.log('testObjectStringify', JSON.stringify({'asd': 5}));");
// await this.injectScriptCode("console.info('testInfo', 6);");
// await this.injectScriptCode("console.error('testError', false);");
// await this.injectScriptCode("console.debug('testDebug', true);");
// print(await this.injectScriptCode("document.body.innerHTML"));
// print(await this.injectScriptCode("null"));
// print(await this.injectScriptCode("undefined"));
// print(await this.injectScriptCode("3"));
// print(await this.injectScriptCode("""
// function asd (a,b) {
Expand Down Expand Up @@ -58,6 +70,17 @@ class MyInAppBrowser extends InAppBrowser {
print("\n\n override $url\n\n");
this.loadUrl(url);
}

@override
void onConsoleMessage(ConsoleMessage consoleMessage) {
print("""
console output:
sourceURL: ${consoleMessage.sourceURL}
lineNumber: ${consoleMessage.lineNumber}
message: ${consoleMessage.message}
messageLevel: ${consoleMessage.messageLevel}
""");
}
}

MyInAppBrowser inAppBrowserFallback = new MyInAppBrowser();
Expand Down Expand Up @@ -107,15 +130,15 @@ class _MyAppState extends State<MyApp> {
),
body: new Center(
child: new RaisedButton(onPressed: () {
chromeSafariBrowser.open("https://flutter.io/");
// inAppBrowserFallback.open("https://flutter.io/", options: {
// //"hidden": true,
// //"toolbarTopFixedTitle": "Fixed title",
// //"useShouldOverrideUrlLoading": true
// //"hideUrlBar": true,
// //"toolbarTop": false,
// //"toolbarBottom": false
// });
//chromeSafariBrowser.open("https://flutter.io/");
inAppBrowserFallback.open("https://flutter.io/", options: {
//"hidden": true,
//"toolbarTopFixedTitle": "Fixed title",
//"useShouldOverrideUrlLoading": true
//"hideUrlBar": true,
//"toolbarTop": false,
//"toolbarBottom": false
});

},
child: Text("Open InAppBrowser")
Expand Down
Loading

0 comments on commit f032b7f

Please # to comment.