windows 初版

This commit is contained in:
WoNiu 2024-04-01 21:41:42 +08:00
parent 87b3157e14
commit bda8bb5570
7 changed files with 299 additions and 226 deletions

View File

@ -1,7 +1,7 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:web_synchronization_tool/main_page.dart'; import 'package:web_synchronization_tool/main_page.dart';
import 'package:web_synchronization_tool/windows/windows_main_page.dart'; import 'package:web_synchronization_tool/windows/windows_main_page.dart';
import 'package:webview_windows/webview_windows.dart';
void main() async { void main() async {
@ -22,8 +22,8 @@ class MyApp extends StatelessWidget {
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true, useMaterial3: true,
), ),
home: const MainPage(), // home: const MainPage(),
// home: const WindowsPage(), home: const WindowsPage(),
); );
} }
} }

View File

@ -1,145 +1,145 @@
//
import 'package:flutter/material.dart'; // import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart'; // import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:web_synchronization_tool/web_widget.dart'; // import 'package:web_synchronization_tool/web_widget.dart';
//
import 'JavaScriptString.dart'; // import 'JavaScriptString.dart';
//
class MainPage extends StatefulWidget { // class MainPage extends StatefulWidget {
const MainPage({super.key}); // const MainPage({super.key});
//
@override // @override
State<MainPage> createState() => _MainPageState(); // State<MainPage> createState() => _MainPageState();
} // }
//
class _MainPageState extends State<MainPage> { // class _MainPageState extends State<MainPage> {
//
late InAppWebViewController mainController; // late InAppWebViewController mainController;
late InAppWebViewController controller; // late InAppWebViewController controller;
//
bool asyncState = false; // bool asyncState = false;
//
@override // @override
void initState() { // void initState() {
super.initState(); // super.initState();
//
} // }
//
@override // @override
void dispose() { // void dispose() {
super.dispose(); // super.dispose();
//
WebStorageManager.instance().deleteAllData(); // WebStorageManager.instance().deleteAllData();
} // }
//
/// // ///
addTouchendEventJS(){ // addTouchendEventJS(){
//
final clickJsUS = UserScript(groupName: 'touchend',source: JavaScriptString.touchendEventJSString, injectionTime: UserScriptInjectionTime.AT_DOCUMENT_START); // final clickJsUS = UserScript(groupName: 'touchend',source: JavaScriptString.touchendEventJSString, injectionTime: UserScriptInjectionTime.AT_DOCUMENT_START);
mainController.addUserScript(userScript: clickJsUS); // mainController.addUserScript(userScript: clickJsUS);
//
mainController.addJavaScriptHandler(handlerName: 'touchend', callback: (args){ // mainController.addJavaScriptHandler(handlerName: 'touchend', callback: (args){
if (asyncState){ // if (asyncState){
int x = double.parse(args.first.toString()).toInt(); // int x = double.parse(args.first.toString()).toInt();
int y = double.parse(args.last.toString()).toInt(); // int y = double.parse(args.last.toString()).toInt();
controller.evaluateJavascript(source: JavaScriptString.clickJSString(x, y) ); // controller.evaluateJavascript(source: JavaScriptString.clickJSString(x, y) );
// controller.evaluateJavascript(source: JavaScriptString.touchendJsString(x, y) ); // // controller.evaluateJavascript(source: JavaScriptString.touchendJsString(x, y) );
// controller.evaluateJavascript(source: JavaScriptString.getClassTouchendJsString(x, y) ); // // controller.evaluateJavascript(source: JavaScriptString.getClassTouchendJsString(x, y) );
} // }
}); // });
//
} // }
//
@override // @override
Widget build(BuildContext context) { // Widget build(BuildContext context) {
return Scaffold( // return Scaffold(
body: Column( // body: Column(
crossAxisAlignment: CrossAxisAlignment.stretch, // crossAxisAlignment: CrossAxisAlignment.stretch,
children: [ // children: [
Container( // Container(
height: 50, // height: 50,
color: Colors.white, // color: Colors.white,
padding: const EdgeInsets.symmetric(horizontal: 50), // padding: const EdgeInsets.symmetric(horizontal: 50),
child: Row( // child: Row(
children: [ // children: [
TextButton( // TextButton(
onPressed: () { // onPressed: () {
controller.evaluateJavascript(source: JavaScriptString.clickJSString(50, 100) ); // controller.evaluateJavascript(source: JavaScriptString.clickJSString(50, 100) );
}, // },
child: const Text('模拟点击测试')), // child: const Text('模拟点击测试')),
TextButton( // TextButton(
onPressed: () { // onPressed: () {
mainController.evaluateJavascript(source: JavaScriptString.inputJsString(45) ); // mainController.evaluateJavascript(source: JavaScriptString.inputJsString(45) );
controller.evaluateJavascript(source: JavaScriptString.inputJsString(45) ); // controller.evaluateJavascript(source: JavaScriptString.inputJsString(45) );
}, // },
child: const Text('模拟输入测试')), // child: const Text('模拟输入测试')),
Row( // Row(
children: [ // children: [
const Text('同步'), // const Text('同步'),
Switch(value: asyncState, onChanged: (value){ // Switch(value: asyncState, onChanged: (value){
setState(() { // setState(() {
asyncState = value; // asyncState = value;
}); // });
}) // })
], // ],
) // )
], // ],
), // ),
), // ),
Expanded(child: pageViewWidget()), // Expanded(child: pageViewWidget()),
], // ],
), // ),
); // );
} // }
//
Widget pageViewWidget() { // Widget pageViewWidget() {
return Row( // return Row(
children: <Widget>[ // children: <Widget>[
Expanded( // Expanded(
child: Container( // child: Container(
color: Colors.yellow, // color: Colors.yellow,
child: KeepAlivePage( // child: KeepAlivePage(
child: WebWidget(controlerCallBack: (_mainController) { // child: WebWidget(controlerCallBack: (_mainController) {
mainController = _mainController; // mainController = _mainController;
//
addTouchendEventJS(); // addTouchendEventJS();
//
}), // }),
), // ),
), // ),
), // ),
Expanded( // Expanded(
child: Container( // child: Container(
color: Colors.blue, // color: Colors.blue,
child: KeepAlivePage( // child: KeepAlivePage(
child: WebWidget(controlerCallBack: (_controller) { // child: WebWidget(controlerCallBack: (_controller) {
controller = _controller; // controller = _controller;
}), // }),
), // ),
), // ),
) // )
], // ],
); // );
} // }
//
} // }
//
class KeepAlivePage extends StatefulWidget { // class KeepAlivePage extends StatefulWidget {
final Widget child; // final Widget child;
//
KeepAlivePage({super.key , required this.child}); // KeepAlivePage({super.key , required this.child});
//
@override // @override
_KeepAlivePageState createState() => _KeepAlivePageState(); // _KeepAlivePageState createState() => _KeepAlivePageState();
} // }
//
class _KeepAlivePageState extends State<KeepAlivePage> with AutomaticKeepAliveClientMixin { // class _KeepAlivePageState extends State<KeepAlivePage> with AutomaticKeepAliveClientMixin {
@override // @override
bool get wantKeepAlive => true; // bool get wantKeepAlive => true;
//
@override // @override
Widget build(BuildContext context) { // Widget build(BuildContext context) {
super.build(context); // super.build(context);
return widget.child; // return widget.child;
} // }
} // }

View File

@ -1,42 +1,42 @@
import 'package:flutter/material.dart'; // import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart'; // import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:web_synchronization_tool/JavaScriptString.dart'; // import 'package:web_synchronization_tool/JavaScriptString.dart';
//
class WebWidget extends StatefulWidget { // class WebWidget extends StatefulWidget {
WebWidget({super.key, required this.controlerCallBack}); // WebWidget({super.key, required this.controlerCallBack});
//
final Function(InAppWebViewController) controlerCallBack; // final Function(InAppWebViewController) controlerCallBack;
//
@override // @override
State<WebWidget> createState() => _WebWidgetState(); // State<WebWidget> createState() => _WebWidgetState();
} // }
//
class _WebWidgetState extends State<WebWidget> { // class _WebWidgetState extends State<WebWidget> {
@override // @override
void initState() { // void initState() {
super.initState(); // super.initState();
} // }
//
@override // @override
Widget build(BuildContext context) { // Widget build(BuildContext context) {
return InAppWebView( // return InAppWebView(
initialUrlRequest: // initialUrlRequest:
URLRequest(url: WebUri('http://www.df6831.com')), // URLRequest(url: WebUri('http://www.df6831.com/mobile')),//
initialSettings: InAppWebViewSettings( // initialSettings: InAppWebViewSettings(
initialScale: 180, // // initialScale: 180,
loadWithOverviewMode: false, // loadWithOverviewMode: false,
useWideViewPort: false, // useWideViewPort: false,
preferredContentMode: UserPreferredContentMode.MOBILE, // // preferredContentMode: UserPreferredContentMode.MOBILE,
cacheEnabled: false, // // // cacheEnabled: false, //
clearSessionCache: true,// // // clearSessionCache: true,//
databaseEnabled:false, // // // databaseEnabled:false, //
domStorageEnabled: false,// dom // // domStorageEnabled: false,// dom
incognito: true, // // incognito: true, //
sharedCookiesEnabled: false, // Cookie // sharedCookiesEnabled: false, // Cookie
), // ),
onWebViewCreated: (_controller) { // onWebViewCreated: (_controller) {
widget.controlerCallBack(_controller); // widget.controlerCallBack(_controller);
}, // },
); // );
} // }
} // }

View File

@ -0,0 +1,56 @@
class WindowsJs {
///
static String clickEventJs = '''
document.addEventListener('click', function(event) {
var x = event.clientX;
var y = event.clientY;
var value = {"x":x,"y":y};
window.chrome.webview.postMessage(value);
// 'btn'
if (event.target.classList.contains('btn')) {
//
var buttonText = event.target.innerText;
var btn = {"x":"成功获取到btn","y":buttonText};
window.chrome.webview.postMessage(btn);
//
event.preventDefault();
//
window.location.href = "http://www.df6831.com/game/";
}
});''';
///
static String clickJs(int x, int y) {
return 'document.elementFromPoint($x, $y).click();';
}
///
static String inputJsString(int value) {
return '''
var inputEvent = new Event('input', {
bubbles: true,
cancelable: true,
});
var inputElement = document.querySelector(".input");
inputElement.value = "$value";
inputElement.dispatchEvent(inputEvent);
''';
}
static String zoom(int zoom){
assert(zoom >= 1 && zoom <= 100, 'zoom 1 到 100');
return '''
// document.body.style.zoom = "$zoom%";
document.body.style.transformOrigin = 'top left';
document.body.style.transform = 'scale(${zoom / 100})';
''';
}
}

View File

@ -1,4 +1,5 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:web_synchronization_tool/windows/windowsJs.dart';
import 'windows_web_page.dart'; import 'windows_web_page.dart';
import 'package:webview_windows/webview_windows.dart'; import 'package:webview_windows/webview_windows.dart';
@ -25,6 +26,7 @@ class _WindowsPageState extends State<WindowsPage> {
} }
Future controllerInit() async { Future controllerInit() async {
await mainController.initialize(); await mainController.initialize();
await controller.initialize(); await controller.initialize();
@ -33,7 +35,6 @@ class _WindowsPageState extends State<WindowsPage> {
controller.clearCache(); controller.clearCache();
controller.clearCookies(); controller.clearCookies();
mainController.loadUrl('http://www.df6831.com/'); mainController.loadUrl('http://www.df6831.com/');
controller.loadUrl('http://www.df6831.com/'); controller.loadUrl('http://www.df6831.com/');
@ -42,22 +43,27 @@ class _WindowsPageState extends State<WindowsPage> {
setState(() { setState(() {
initDone = true; initDone = true;
}); });
// controller.setUserAgent('Mobile');
///
mainController.webMessage.listen((event) {
print('mainController listen -- $event');
controller.executeScript(WindowsJs.clickJs(event['x'], event['y']));
});
///
controller.webMessage.listen((event) {
// print('controller listen -- $event');
});
} }
/// ///
addClickEventJS() { addClickEventJS() {
mainController.addScriptToExecuteOnDocumentCreated(WindowsJs.clickEventJs);
controller.executeScript('document.documentElement.innerHTML').then((value){ controller.addScriptToExecuteOnDocumentCreated(WindowsJs.clickEventJs);
print(value);
});
mainController.addScriptToExecuteOnDocumentCreated(
JavaScriptString.clickEventJSString).then((value){
print(value);
});
} }
@override @override
@ -75,33 +81,39 @@ class _WindowsPageState extends State<WindowsPage> {
children: [ children: [
TextButton( TextButton(
onPressed: () { onPressed: () {
controller.executeScript( mainController.executeScript(WindowsJs.clickJs(200, 900));
JavaScriptString.clickJSString(600, 280)).then((value){ // controller.executeScript(WindowsJs.clickJs(600, 100));
print(value);
});
}, },
child: const Text('模拟点击测试')), child: const Text('模拟点击测试')),
TextButton( TextButton(
onPressed: () { onPressed: () {
mainController mainController.executeScript(WindowsJs.inputJsString(45));
.executeScript(JavaScriptString.inputJsString(45)); controller.executeScript(WindowsJs.inputJsString(45));
controller
.executeScript(JavaScriptString.inputJsString(45));
}, },
child: const Text('模拟输入测试')), child: const Text('模拟输入测试')),
TextButton( TextButton(
onPressed: () { onPressed: () {
mainController.loadUrl('http://www.df6831.com/game/'); mainController.loadUrl('http://www.df6831.com/');
// controller.loadUrl('http://www.df6831.com/game/');
}, },
child: const Text('跳转')), child: const Text('跳转首页')),
const SizedBox( TextButton(
width: 50, onPressed: () {
child: TextField( mainController.executeScript(WindowsJs.zoom(70));
keyboardType: TextInputType.number, controller.executeScript(WindowsJs.zoom(40));
decoration: InputDecoration(prefixText: '网页数量'), },
), child: const Text('缩放')),
) TextButton(
onPressed: () {
mainController.openDevTools();
},
child: const Text('开发者')),
TextButton(
onPressed: () {
mainController.clearCookies();
mainController.clearCache();
},
child: const Text('清除缓存')),
], ],
), ),
), ),
@ -115,12 +127,16 @@ class _WindowsPageState extends State<WindowsPage> {
Widget pageViewWidget() { Widget pageViewWidget() {
return Row( return Row(
children: [ children: [
Expanded( SizedBox(
width: 1000,
height: 1000,
child: WindowsWebWidget( child: WindowsWebWidget(
controller: mainController, controller: mainController,
), ),
), ),
Expanded( SizedBox(
width: 500,
height:400,
child: WindowsWebWidget( child: WindowsWebWidget(
controller: controller, controller: controller,
), ),

View File

@ -21,7 +21,7 @@ class _WindowsWebWidgetState extends State<WindowsWebWidget> {
} }
Widget inWindowsWebView(){ Widget inWindowsWebView(){
return Webview(widget.controller,); return Webview(widget.controller);
} }
} }

View File

@ -12,8 +12,9 @@ dependencies:
sdk: flutter sdk: flutter
cupertino_icons: ^1.0.2 cupertino_icons: ^1.0.2
flutter_inappwebview: ^6.0.0 webview_windows:
webview_windows: ^0.4.0 path: ../flutter-webview-windows-main
window_manager:
dev_dependencies: dev_dependencies:
flutter_test: flutter_test: