added onConsoleMessage and onScrollChanged web support

This commit is contained in:
Lorenzo Pichilli 2022-04-22 14:41:05 +02:00
parent 9a5c952cf2
commit 2570cb38eb
5 changed files with 101 additions and 4 deletions

View File

@ -19,10 +19,17 @@
<title>flutter_inappwebview_example</title>
<link rel="manifest" href="manifest.json">
</head>
<body>
<body style="min-height: 3000px;">
<h1>Simple Page 1</h1>
<a href="/page-2.html">Go to page 2</a>
<br>
<a href="/heavy-page.html">Go to heavy-page</a>
<script>
window.addEventListener('load', function (event) {
setTimeout(function () {
console.log('test');
});
});
</script>
</body>
</html>

View File

@ -7,6 +7,34 @@ window.flutter_inappwebview = {
if (iframe != null) {
window.flutter_inappwebview.iframe = iframe;
iframe.addEventListener('load', function (event) {
try {
var oldLogs = {
'log': iframe.contentWindow.console.log,
'debug': iframe.contentWindow.console.debug,
'error': iframe.contentWindow.console.error,
'info': iframe.contentWindow.console.info,
'warn': iframe.contentWindow.console.warn
};
for (var k in oldLogs) {
(function(oldLog) {
iframe.contentWindow.console[oldLog] = function() {
var message = '';
for (var i in arguments) {
if (message == '') {
message += arguments[i];
} else {
message += ' ' + arguments[i];
}
}
oldLogs[oldLog].call(iframe.contentWindow.console, ...arguments);
window.flutter_inappwebview.nativeCommunication('onConsoleMessage', window.flutter_inappwebview.viewId, [oldLog, message]);
}
})(k);
}
} catch (e) {
console.log(e);
}
var url = iframe.src;
try {
url = iframe.contentWindow.location.href;
@ -51,6 +79,18 @@ window.flutter_inappwebview = {
} catch (e) {
console.log(e);
}
iframe.contentWindow.addEventListener('scroll', function (event) {
var x = 0;
var y = 0;
try {
x = iframe.contentWindow.scrollX;
y = iframe.contentWindow.scrollY;
} catch (e) {
console.log(e);
}
window.flutter_inappwebview.nativeCommunication('onScrollChanged', window.flutter_inappwebview.viewId, [x, y]);
});
});
}
},

View File

@ -25,7 +25,7 @@ abstract class WebView {
///Event fired when the [WebView] starts to load an [url].
///
///**NOTE**: on Web it will be dispatched at the same time of [onLoadStop] event
///**NOTE for Web**: it will be dispatched at the same time of [onLoadStop] event
///because there isn't any way to capture the real load start event from an iframe.
///
///**Supported Platforms/Implementations**:
@ -76,9 +76,12 @@ abstract class WebView {
///Event fired when the [WebView] receives a [ConsoleMessage].
///
///**NOTE for Web**: this event will be called only if the iframe has the same origin.
///
///**Supported Platforms/Implementations**:
///- Android native WebView ([Official API - WebChromeClient.onConsoleMessage](https://developer.android.com/reference/android/webkit/WebChromeClient#onConsoleMessage(android.webkit.ConsoleMessage)))
///- iOS
///- Web
final void Function(
InAppWebViewController controller, ConsoleMessage consoleMessage)?
onConsoleMessage;
@ -120,9 +123,12 @@ abstract class WebView {
///
///[y] represents the current vertical scroll origin in pixels.
///
///**NOTE for Web**: this event will be called only if the iframe has the same origin.
///
///**Supported Platforms/Implementations**:
///- Android native WebView ([Official API - WebView.onScrollChanged](https://developer.android.com/reference/android/webkit/WebView#onScrollChanged(int,%20int,%20int,%20int)))
///- iOS ([Official API - UIScrollViewDelegate.scrollViewDidScroll](https://developer.apple.com/documentation/uikit/uiscrollviewdelegate/1619392-scrollviewdidscroll))
///- Web ([Official API - Window.onscroll](https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onscroll))
final void Function(InAppWebViewController controller, int x, int y)?
onScrollChanged;
@ -164,10 +170,10 @@ abstract class WebView {
///
///**NOTE**: to allow JavaScript to open windows, you need to set [InAppWebViewSettings.javaScriptCanOpenWindowsAutomatically] option to `true`.
///
///**NOTE**: on Android you need to set [InAppWebViewSettings.supportMultipleWindows] option to `true`.
///**NOTE for Android**: you need to set [InAppWebViewSettings.supportMultipleWindows] option to `true`.
///Also, if the request has been created using JavaScript (`window.open()`), then there are some limitation: check the [NavigationAction] class.
///
///**NOTE**: on iOS, setting these initial options: [InAppWebViewSettings.supportZoom], [InAppWebViewSettings.useOnLoadResource], [InAppWebViewSettings.useShouldInterceptAjaxRequest],
///**NOTE for iOS**: setting these initial options: [InAppWebViewSettings.supportZoom], [InAppWebViewSettings.useOnLoadResource], [InAppWebViewSettings.useShouldInterceptAjaxRequest],
///[InAppWebViewSettings.useShouldInterceptFetchRequest], [InAppWebViewSettings.applicationNameForUserAgent], [InAppWebViewSettings.javaScriptCanOpenWindowsAutomatically],
///[InAppWebViewSettings.javaScriptEnabled], [InAppWebViewSettings.minimumFontSize], [InAppWebViewSettings.preferredContentMode], [InAppWebViewSettings.incognito],
///[InAppWebViewSettings.cacheEnabled], [InAppWebViewSettings.mediaPlaybackRequiresUserGesture],
@ -380,6 +386,8 @@ abstract class WebView {
///
///[isReload] indicates if this url is being reloaded. Available only on Android.
///
///**NOTE for Web**: this event will be called only if the iframe has the same origin.
///
///**Supported Platforms/Implementations**:
///- Android native WebView ([Official API - WebViewClient.doUpdateVisitedHistory](https://developer.android.com/reference/android/webkit/WebViewClient#doUpdateVisitedHistory(android.webkit.WebView,%20java.lang.String,%20boolean)))
///- iOS

View File

@ -233,4 +233,36 @@ class InAppWebViewWebElement {
};
_channel.invokeMethod("onUpdateVisitedHistory", obj);
}
onScrollChanged(int x, int y) async {
var obj = {
"x": x,
"y": y
};
_channel.invokeMethod("onScrollChanged", obj);
}
onConsoleMessage(String type, String? message) async {
int messageLevel = 1;
switch (type) {
case 'debug':
messageLevel = 0;
break;
case 'error':
messageLevel = 3;
break;
case 'warn':
messageLevel = 2;
break;
case 'info':
case 'log':
default:
messageLevel = 1;
}
var obj = {
"messageLevel": messageLevel,
"message": message
};
_channel.invokeMethod("onConsoleMessage", obj);
}
}

View File

@ -65,6 +65,16 @@ void _dartNativeCommunication(String method, int viewId, [List? args]) {
String url = args![0] as String;
webViewHtmlElement.onUpdateVisitedHistory(url);
break;
case 'onScrollChanged':
int x = args![0] as int;
int y = args[1] as int;
webViewHtmlElement.onScrollChanged(x, y);
break;
case 'onConsoleMessage':
String type = args![0] as String;
String? message = args[1] as String?;
webViewHtmlElement.onConsoleMessage(type, message);
break;
}
}
}