diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 1c0d46b0..26e70306 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -15,16 +15,26 @@ - - - - + + + + + + + + + - + + + + + + @@ -44,53 +54,11 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + @@ -99,26 +67,47 @@ - + - - + + - + - - + + - + + + + + + + + + + + + + + + + + + + + + + @@ -131,12 +120,6 @@ - AndroidInAppWebViewMixedContentMode - IosInAppWebViewSelectionGranularity - preferredContent - IosWebViewOptionsPresentationStyle - AndroidInAppWebViewCacheMode - Action IosSafariOptionsDismissButtonStyle method AjaxRequestEvent @@ -146,9 +129,7 @@ my-special-custom-scheme should resourceCustomSchemes - evaluateJavascript WebHistory - shouldInterceptAjaxRequest flutterInAppBrowserPlatformReady evaluateJ AjaxRequest @@ -161,6 +142,14 @@ ajaxReq window. shouldInterceptFetchRequest + onconsole + shouldInterceptAjaxRequest + console + getHT + getFavicons + getHtml + loadUrl + evaluateJavascript activity.getPreferences(0) @@ -186,22 +175,12 @@ @@ -270,6 +259,12 @@ + + + + + + @@ -488,7 +483,7 @@ - + @@ -499,15 +494,15 @@ - + - + - + @@ -538,22 +533,6 @@ - - - - - - - - - - - - - - - - @@ -610,11 +589,6 @@ - - - - - @@ -651,13 +625,6 @@ - - - - - - - @@ -686,13 +653,6 @@ - - - - - - - @@ -713,43 +673,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -757,6 +680,16 @@ + + + + + + + + + + @@ -764,17 +697,138 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + diff --git a/CHANGELOG.md b/CHANGELOG.md index 0acf0bd7..a470c6de 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,7 @@ ### BREAKING CHANGES - Deleted `WebResourceRequest` class - Updated `WebResourceResponse` class +- Updated `ConsoleMessage` class - Updated `ConsoleMessageLevel` class - Updated `onLoadResource` event - Updated `CookieManager` class diff --git a/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppWebView/InAppWebChromeClient.java b/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppWebView/InAppWebChromeClient.java index 04ccc4e3..d3071b04 100644 --- a/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppWebView/InAppWebChromeClient.java +++ b/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppWebView/InAppWebChromeClient.java @@ -439,8 +439,6 @@ public class InAppWebChromeClient extends WebChromeClient implements PluginRegis Map obj = new HashMap<>(); if (inAppBrowserActivity != null) obj.put("uuid", inAppBrowserActivity.uuid); - obj.put("sourceURL", consoleMessage.sourceId()); - obj.put("lineNumber", consoleMessage.lineNumber()); obj.put("message", consoleMessage.message()); obj.put("messageLevel", consoleMessage.messageLevel().ordinal()); getChannel().invokeMethod("onConsoleMessage", obj); diff --git a/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppWebView/InAppWebView.java b/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppWebView/InAppWebView.java index 3288328d..b9844d5a 100644 --- a/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppWebView/InAppWebView.java +++ b/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppWebView/InAppWebView.java @@ -1097,15 +1097,13 @@ final public class InAppWebView extends InputAwareWebView { } public void injectDeferredObject(String source, String jsWrapper, final MethodChannel.Result result) { - String scriptToInject; + String scriptToInject = source; if (jsWrapper != null) { org.json.JSONArray jsonEsc = new org.json.JSONArray(); jsonEsc.put(source); String jsonRepr = jsonEsc.toString(); String jsonSourceString = jsonRepr.substring(1, jsonRepr.length() - 1); scriptToInject = String.format(jsWrapper, jsonSourceString); - } else { - scriptToInject = source; } final String finalScriptToInject = scriptToInject; ( (inAppBrowserActivity != null) ? inAppBrowserActivity : flutterWebView.activity ).runOnUiThread(new Runnable() { @@ -1120,37 +1118,7 @@ final public class InAppWebView extends InputAwareWebView { public void onReceiveValue(String s) { if (result == null) return; - - JsonReader reader = new JsonReader(new StringReader(s)); - - // Must set lenient to parse single values - reader.setLenient(true); - - try { - String msg; - if (reader.peek() == JsonToken.STRING) { - msg = reader.nextString(); - - JsonReader reader2 = new JsonReader(new StringReader(msg)); - reader2.setLenient(true); - - if (reader2.peek() == JsonToken.STRING) - msg = reader2.nextString(); - - result.success(msg); - } else { - result.success(""); - } - - } catch (IOException e) { - Log.e(LOG_TAG, "IOException", e); - } finally { - try { - reader.close(); - } catch (IOException e) { - // NOOP - } - } + result.success(s); } }); } @@ -1159,8 +1127,7 @@ final public class InAppWebView extends InputAwareWebView { } public void evaluateJavascript(String source, MethodChannel.Result result) { - String jsWrapper = "(function(){return JSON.stringify(eval(%s));})();"; - injectDeferredObject(source, jsWrapper, result); + injectDeferredObject(source, null, result); } public void injectJavascriptFileFromUrl(String urlFile) { diff --git a/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/JavaScriptBridgeInterface.java b/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/JavaScriptBridgeInterface.java index 2eef9ab1..f59dba56 100644 --- a/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/JavaScriptBridgeInterface.java +++ b/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/JavaScriptBridgeInterface.java @@ -5,6 +5,7 @@ import android.os.Handler; import android.os.Looper; import android.util.Log; import android.webkit.JavascriptInterface; +import android.webkit.ValueCallback; import com.pichillilorenzo.flutter_inappbrowser.InAppWebView.InAppWebView; @@ -61,7 +62,7 @@ public class JavaScriptBridgeInterface { return; } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { - webView.evaluateJavascript("window." + name + "[" + _callHandlerID + "](" + json + "); delete window." + name + "[" + _callHandlerID + "];", (MethodChannel.Result) null); + webView.evaluateJavascript("window." + name + "[" + _callHandlerID + "](" + json + "); delete window." + name + "[" + _callHandlerID + "];", (ValueCallback) null); } else { webView.loadUrl("javascript:window." + name + "[" + _callHandlerID + "](" + json + "); delete window." + name + "[" + _callHandlerID + "];"); diff --git a/example/lib/inline_example.screen.dart b/example/lib/inline_example.screen.dart index 7a398e3a..9ade8a3e 100755 --- a/example/lib/inline_example.screen.dart +++ b/example/lib/inline_example.screen.dart @@ -93,7 +93,7 @@ class _InlineExampleScreenState extends State { initialOptions: InAppWebViewWidgetOptions( inAppWebViewOptions: InAppWebViewOptions( debuggingEnabled: true, - //clearCache: true, + clearCache: true, useShouldOverrideUrlLoading: true, useOnTargetBlank: true, useOnLoadResource: true, @@ -196,10 +196,8 @@ class _InlineExampleScreenState extends State { onConsoleMessage: (InAppWebViewController controller, ConsoleMessage consoleMessage) { print(""" console output: - sourceURL: ${consoleMessage.sourceURL} - lineNumber: ${consoleMessage.lineNumber} message: ${consoleMessage.message} - messageLevel: ${consoleMessage.messageLevel.toValue()} + messageLevel: ${consoleMessage.messageLevel.toString()} """); }, onDownloadStart: (InAppWebViewController controller, String url) async { diff --git a/example/lib/webview_example.screen.dart b/example/lib/webview_example.screen.dart index d3f61fa9..73912fda 100644 --- a/example/lib/webview_example.screen.dart +++ b/example/lib/webview_example.screen.dart @@ -60,8 +60,6 @@ class MyInAppBrowser extends InAppBrowser { void onConsoleMessage(ConsoleMessage consoleMessage) { print(""" console output: - sourceURL: ${consoleMessage.sourceURL} - lineNumber: ${consoleMessage.lineNumber} message: ${consoleMessage.message} messageLevel: ${consoleMessage.messageLevel.toValue()} """); diff --git a/example/test_assets/in_app_webview_on_console_message_test.html b/example/test_assets/in_app_webview_on_console_message_test.html new file mode 100644 index 00000000..48cd6af4 --- /dev/null +++ b/example/test_assets/in_app_webview_on_console_message_test.html @@ -0,0 +1,15 @@ + + + + + + + InAppWebViewOnConsoleMessageTest + + +

InAppWebViewOnConsoleMessageTest

+ + + \ No newline at end of file diff --git a/example/test_assets/in_app_webview_on_download_start_test.html b/example/test_assets/in_app_webview_on_download_start_test.html new file mode 100644 index 00000000..d772cb2f --- /dev/null +++ b/example/test_assets/in_app_webview_on_download_start_test.html @@ -0,0 +1,18 @@ + + + + + + + InAppWebViewOnDownloadStartTest + + +

InAppWebViewOnDownloadStartTest

+ download file + + + \ No newline at end of file diff --git a/example/test_assets/in_app_webview_on_target_blank_test.html b/example/test_assets/in_app_webview_on_target_blank_test.html new file mode 100644 index 00000000..ad5f4918 --- /dev/null +++ b/example/test_assets/in_app_webview_on_target_blank_test.html @@ -0,0 +1,18 @@ + + + + + + + InAppWebViewOnTargetBlankTest + + +

InAppWebViewOnTargetBlankTest

+ target blank + + + \ No newline at end of file diff --git a/example/test_driver/app_test.dart b/example/test_driver/app_test.dart index fa50fb3c..2ecd7d1b 100644 --- a/example/test_driver/app_test.dart +++ b/example/test_driver/app_test.dart @@ -118,5 +118,53 @@ void main() { expect(true, title.contains("Lorenzo Pichilli") && title.contains("200")); }, timeout: new Timeout(new Duration(minutes: 5))); + test('InAppWebViewShouldOverrideUrlLoadingTest', () async { + await Future.delayed(const Duration(milliseconds: 2000)); + final appBarTitle = find.byValueKey('AppBarTitle'); + + while((await driver.getText(appBarTitle)) == "InAppWebViewShouldOverrideUrlLoadingTest") { + await Future.delayed(const Duration(milliseconds: 1000)); + } + + String url = await driver.getText(appBarTitle); + expect(url, "https://flutter.dev/"); + }, timeout: new Timeout(new Duration(minutes: 5))); + + test('InAppWebViewOnConsoleMessageTest', () async { + await Future.delayed(const Duration(milliseconds: 2000)); + final appBarTitle = find.byValueKey('AppBarTitle'); + + while((await driver.getText(appBarTitle)) == "InAppWebViewOnConsoleMessageTest") { + await Future.delayed(const Duration(milliseconds: 1000)); + } + + String title = await driver.getText(appBarTitle); + expect(title, "message LOG"); + }, timeout: new Timeout(new Duration(minutes: 5))); + + test('InAppWebViewOnDownloadStartTest', () async { + await Future.delayed(const Duration(milliseconds: 2000)); + final appBarTitle = find.byValueKey('AppBarTitle'); + + while((await driver.getText(appBarTitle)) == "InAppWebViewOnDownloadStartTest") { + await Future.delayed(const Duration(milliseconds: 1000)); + } + + String url = await driver.getText(appBarTitle); + expect(url, "http://192.168.1.20:8082/test-download-file"); + }, timeout: new Timeout(new Duration(minutes: 5))); + + test('InAppWebViewOnTargetBlankTest', () async { + await Future.delayed(const Duration(milliseconds: 2000)); + final appBarTitle = find.byValueKey('AppBarTitle'); + + while((await driver.getText(appBarTitle)) == "InAppWebViewOnTargetBlankTest") { + await Future.delayed(const Duration(milliseconds: 1000)); + } + + String url = await driver.getText(appBarTitle); + expect(url, "https://flutter.dev/"); + }, timeout: new Timeout(new Duration(minutes: 5))); + }); } \ No newline at end of file diff --git a/example/test_driver/in_app_webview_on_console_message_test.dart b/example/test_driver/in_app_webview_on_console_message_test.dart new file mode 100644 index 00000000..9783c8f3 --- /dev/null +++ b/example/test_driver/in_app_webview_on_console_message_test.dart @@ -0,0 +1,61 @@ +import 'package:flutter/material.dart'; + +import 'package:flutter_inappbrowser/flutter_inappbrowser.dart'; + +import 'main_test.dart'; +import 'util_test.dart'; +import 'custom_widget_test.dart'; + +class InAppWebViewOnConsoleMessageTest extends WidgetTest { + final InAppWebViewOnConsoleMessageTestState state = InAppWebViewOnConsoleMessageTestState(); + + @override + InAppWebViewOnConsoleMessageTestState createState() => state; +} + +class InAppWebViewOnConsoleMessageTestState extends WidgetTestState { + String appBarTitle = "InAppWebViewOnConsoleMessageTest"; + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: myAppBar(state: this, title: appBarTitle), + body: Container( + child: Column(children: [ + Expanded( + child: Container( + child: InAppWebView( + initialFile: "test_assets/in_app_webview_on_console_message_test.html", + initialHeaders: {}, + initialOptions: InAppWebViewWidgetOptions( + inAppWebViewOptions: InAppWebViewOptions( + clearCache: true, + debuggingEnabled: true + ) + ), + onWebViewCreated: (InAppWebViewController controller) { + webView = controller; + }, + onLoadStart: (InAppWebViewController controller, String url) { + + }, + onLoadStop: (InAppWebViewController controller, String url) { + setState(() { + appBarTitle = "true"; + }); + nextTest(context: context, state: this); + }, + onConsoleMessage: (InAppWebViewController controller, ConsoleMessage consoleMessage) { + setState(() { + appBarTitle = consoleMessage.message + " " + consoleMessage.messageLevel.toString(); + }); + nextTest(context: context, state: this); + }, + ), + ), + ), + ]) + ) + ); + } +} diff --git a/example/test_driver/in_app_webview_on_download_start_test.dart b/example/test_driver/in_app_webview_on_download_start_test.dart new file mode 100644 index 00000000..0183ac98 --- /dev/null +++ b/example/test_driver/in_app_webview_on_download_start_test.dart @@ -0,0 +1,59 @@ +import 'package:flutter/material.dart'; + +import 'package:flutter_inappbrowser/flutter_inappbrowser.dart'; + +import 'main_test.dart'; +import 'util_test.dart'; +import 'custom_widget_test.dart'; + +class InAppWebViewOnDownloadStartTest extends WidgetTest { + final InAppWebViewOnDownloadStartTestState state = InAppWebViewOnDownloadStartTestState(); + + @override + InAppWebViewOnDownloadStartTestState createState() => state; +} + +class InAppWebViewOnDownloadStartTestState extends WidgetTestState { + String appBarTitle = "InAppWebViewOnDownloadStartTest"; + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: myAppBar(state: this, title: appBarTitle), + body: Container( + child: Column(children: [ + Expanded( + child: Container( + child: InAppWebView( + initialFile: "test_assets/in_app_webview_on_downlaod_start_test.html", + initialHeaders: {}, + initialOptions: InAppWebViewWidgetOptions( + inAppWebViewOptions: InAppWebViewOptions( + clearCache: true, + debuggingEnabled: true, + useOnDownloadStart: true + ) + ), + onWebViewCreated: (InAppWebViewController controller) { + webView = controller; + }, + onLoadStart: (InAppWebViewController controller, String url) { + + }, + onLoadStop: (InAppWebViewController controller, String url) { + + }, + onDownloadStart: (InAppWebViewController controller, String url) { + setState(() { + appBarTitle = url; + }); + nextTest(context: context, state: this); + }, + ), + ), + ), + ]) + ) + ); + } +} diff --git a/example/test_driver/in_app_webview_on_target_blank_test.dart b/example/test_driver/in_app_webview_on_target_blank_test.dart new file mode 100644 index 00000000..8feabf99 --- /dev/null +++ b/example/test_driver/in_app_webview_on_target_blank_test.dart @@ -0,0 +1,59 @@ +import 'package:flutter/material.dart'; + +import 'package:flutter_inappbrowser/flutter_inappbrowser.dart'; + +import 'main_test.dart'; +import 'util_test.dart'; +import 'custom_widget_test.dart'; + +class InAppWebViewOnTargetBlankTest extends WidgetTest { + final InAppWebViewOnTargetBlankTestState state = InAppWebViewOnTargetBlankTestState(); + + @override + InAppWebViewOnTargetBlankTestState createState() => state; +} + +class InAppWebViewOnTargetBlankTestState extends WidgetTestState { + String appBarTitle = "InAppWebViewOnTargetBlankTest"; + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: myAppBar(state: this, title: appBarTitle), + body: Container( + child: Column(children: [ + Expanded( + child: Container( + child: InAppWebView( + initialFile: "test_assets/in_app_webview_on_target_blank_test.html", + initialHeaders: {}, + initialOptions: InAppWebViewWidgetOptions( + inAppWebViewOptions: InAppWebViewOptions( + clearCache: true, + debuggingEnabled: true, + useOnTargetBlank: true + ) + ), + onWebViewCreated: (InAppWebViewController controller) { + webView = controller; + }, + onLoadStart: (InAppWebViewController controller, String url) { + + }, + onLoadStop: (InAppWebViewController controller, String url) { + + }, + onTargetBlank: (InAppWebViewController controller, String url) { + setState(() { + appBarTitle = url; + }); + nextTest(context: context, state: this); + }, + ), + ), + ), + ]) + ) + ); + } +} diff --git a/example/test_driver/in_app_webview_should_override_url_loading_test.dart b/example/test_driver/in_app_webview_should_override_url_loading_test.dart new file mode 100644 index 00000000..c56b491d --- /dev/null +++ b/example/test_driver/in_app_webview_should_override_url_loading_test.dart @@ -0,0 +1,60 @@ +import 'package:flutter/material.dart'; + +import 'package:flutter_inappbrowser/flutter_inappbrowser.dart'; + +import 'custom_widget_test.dart'; +import 'main_test.dart'; +import 'util_test.dart'; + +class InAppWebViewShouldOverrideUrlLoadingTest extends WidgetTest { + final InAppWebViewShouldOverrideUrlLoadingTestState state = InAppWebViewShouldOverrideUrlLoadingTestState(); + + @override + InAppWebViewShouldOverrideUrlLoadingTestState createState() => state; +} + +class InAppWebViewShouldOverrideUrlLoadingTestState extends WidgetTestState { + String url = "https://flutter.dev/"; + String appBarTitle = "InAppWebViewShouldOverrideUrlLoadingTest"; + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: myAppBar(state: this, title: appBarTitle), + body: Container( + child: Column(children: [ + Expanded( + child: Container( + child: InAppWebView( + initialUrl: "https://www.google.com/", + initialHeaders: {}, + initialOptions: InAppWebViewWidgetOptions( + inAppWebViewOptions: InAppWebViewOptions( + clearCache: true, + debuggingEnabled: true, + useShouldOverrideUrlLoading: true + ) + ), + onWebViewCreated: (InAppWebViewController controller) { + webView = controller; + }, + onLoadStart: (InAppWebViewController controller, String url) { + + }, + onLoadStop: (InAppWebViewController controller, String url) { + setState(() { + appBarTitle = url; + }); + nextTest(context: context, state: this); + }, + shouldOverrideUrlLoading: (InAppWebViewController controller, String url) { + controller.loadUrl(url: url); + }, + ), + ), + ), + ]) + ) + ); + } +} diff --git a/example/test_driver/main_test.dart b/example/test_driver/main_test.dart index 3b035a75..3746cf3c 100644 --- a/example/test_driver/main_test.dart +++ b/example/test_driver/main_test.dart @@ -8,8 +8,12 @@ import 'in_app_webview_fetch_test.dart'; import 'in_app_webview_initial_file_test.dart'; import 'in_app_webview_initial_url_test.dart'; import 'in_app_webview_javascript_handler_test.dart'; +import 'in_app_webview_on_console_message_test.dart'; +import 'in_app_webview_on_download_start_test.dart'; import 'in_app_webview_on_load_resource_custom_scheme_test.dart'; import 'in_app_webview_on_load_resource_test.dart'; +import 'in_app_webview_on_target_blank_test.dart'; +import 'in_app_webview_should_override_url_loading_test.dart'; List testRoutes = []; Map buildRoutes({@required BuildContext context}) { @@ -21,6 +25,11 @@ Map buildRoutes({@required BuildContext context}) { '/InAppWebViewAjaxTest': (context) => InAppWebViewAjaxTest(), '/InAppWebViewOnLoadResourceCustomSchemeTest': (context) => InAppWebViewOnLoadResourceCustomSchemeTest(), '/InAppWebViewFetchTest': (context) => InAppWebViewFetchTest(), + '/InAppWebViewFetchTest': (context) => InAppWebViewFetchTest(), + '/InAppWebViewShouldOverrideUrlLoadingTest': (context) => InAppWebViewShouldOverrideUrlLoadingTest(), + '/InAppWebViewOnConsoleMessageTest': (context) => InAppWebViewOnConsoleMessageTest(), + '/InAppWebViewOnDownloadStartTest': (context) => InAppWebViewOnDownloadStartTest(), + '/InAppWebViewOnTargetBlankTest': (context) => InAppWebViewOnTargetBlankTest(), }; routes.forEach((k, v) => testRoutes.add(k)); return routes; diff --git a/ios/Classes/InAppWebView.swift b/ios/Classes/InAppWebView.swift index a96d85d7..d8bdeea5 100755 --- a/ios/Classes/InAppWebView.swift +++ b/ios/Classes/InAppWebView.swift @@ -1204,48 +1204,35 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate, WKNavi } } - public func injectDeferredObject(source: String, withWrapper jsWrapper: String, result: FlutterResult?) { - let jsonData: Data? = try? JSONSerialization.data(withJSONObject: [source], options: []) - let sourceArrayString = String(data: jsonData!, encoding: String.Encoding.utf8) - if sourceArrayString != nil { + public func injectDeferredObject(source: String, withWrapper jsWrapper: String?, result: FlutterResult?) { + var jsToInject = source + if let wrapper = jsWrapper { + let jsonData: Data? = try? JSONSerialization.data(withJSONObject: [source], options: []) + let sourceArrayString = String(data: jsonData!, encoding: String.Encoding.utf8) let sourceString: String? = (sourceArrayString! as NSString).substring(with: NSRange(location: 1, length: (sourceArrayString?.count ?? 0) - 2)) - let jsToInject = String(format: jsWrapper, sourceString!) - - evaluateJavaScript(jsToInject, completionHandler: {(value, error) in - if result == nil { - return - } - - if error != nil { - let userInfo = (error! as NSError).userInfo - self.onConsoleMessage(sourceURL: (userInfo["WKJavaScriptExceptionSourceURL"] as? URL)?.absoluteString ?? "", lineNumber: userInfo["WKJavaScriptExceptionLineNumber"] as! Int, message: userInfo["WKJavaScriptExceptionMessage"] as! String, messageLevel: 3) - } - - if value == nil { - result!("") - return - } - - do { - let data: Data = ("[" + String(describing: value!) + "]").data(using: String.Encoding.utf8, allowLossyConversion: false)! - let json: Array = try JSONSerialization.jsonObject(with: data, options: []) as! Array - if json[0] is String { - result!(json[0]) - } - else { - result!(value) - } - } catch let error as NSError { - result!(FlutterError(code: "InAppBrowserFlutterPlugin", message: "Failed to load: \(error.localizedDescription)", details: error)) - } - - }) + jsToInject = String(format: wrapper, sourceString!) } + evaluateJavaScript(jsToInject, completionHandler: {(value, error) in + if result == nil { + return + } + + if error != nil { + let userInfo = (error! as NSError).userInfo + self.onConsoleMessage(message: userInfo["WKJavaScriptExceptionMessage"] as! String, messageLevel: 3) + } + + if value == nil { + result!("") + return + } + + result!(value) + }) } public func evaluateJavascript(source: String, result: FlutterResult?) { - let jsWrapper = "(function(){return JSON.stringify(eval(%@));})();" - injectDeferredObject(source: source, withWrapper: jsWrapper, result: result) + injectDeferredObject(source: source, withWrapper: nil, result: result) } public func injectJavascriptFileFromUrl(urlFile: String) { @@ -2007,8 +1994,8 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate, WKNavi } } - public func onConsoleMessage(sourceURL: String, lineNumber: Int, message: String, messageLevel: Int) { - var arguments: [String: Any] = ["sourceURL": sourceURL, "lineNumber": lineNumber, "message": message, "messageLevel": messageLevel] + public func onConsoleMessage(message: String, messageLevel: Int) { + var arguments: [String: Any] = ["message": message, "messageLevel": messageLevel] if IABController != nil { arguments["uuid"] = IABController!.uuid } @@ -2065,7 +2052,7 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate, WKNavi messageLevel = 1 break; } - onConsoleMessage(sourceURL: "", lineNumber: 1, message: message.body as! String, messageLevel: messageLevel) + onConsoleMessage(message: message.body as! String, messageLevel: messageLevel) } else if message.name == "callHandler" { let body = message.body as! [String: Any] diff --git a/lib/src/in_app_webview.dart b/lib/src/in_app_webview.dart index 4769baf3..83a0df3f 100755 --- a/lib/src/in_app_webview.dart +++ b/lib/src/in_app_webview.dart @@ -418,11 +418,9 @@ class InAppWebViewController { _inAppBrowser.shouldOverrideUrlLoading(url); break; case "onConsoleMessage": - String sourceURL = call.arguments["sourceURL"]; - int lineNumber = call.arguments["lineNumber"]; String message = call.arguments["message"]; ConsoleMessageLevel messageLevel = ConsoleMessageLevel.fromValue(call.arguments["messageLevel"]); - ConsoleMessage consoleMessage = ConsoleMessage(sourceURL: sourceURL, lineNumber: lineNumber, message: message, messageLevel: messageLevel); + ConsoleMessage consoleMessage = ConsoleMessage(message: message, messageLevel: messageLevel); if (_widget != null && _widget.onConsoleMessage != null) _widget.onConsoleMessage(this, consoleMessage); else if (_inAppBrowser != null) @@ -744,7 +742,7 @@ class InAppWebViewController { InAppWebViewWidgetOptions options = await getOptions(); if (options != null && options.inAppWebViewOptions.javaScriptEnabled == true) { html = await evaluateJavascript(source: "window.document.getElementsByTagName('html')[0].outerHTML;"); - if (html.isNotEmpty) + if (html != null && html.isNotEmpty) return html; } @@ -1064,14 +1062,17 @@ class InAppWebViewController { } ///Evaluates JavaScript code into the WebView and returns the result of the evaluation. - Future evaluateJavascript({@required String source}) async { + Future evaluateJavascript({@required String source}) async { Map args = {}; if (_inAppBrowserUuid != null && _inAppBrowser != null) { _inAppBrowser.throwIsNotOpened(); args.putIfAbsent('uuid', () => _inAppBrowserUuid); } args.putIfAbsent('source', () => source); - return await _channel.invokeMethod('evaluateJavascript', args); + var data = await _channel.invokeMethod('evaluateJavascript', args); + if (data != null && Platform.isAndroid) + data = json.decode(data); + return data; } ///Injects an external JavaScript file into the WebView from a defined url. diff --git a/lib/src/types.dart b/lib/src/types.dart index e977cdb7..6a1d1de1 100644 --- a/lib/src/types.dart +++ b/lib/src/types.dart @@ -33,6 +33,21 @@ class ConsoleMessageLevel { return null; } toValue() => _value; + toString() { + switch(_value) { + case 0: + return "TIP"; + case 2: + return "WARNING"; + case 3: + return "ERROR"; + case 4: + return "DEBUG"; + case 1: + default: + return "LOG"; + } + } static const TIP = const ConsoleMessageLevel._internal(0); static const LOG = const ConsoleMessageLevel._internal(1); @@ -40,7 +55,6 @@ class ConsoleMessageLevel { static const ERROR = const ConsoleMessageLevel._internal(3); static const DEBUG = const ConsoleMessageLevel._internal(4); - bool operator ==(value) => value == _value; @override @@ -143,12 +157,10 @@ class CustomSchemeResponse { ///To receive notifications of these messages, use the [onConsoleMessage] event. class ConsoleMessage { - String sourceURL; - int lineNumber; String message; ConsoleMessageLevel messageLevel; - ConsoleMessage({this.sourceURL = "", this.lineNumber = 1, this.message = "", this.messageLevel = ConsoleMessageLevel.LOG}); + ConsoleMessage({this.message = "", this.messageLevel = ConsoleMessageLevel.LOG}); } ///WebHistory class.