Fixed iOS userContentController didReceive WKScriptMessage event when using a WebView created with a windowId
This commit is contained in:
parent
b8ec14d6b6
commit
f7af3130a4
|
@ -1,8 +1,9 @@
|
||||||
## 4.0.0+3
|
## 4.0.0+4
|
||||||
|
|
||||||
- Reverted calling `handler.post` on Android when a WebView is created
|
- Reverted calling `handler.post` on Android when a WebView is created
|
||||||
- Fixed iOS extra bottom padding when opening the keyboard
|
- Fixed iOS extra bottom padding when opening the keyboard
|
||||||
- Fixed "Build for web not working – The integer literal 9223372036854775807 can't be represented exactly in JavaScript" [#429](https://github.com/pichillilorenzo/flutter_inappwebview/issues/429)
|
- Fixed "Build for web not working – The integer literal 9223372036854775807 can't be represented exactly in JavaScript" [#429](https://github.com/pichillilorenzo/flutter_inappwebview/issues/429)
|
||||||
|
- Fixed iOS userContentController didReceive WKScriptMessage event when using a WebView created with a `windowId`
|
||||||
|
|
||||||
## 4.0.0
|
## 4.0.0
|
||||||
|
|
||||||
|
|
|
@ -52,8 +52,9 @@ if (window.Promise == null) {
|
||||||
let javaScriptBridgeJS = """
|
let javaScriptBridgeJS = """
|
||||||
window.\(JAVASCRIPT_BRIDGE_NAME) = {};
|
window.\(JAVASCRIPT_BRIDGE_NAME) = {};
|
||||||
window.\(JAVASCRIPT_BRIDGE_NAME).callHandler = function() {
|
window.\(JAVASCRIPT_BRIDGE_NAME).callHandler = function() {
|
||||||
|
var _windowId = window._flutter_inappwebview_windowId;
|
||||||
var _callHandlerID = setTimeout(function(){});
|
var _callHandlerID = setTimeout(function(){});
|
||||||
window.webkit.messageHandlers['callHandler'].postMessage( {'handlerName': arguments[0], '_callHandlerID': _callHandlerID, 'args': JSON.stringify(Array.prototype.slice.call(arguments, 1))} );
|
window.webkit.messageHandlers['callHandler'].postMessage( {'handlerName': arguments[0], '_callHandlerID': _callHandlerID, 'args': JSON.stringify(Array.prototype.slice.call(arguments, 1)), '_windowId': _windowId} );
|
||||||
return new Promise(function(resolve, reject) {
|
return new Promise(function(resolve, reject) {
|
||||||
window.\(JAVASCRIPT_BRIDGE_NAME)[_callHandlerID] = resolve;
|
window.\(JAVASCRIPT_BRIDGE_NAME)[_callHandlerID] = resolve;
|
||||||
});
|
});
|
||||||
|
@ -63,6 +64,7 @@ window.\(JAVASCRIPT_BRIDGE_NAME).callHandler = function() {
|
||||||
// the message needs to be concatenated with '' in order to have the same behavior like on Android
|
// the message needs to be concatenated with '' in order to have the same behavior like on Android
|
||||||
let consoleLogJS = """
|
let consoleLogJS = """
|
||||||
(function(console) {
|
(function(console) {
|
||||||
|
|
||||||
var oldLogs = {
|
var oldLogs = {
|
||||||
'consoleLog': console.log,
|
'consoleLog': console.log,
|
||||||
'consoleDebug': console.debug,
|
'consoleDebug': console.debug,
|
||||||
|
@ -83,7 +85,9 @@ let consoleLogJS = """
|
||||||
message += ' ' + arguments[i];
|
message += ' ' + arguments[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
window.webkit.messageHandlers[oldLog].postMessage(message);
|
|
||||||
|
var _windowId = window._flutter_inappwebview_windowId;
|
||||||
|
window.webkit.messageHandlers[oldLog].postMessage({'message': message, '_windowId': _windowId});
|
||||||
oldLogs[oldLog].apply(null, arguments);
|
oldLogs[oldLog].apply(null, arguments);
|
||||||
}
|
}
|
||||||
})(k);
|
})(k);
|
||||||
|
@ -141,12 +145,17 @@ function wkwebview_FindAllAsyncForElement(element, keyword) {
|
||||||
value.substr(idx + keyword.length)
|
value.substr(idx + keyword.length)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
var _windowId = window._flutter_inappwebview_windowId;
|
||||||
|
|
||||||
window.webkit.messageHandlers["onFindResultReceived"].postMessage(
|
window.webkit.messageHandlers["onFindResultReceived"].postMessage(
|
||||||
JSON.stringify({
|
{
|
||||||
activeMatchOrdinal: wkwebview_CurrentHighlight,
|
'findResult': {
|
||||||
numberOfMatches: wkwebview_SearchResultCount,
|
'activeMatchOrdinal': wkwebview_CurrentHighlight,
|
||||||
isDoneCounting: wkwebview_IsDoneCounting
|
'numberOfMatches': wkwebview_SearchResultCount,
|
||||||
})
|
'isDoneCounting': wkwebview_IsDoneCounting
|
||||||
|
},
|
||||||
|
'_windowId': _windowId
|
||||||
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} else if (element.nodeType == 1) {
|
} else if (element.nodeType == 1) {
|
||||||
|
@ -171,12 +180,18 @@ function wkwebview_FindAllAsync(keyword) {
|
||||||
wkwebview_ClearMatches();
|
wkwebview_ClearMatches();
|
||||||
wkwebview_FindAllAsyncForElement(document.body, keyword.toLowerCase());
|
wkwebview_FindAllAsyncForElement(document.body, keyword.toLowerCase());
|
||||||
wkwebview_IsDoneCounting = true;
|
wkwebview_IsDoneCounting = true;
|
||||||
|
|
||||||
|
var _windowId = window._flutter_inappwebview_windowId;
|
||||||
|
|
||||||
window.webkit.messageHandlers["onFindResultReceived"].postMessage(
|
window.webkit.messageHandlers["onFindResultReceived"].postMessage(
|
||||||
JSON.stringify({
|
{
|
||||||
activeMatchOrdinal: wkwebview_CurrentHighlight,
|
'findResult': {
|
||||||
numberOfMatches: wkwebview_SearchResultCount,
|
'activeMatchOrdinal': wkwebview_CurrentHighlight,
|
||||||
isDoneCounting: wkwebview_IsDoneCounting
|
'numberOfMatches': wkwebview_SearchResultCount,
|
||||||
})
|
'isDoneCounting': wkwebview_IsDoneCounting
|
||||||
|
},
|
||||||
|
'_windowId': _windowId
|
||||||
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -238,12 +253,17 @@ function wkwebview_FindNext(forward) {
|
||||||
block: "center"
|
block: "center"
|
||||||
});
|
});
|
||||||
|
|
||||||
|
var _windowId = window._flutter_inappwebview_windowId;
|
||||||
|
|
||||||
window.webkit.messageHandlers["onFindResultReceived"].postMessage(
|
window.webkit.messageHandlers["onFindResultReceived"].postMessage(
|
||||||
JSON.stringify({
|
{
|
||||||
activeMatchOrdinal: wkwebview_CurrentHighlight,
|
'findResult': {
|
||||||
numberOfMatches: wkwebview_SearchResultCount,
|
'activeMatchOrdinal': wkwebview_CurrentHighlight,
|
||||||
isDoneCounting: wkwebview_IsDoneCounting
|
'numberOfMatches': wkwebview_SearchResultCount,
|
||||||
})
|
'isDoneCounting': wkwebview_IsDoneCounting
|
||||||
|
},
|
||||||
|
'_windowId': _windowId
|
||||||
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1141,6 +1161,9 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate, WKNavi
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let userScript = WKUserScript(source: "window._flutter_inappwebview_windowId = \(windowId == nil ? "null" : String(windowId!));" , injectionTime: .atDocumentStart, forMainFrameOnly: false)
|
||||||
|
configuration.userContentController.addUserScript(userScript)
|
||||||
|
|
||||||
if windowId != nil {
|
if windowId != nil {
|
||||||
// the new created window webview has the same WKWebViewConfiguration variable reference
|
// the new created window webview has the same WKWebViewConfiguration variable reference
|
||||||
return
|
return
|
||||||
|
@ -2812,7 +2835,13 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate, WKNavi
|
||||||
if let r = result {
|
if let r = result {
|
||||||
json = r as! String
|
json = r as! String
|
||||||
}
|
}
|
||||||
self.evaluateJavaScript("if(window.\(JAVASCRIPT_BRIDGE_NAME)[\(_callHandlerID)] != null) {window.\(JAVASCRIPT_BRIDGE_NAME)[\(_callHandlerID)](\(json)); delete window.\(JAVASCRIPT_BRIDGE_NAME)[\(_callHandlerID)];}", completionHandler: nil)
|
|
||||||
|
self.evaluateJavaScript("""
|
||||||
|
if(window.\(JAVASCRIPT_BRIDGE_NAME)[\(_callHandlerID)] != null) {
|
||||||
|
window.\(JAVASCRIPT_BRIDGE_NAME)[\(_callHandlerID)](\(json));
|
||||||
|
delete window.\(JAVASCRIPT_BRIDGE_NAME)[\(_callHandlerID)];
|
||||||
|
}
|
||||||
|
""", completionHandler: nil)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -2905,24 +2934,43 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate, WKNavi
|
||||||
messageLevel = 1
|
messageLevel = 1
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
onConsoleMessage(message: message.body as! String, messageLevel: messageLevel)
|
let body = message.body as! [String: Any?]
|
||||||
|
let consoleMessage = body["message"] as! String
|
||||||
|
|
||||||
|
let _windowId = body["_windowId"] as? Int64
|
||||||
|
var webView = self
|
||||||
|
if let wId = _windowId, let webViewTransport = InAppWebView.windowWebViews[wId] {
|
||||||
|
webView = webViewTransport.webView
|
||||||
|
}
|
||||||
|
webView.onConsoleMessage(message: consoleMessage, messageLevel: messageLevel)
|
||||||
} else if message.name == "callHandler" {
|
} else if message.name == "callHandler" {
|
||||||
let body = message.body as! [String: Any]
|
let body = message.body as! [String: Any?]
|
||||||
let handlerName = body["handlerName"] as! String
|
let handlerName = body["handlerName"] as! String
|
||||||
if handlerName == "onPrint" {
|
if handlerName == "onPrint" {
|
||||||
printCurrentPage(printCompletionHandler: nil)
|
printCurrentPage(printCompletionHandler: nil)
|
||||||
}
|
}
|
||||||
let _callHandlerID = body["_callHandlerID"] as! Int64
|
let _callHandlerID = body["_callHandlerID"] as! Int64
|
||||||
let args = body["args"] as! String
|
let args = body["args"] as! String
|
||||||
onCallJsHandler(handlerName: handlerName, _callHandlerID: _callHandlerID, args: args)
|
|
||||||
} else if message.name == "onFindResultReceived" {
|
let _windowId = body["_windowId"] as? Int64
|
||||||
if let resource = convertToDictionary(text: message.body as! String) {
|
var webView = self
|
||||||
let activeMatchOrdinal = resource["activeMatchOrdinal"] as! Int
|
if let wId = _windowId, let webViewTransport = InAppWebView.windowWebViews[wId] {
|
||||||
let numberOfMatches = resource["numberOfMatches"] as! Int
|
webView = webViewTransport.webView
|
||||||
let isDoneCounting = resource["isDoneCounting"] as! Bool
|
|
||||||
|
|
||||||
self.onFindResultReceived(activeMatchOrdinal: activeMatchOrdinal, numberOfMatches: numberOfMatches, isDoneCounting: isDoneCounting)
|
|
||||||
}
|
}
|
||||||
|
webView.onCallJsHandler(handlerName: handlerName, _callHandlerID: _callHandlerID, args: args)
|
||||||
|
} else if message.name == "onFindResultReceived" {
|
||||||
|
let body = message.body as! [String: Any?]
|
||||||
|
let findResult = body["findResult"] as! [String: Any]
|
||||||
|
let activeMatchOrdinal = findResult["activeMatchOrdinal"] as! Int
|
||||||
|
let numberOfMatches = findResult["numberOfMatches"] as! Int
|
||||||
|
let isDoneCounting = findResult["isDoneCounting"] as! Bool
|
||||||
|
|
||||||
|
let _windowId = body["_windowId"] as? Int64
|
||||||
|
var webView = self
|
||||||
|
if let wId = _windowId, let webViewTransport = InAppWebView.windowWebViews[wId] {
|
||||||
|
webView = webViewTransport.webView
|
||||||
|
}
|
||||||
|
webView.onFindResultReceived(activeMatchOrdinal: activeMatchOrdinal, numberOfMatches: numberOfMatches, isDoneCounting: isDoneCounting)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
name: flutter_inappwebview
|
name: flutter_inappwebview
|
||||||
description: A Flutter plugin that allows you to add an inline webview, to use an headless webview, and to open an in-app browser window.
|
description: A Flutter plugin that allows you to add an inline webview, to use an headless webview, and to open an in-app browser window.
|
||||||
version: 4.0.0+3
|
version: 4.0.0+4
|
||||||
homepage: https://github.com/pichillilorenzo/flutter_inappwebview
|
homepage: https://github.com/pichillilorenzo/flutter_inappwebview
|
||||||
|
|
||||||
environment:
|
environment:
|
||||||
|
|
Loading…
Reference in New Issue