javascript - Flutter webview_flutter:无法使用 java 脚本代码检测平台
问题描述
我正在使用 webview_flutter 来显示一个网页。我正在使用下面的代码来检测平台,比如它是 android 还是 ios。我下面的代码不起作用。Flutter webview - https://pub.dev/packages/webview_flutter 我在正文中添加了以下类。
class - view-withKeyboard
function applyAfterResize() {
console.log(getMobileOperatingSystem());
if (getMobileOperatingSystem() == 'ios') {
if (originalPotion !== false) {
var wasWithKeyboard = $('body').hasClass('view-withKeyboard');
var nowWithKeyboard = false;
var diff = Math.abs(originalPotion - ($(window).width() + $(window).height()));
if (diff > 100) nowWithKeyboard = true;
$('body').toggleClass('view-withKeyboard', nowWithKeyboard);
if (wasWithKeyboard != nowWithKeyboard) {
//onKeyboardOnOff(nowWithKeyboard);
}
}
}
}
$(document).on('focus blur', '.white-div input[type=text]', function(e){
//alert('here');
if (getMobileOperatingSystem() == 'ios') {
var $obj = $(this);
var nowWithKeyboard = (e.type == 'focusin');
$('body').toggleClass('view-withKeyboard', nowWithKeyboard);
onKeyboardOnOff(nowWithKeyboard);
}
});
解决方案
首先启用JavascriptMOde并使用evaluateJavascript
WebView(
javascriptMode: JavascriptMode.unrestricted,
initialUrl: '',
onWebViewCreated:
(WebViewController webViewController) async {
webViewController.evaluateJavascript(
"javascript:(function() { " +
"console.log('Test Test Test');" +
"})()");
},
),
推荐阅读
- reactjs - 为什么我在减速器中使用状态自定义类方法得到意外结果?
- python - 简化暴力破解程序并添加功能
- vb.net - Linq VB.net 到 PowerShell 5.1
- react-native - 与更新本机导航中的参数混淆
- javascript - 变量设置如何与等待一起工作?
- regex - 如何从 dart 中的 RGB 字符串中获取 R、G 和 B 的值?
- flutter - 检查文本是否包含字符串列表中的至少一个字符串 - Dart - Flutter
- django - Django 相关模型中的反向查询名称到底是什么?
- java - Gradle Docker 构建问题
- kubernetes - Kubernetes CRD 中的“存储”是什么意思?