diff --git a/flutter_inappwebview_web/lib/assets/web/web_support.js b/flutter_inappwebview_web/lib/assets/web/web_support.js index e3893d874..f68db7e34 100644 --- a/flutter_inappwebview_web/lib/assets/web/web_support.js +++ b/flutter_inappwebview_web/lib/assets/web/web_support.js @@ -304,87 +304,6 @@ window.flutter_inappwebview = { webView.settings = newSettings; }, - reload: function() { - var iframe = webView.iframe; - if (iframe != null && iframe.contentWindow != null) { - try { - iframe.contentWindow.location.reload(); - } catch (e) { - console.log(e); - iframe.contentWindow.location.href = iframe.src; - } - } - }, - goBack: function() { - var iframe = webView.iframe; - if (iframe != null) { - try { - iframe.contentWindow.history.back(); - } catch (e) { - console.log(e); - } - } - }, - goForward: function() { - var iframe = webView.iframe; - if (iframe != null) { - try { - iframe.contentWindow.history.forward(); - } catch (e) { - console.log(e); - } - } - }, - goBackOrForward: function(steps) { - var iframe = webView.iframe; - if (iframe != null) { - try { - iframe.contentWindow.history.go(steps); - } catch (e) { - console.log(e); - } - } - }, - evaluateJavascript: function(source) { - var iframe = webView.iframe; - var result = null; - if (iframe != null) { - try { - result = JSON.stringify(iframe.contentWindow.eval(source)); - } catch (e) {} - } - return result; - }, - stopLoading: function(steps) { - var iframe = webView.iframe; - if (iframe != null) { - try { - iframe.contentWindow.stop(); - } catch (e) { - console.log(e); - } - } - }, - getUrl: function() { - var iframe = webView.iframe; - var url = iframe.src; - try { - url = iframe.contentWindow.location.href; - } catch (e) { - console.log(e); - } - return url; - }, - getTitle: function() { - var iframe = webView.iframe; - var title = null; - try { - title = iframe.contentDocument.title; - } catch (e) { - console.log(e); - } - return title; - }, injectJavascriptFileFromUrl: function(urlFile, scriptHtmlTagAttributes) { var iframe = webView.iframe; try { @@ -472,32 +391,6 @@ window.flutter_inappwebview = { console.log(e); } }, - printCurrentPage: function() { - var iframe = webView.iframe; - try { - iframe.contentWindow.print(); - } catch (e) { - console.log(e); - } - }, - getContentHeight: function() { - var iframe = webView.iframe; - try { - return iframe.contentDocument.documentElement.scrollHeight; - } catch (e) { - console.log(e); - } - return null; - }, - getContentWidth: function() { - var iframe = webView.iframe; - try { - return iframe.contentDocument.documentElement.scrollWidth; - } catch (e) { - console.log(e); - } - return null; - }, getSelectedText: function() { var iframe = webView.iframe; try { @@ -515,73 +408,6 @@ window.flutter_inappwebview = { console.log(e); } return null; - }, - getScrollX: function() { - var iframe = webView.iframe; - try { - return iframe.contentWindow.scrollX; - } catch (e) { - console.log(e); - } - return null; - }, - getScrollY: function() { - var iframe = webView.iframe; - try { - return iframe.contentWindow.scrollY; - } catch (e) { - console.log(e); - } - return null; - }, - isSecureContext: function() { - var iframe = webView.iframe; - try { - return iframe.contentWindow.isSecureContext; - } catch (e) { - console.log(e); - } - return false; - }, - canScrollVertically: function() { - var iframe = webView.iframe; - try { - return iframe.contentDocument.body.scrollHeight > iframe.contentWindow.innerHeight; - } catch (e) { - console.log(e); - } - return false; - }, - canScrollHorizontally: function() { - var iframe = webView.iframe; - try { - return iframe.contentDocument.body.scrollWidth > iframe.contentWindow.innerWidth; - } catch (e) { - console.log(e); - } - return false; - }, - getSize: function() { - var iframeContainer = webView.iframeContainer; - var width = 0.0; - var height = 0.0; - if (iframeContainer.style.width != null && iframeContainer.style.width != '' && iframeContainer.style.width.indexOf('px') > 0) { - width = parseFloat(iframeContainer.style.width); - } - if (width == null || width == 0.0) { - width = iframeContainer.getBoundingClientRect().width; - } - if (iframeContainer.style.height != null && iframeContainer.style.height != '' && iframeContainer.style.height.indexOf('px') > 0) { - height = parseFloat(iframeContainer.style.height); - } - if (height == null || height == 0.0) { - height = iframeContainer.getBoundingClientRect().height; - } - - return { - width: width, - height: height - }; } }; @@ -590,4 +416,4 @@ window.flutter_inappwebview = { getCookieExpirationDate: function(timestamp) { return (new Date(timestamp)).toUTCString(); } -}; \ No newline at end of file +}; diff --git a/flutter_inappwebview_web/lib/web/in_app_web_view_web_element.dart b/flutter_inappwebview_web/lib/web/in_app_web_view_web_element.dart index c56832b6f..da2d612ac 100644 --- a/flutter_inappwebview_web/lib/web/in_app_web_view_web_element.dart +++ b/flutter_inappwebview_web/lib/web/in_app_web_view_web_element.dart @@ -1,4 +1,5 @@ import 'dart:async'; +import 'dart:convert'; import 'dart:developer'; import 'dart:js_interop'; @@ -17,6 +18,10 @@ extension on HTMLIFrameElement { external String? get csp; } +extension on Window { + external dynamic eval(JSString str); +} + class InAppWebViewWebElement implements Disposable { late dynamic _viewId; late BinaryMessenger _messenger; @@ -317,31 +322,67 @@ class InAppWebViewWebElement implements Disposable { } Future reload() async { - jsWebView?.reload(); + final iframeWindow = iframe.contentWindow; + if (iframeWindow != null) { + try { + iframeWindow.location.reload(); + } catch (e) { + print(e); + iframeWindow.location.href = iframe.src; + } + } } Future goBack() async { - jsWebView?.goBack(); + try { + iframe.contentWindow?.history.back(); + } catch (e) { + print(e); + } } Future goForward() async { - jsWebView?.goForward(); + try { + iframe.contentWindow?.history.forward(); + } catch (e) { + print(e); + } } Future goBackOrForward({required int steps}) async { - jsWebView?.goBackOrForward(steps.toJS); + try { + iframe.contentWindow?.history.go(steps); + } catch (e) { + print(e); + } } - Future evaluateJavascript({required String source}) async { - return jsWebView?.evaluateJavascript(source.toJS)?.toDart; + Future evaluateJavascript({required String source}) async { + var result = null; + final iframeWindow = iframe.contentWindow; + if (iframeWindow != null) { + try { + result = jsonEncode(iframeWindow.eval(source.toJS)); + } catch (e) {} + } + return result; } Future stopLoading() async { - jsWebView?.stopLoading(); + try { + iframe.contentWindow?.stop(); + } catch (e) { + print(e); + } } Future getUrl() async { - String? url = jsWebView?.getUrl()?.toDart; + String? url; + try { + url = iframe.contentWindow?.location.href; + } catch (e) { + print(e); + } if (url == null || url.isEmpty || url == 'about:blank') { url = iframe.src; } @@ -349,7 +390,7 @@ class InAppWebViewWebElement implements Disposable { } Future getTitle() async { - return jsWebView?.getTitle()?.toDart; + return iframe.contentDocument?.title; } Future postUrl( @@ -388,15 +429,29 @@ class InAppWebViewWebElement implements Disposable { } Future printCurrentPage() async { - jsWebView?.printCurrentPage(); + try { + iframe.contentWindow?.print(); + } catch (e) { + print(e); + } } Future getContentHeight() async { - return jsWebView?.getContentHeight()?.toDartInt; + try { + return iframe.contentDocument?.documentElement?.scrollHeight; + } catch (e) { + print(e); + } + return null; } Future getContentWidth() async { - return jsWebView?.getContentWidth()?.toDartInt; + try { + return iframe.contentDocument?.documentElement?.scrollWidth; + } catch (e) { + print(e); + } + return null; } Future getOriginalUrl() async { @@ -412,23 +467,45 @@ class InAppWebViewWebElement implements Disposable { } Future getScrollX() async { - return jsWebView?.getScrollX()?.toDartInt; + try { + return iframe.contentWindow?.scrollX.toInt(); + } catch (e) { + print(e); + } + return null; } Future getScrollY() async { - return jsWebView?.getScrollY()?.toDartInt; + try { + return iframe.contentWindow?.scrollY.toInt(); + } catch (e) { + print(e); + } + return null; } Future isSecureContext() async { - return jsWebView?.isSecureContext().toDart ?? false; + return iframe.contentWindow?.isSecureContext ?? false; } Future canScrollVertically() async { - return jsWebView?.canScrollVertically().toDart ?? false; + try { + return (iframe.contentDocument?.body?.scrollHeight ?? 0) > + (iframe.contentWindow?.innerHeight ?? 0); + } catch (e) { + print(e); + } + return false; } Future canScrollHorizontally() async { - return jsWebView?.canScrollHorizontally().toDart ?? false; + try { + return (iframe.contentDocument?.body?.scrollWidth ?? 0) > + (iframe.contentWindow?.innerWidth ?? 0); + } catch (e) { + print(e); + } + return false; } Set getSandbox() { @@ -444,8 +521,22 @@ class InAppWebViewWebElement implements Disposable { } Size getSize() { - var size = jsWebView?.getSize(); - return Size(size!.width!.toDartDouble, size.height!.toDartDouble); + double? width = 0.0; + double? height = 0.0; + if (iframeContainer.style.width.indexOf('px') > 0) { + width = double.tryParse(iframeContainer.style.width.split('px')[0]); + } + if (width == null || width == 0.0) { + width = iframeContainer.getBoundingClientRect().width; + } + if (iframeContainer.style.height.indexOf('px') > 0) { + height = double.tryParse(iframeContainer.style.height.split('px')[0]); + } + if (height == null || height == 0.0) { + height = iframeContainer.getBoundingClientRect().height; + } + + return Size(width, height); } Future setSettings(InAppWebViewSettings newSettings) async { diff --git a/flutter_inappwebview_web/lib/web/js_bridge.dart b/flutter_inappwebview_web/lib/web/js_bridge.dart index 31b0a05ea..94814e4ae 100644 --- a/flutter_inappwebview_web/lib/web/js_bridge.dart +++ b/flutter_inappwebview_web/lib/web/js_bridge.dart @@ -13,14 +13,6 @@ extension type JSWebView._(JSObject _) implements JSObject { external HTMLDivElement get iframeContainer; external void prepare(JSAny? settings); external void setSettings(JSAny? newSettings); - external void reload(); - external void goBack(); - external void goForward(); - external void goBackOrForward(JSNumber steps); - external JSString? evaluateJavascript(JSString source); - external void stopLoading(); - external JSString? getUrl(); - external JSString? getTitle(); external void injectJavascriptFileFromUrl( JSString urlFile, JSAny? scriptHtmlTagAttributes); external void injectCSSCode(JSString source); @@ -28,16 +20,7 @@ extension type JSWebView._(JSObject _) implements JSObject { JSString urlFile, JSAny? cssLinkHtmlTagAttributes); external void scrollTo(JSNumber x, JSNumber y, JSBoolean animated); external void scrollBy(JSNumber x, JSNumber y, JSBoolean animated); - external void printCurrentPage(); - external JSNumber? getContentHeight(); - external JSNumber? getContentWidth(); external JSPromise? getSelectedText(); - external JSNumber? getScrollX(); - external JSNumber? getScrollY(); - external JSBoolean isSecureContext(); - external JSBoolean canScrollVertically(); - external JSBoolean canScrollHorizontally(); - external JSSize getSize(); } @JS('window.flutter_inappwebview')