| 
									
										
										
										
											2024-04-02 11:02:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import 'dart:ffi'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 14:36:36 +08:00
										 |  |  | import 'package:web_synchronization_tool/windows/socket_tool.dart'; | 
					
						
							| 
									
										
										
										
											2024-04-02 11:02:06 +08:00
										 |  |  | import 'package:web_synchronization_tool/windows/windowsJs.dart'; | 
					
						
							|  |  |  | import 'package:webview_windows/webview_windows.dart'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class SynchronizationWebTool{ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-02 20:29:52 +08:00
										 |  |  |   // 私有构造函数
 | 
					
						
							|  |  |  |   SynchronizationWebTool._(); | 
					
						
							|  |  |  |   // 私有静态变量,保存类的唯一实例
 | 
					
						
							|  |  |  |   static SynchronizationWebTool? _instance; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // 公开的静态方法,返回类的唯一实例
 | 
					
						
							|  |  |  |   static SynchronizationWebTool getInstance() { | 
					
						
							|  |  |  |     _instance ??= SynchronizationWebTool._(); | 
					
						
							|  |  |  |     return _instance!; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-05 18:10:38 +08:00
										 |  |  |   bool webSync = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /// 主控
 | 
					
						
							|  |  |  |   late WebviewController mainController; | 
					
						
							| 
									
										
										
										
											2024-04-02 11:02:06 +08:00
										 |  |  |   /// 受控
 | 
					
						
							| 
									
										
										
										
											2024-04-02 20:29:52 +08:00
										 |  |  |   List<WebviewController> _childControllers = []; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 12:54:07 +08:00
										 |  |  |   /// 设置控制器时 注入js
 | 
					
						
							| 
									
										
										
										
											2024-04-02 20:29:52 +08:00
										 |  |  |   setChildController(List<WebviewController> childControllers){ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (var controller in childControllers) { | 
					
						
							|  |  |  |       controller.addScriptToExecuteOnDocumentCreated(WindowsJs.clickEventJs); | 
					
						
							| 
									
										
										
										
											2024-04-03 10:37:41 +08:00
										 |  |  |       // controller.addScriptToExecuteOnDocumentCreated(WindowsJs.onloadZoom(90));
 | 
					
						
							| 
									
										
										
										
											2024-04-02 20:29:52 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-05 18:10:38 +08:00
										 |  |  |     mainController = childControllers.first; | 
					
						
							|  |  |  |     _childControllers = childControllers.sublist(1,10); | 
					
						
							| 
									
										
										
										
											2024-04-02 20:29:52 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 10:37:41 +08:00
										 |  |  |     // 滚动监听
 | 
					
						
							| 
									
										
										
										
											2024-04-03 12:54:07 +08:00
										 |  |  |     // mainController.addScriptToExecuteOnDocumentCreated(WindowsJs.scrollEventJs);
 | 
					
						
							| 
									
										
										
										
											2024-04-03 10:37:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-05 18:10:38 +08:00
										 |  |  |     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); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-02 20:29:52 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   List<WebviewController> get childController{ | 
					
						
							|  |  |  |     return _childControllers; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2024-04-02 11:02:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /// 点击同步
 | 
					
						
							| 
									
										
										
										
											2024-04-03 18:27:31 +08:00
										 |  |  |   clickSynchronization(int x,int y){ | 
					
						
							| 
									
										
										
										
											2024-04-02 11:02:06 +08:00
										 |  |  |     for (var controller in childController) { | 
					
						
							|  |  |  |       controller.executeScript(WindowsJs.clickJs(x, y)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 14:36:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 10:37:41 +08:00
										 |  |  |   /// 滚动同步
 | 
					
						
							|  |  |  |   scrollSynchronization(int y){ | 
					
						
							|  |  |  |     for (var controller in childController) { | 
					
						
							|  |  |  |       controller.executeScript(WindowsJs.scrollTo(y) ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-02 11:02:06 +08:00
										 |  |  |   /// 输入
 | 
					
						
							| 
									
										
										
										
											2024-04-03 14:36:36 +08:00
										 |  |  |   input(List<int> values){ | 
					
						
							|  |  |  |     for (int i =0;i<childController.length;i++){ | 
					
						
							|  |  |  |       WebviewController controller = childController[i]; | 
					
						
							|  |  |  |       controller.executeScript(WindowsJs.inputJs(values[i])); | 
					
						
							| 
									
										
										
										
											2024-04-02 11:02:06 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |