98 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			Dart
		
	
	
	
			
		
		
	
	
			98 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			Dart
		
	
	
	
| 
 | |
| import 'dart:ffi';
 | |
| 
 | |
| import 'package:web_synchronization_tool/windows/socket_tool.dart';
 | |
| import 'package:web_synchronization_tool/windows/windowsJs.dart';
 | |
| import 'package:webview_windows/webview_windows.dart';
 | |
| 
 | |
| class SynchronizationWebTool{
 | |
| 
 | |
|   // 私有构造函数
 | |
|   SynchronizationWebTool._();
 | |
|   // 私有静态变量,保存类的唯一实例
 | |
|   static SynchronizationWebTool? _instance;
 | |
| 
 | |
|   // 公开的静态方法,返回类的唯一实例
 | |
|   static SynchronizationWebTool getInstance() {
 | |
|     _instance ??= SynchronizationWebTool._();
 | |
|     return _instance!;
 | |
|   }
 | |
| 
 | |
|   bool webSync = false;
 | |
| 
 | |
|   /// 主控
 | |
|   late WebviewController mainController;
 | |
|   /// 受控
 | |
|   List<WebviewController> _childControllers = [];
 | |
| 
 | |
|   /// 设置控制器时 注入js
 | |
|   setChildController(List<WebviewController> childControllers){
 | |
| 
 | |
|     for (var controller in childControllers) {
 | |
|       controller.addScriptToExecuteOnDocumentCreated(WindowsJs.clickEventJs);
 | |
|       // controller.addScriptToExecuteOnDocumentCreated(WindowsJs.onloadZoom(90));
 | |
|     }
 | |
| 
 | |
|     mainController = childControllers.first;
 | |
|     _childControllers = childControllers.sublist(1,10);
 | |
| 
 | |
|     // 滚动监听
 | |
|     // mainController.addScriptToExecuteOnDocumentCreated(WindowsJs.scrollEventJs);
 | |
| 
 | |
|     mainController.webMessage.listen((event) {
 | |
|       print('mainController listen --');
 | |
| 
 | |
|       if (event['scroll']  != null ) {
 | |
|         print(event);
 | |
|       }
 | |
| 
 | |
|       if (!webSync) return;
 | |
| 
 | |
|       if (event['click'] != null) {
 | |
|         Map click = event['click'];
 | |
|         int x = click['x'] as int;
 | |
|         int y = click['y'] as int;
 | |
| 
 | |
|         clickSynchronization(x, y);
 | |
|         SocketUtils.getInstance().sendClickMessage(click);
 | |
|       }
 | |
| 
 | |
|       if (event['scroll']  != null ) {
 | |
|         print(event);
 | |
|         int y = event['y'] as int;
 | |
|         scrollSynchronization(y);
 | |
|       }
 | |
| 
 | |
|     });
 | |
| 
 | |
|   }
 | |
| 
 | |
|   List<WebviewController> get childController{
 | |
|     return _childControllers;
 | |
|   }
 | |
| 
 | |
|   /// 点击同步
 | |
|   clickSynchronization(int x,int y){
 | |
|     for (var controller in childController) {
 | |
|       controller.executeScript(WindowsJs.clickJs(x, y));
 | |
|     }
 | |
|   }
 | |
| 
 | |
| 
 | |
|   /// 滚动同步
 | |
|   scrollSynchronization(int y){
 | |
|     for (var controller in childController) {
 | |
|       controller.executeScript(WindowsJs.scrollTo(y) );
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   /// 输入
 | |
|   input(List<int> values){
 | |
|     for (int i =0;i<childController.length;i++){
 | |
|       WebviewController controller = childController[i];
 | |
|       controller.executeScript(WindowsJs.inputJs(values[i]));
 | |
|     }
 | |
|   }
 | |
| 
 | |
| }
 |