commit 9ac74625236a0df15c05689c588164415eb93c71 Author: WoNiu Date: Sat Mar 30 10:58:39 2024 +0800 android 初版 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c9dd01d --- /dev/null +++ b/.gitignore @@ -0,0 +1,49 @@ +# Miscellaneous +*.class +*.log +*.pyc +*.swp +.DS_Store +.atom/ +.buildlog/ +.history +.svn/ +migrate_working_dir/ + +# IntelliJ related +*.iml +*.ipr +*.iws +.idea/ + +# The .vscode folder contains launch configuration and tasks you configure in +# VS Code which you may wish to be included in version control, so this line +# is commented out by default. +#.vscode/ + +# Flutter/Dart/Pub related +**/doc/api/ +**/ios/Flutter/.last_build_id +.dart_tool/ +.flutter-plugins +.flutter-plugins-dependencies +.pub-cache/ +.pub/ +/build/ + +# Symbolication related +app.*.symbols + +# Obfuscation related +app.*.map.json + +# Android Studio will place build artifacts here +/android/app/debug +/android/app/profile +/android/app/release +windows/ +android/ +test/widget_test.dart +pubspec.lock +.metadata +analysis_options.yaml diff --git a/README.md b/README.md new file mode 100644 index 0000000..85f98be --- /dev/null +++ b/README.md @@ -0,0 +1,16 @@ +# web_synchronization_tool + +A new Flutter project. + +## Getting Started + +This project is a starting point for a Flutter application. + +A few resources to get you started if this is your first Flutter project: + +- [Lab: Write your first Flutter app](https://docs.flutter.dev/get-started/codelab) +- [Cookbook: Useful Flutter samples](https://docs.flutter.dev/cookbook) + +For help getting started with Flutter development, view the +[online documentation](https://docs.flutter.dev/), which offers tutorials, +samples, guidance on mobile development, and a full API reference. diff --git a/lib/JavaScriptString.dart b/lib/JavaScriptString.dart new file mode 100644 index 0000000..864c9fc --- /dev/null +++ b/lib/JavaScriptString.dart @@ -0,0 +1,35 @@ +class JavaScriptString { + /// 点击监听 + static String clickEventkJSString = + '''document.addEventListener('click', function(event) { + var x = event.clientX; + var y = event.clientY; + + console.log('点击坐标:x=' + x + ', y=' + y); + window.flutter_inappwebview.callHandler('Click', x, y); + +});'''; + + /// 模拟点击 + static String clickJSString(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(".bet-money"); + +inputElement.value = "$value"; + +inputElement.dispatchEvent(inputEvent); + + '''; + } +} diff --git a/lib/main.dart b/lib/main.dart new file mode 100644 index 0000000..ee38e49 --- /dev/null +++ b/lib/main.dart @@ -0,0 +1,23 @@ +import 'package:flutter/material.dart'; +import 'package:web_synchronization_tool/main_page.dart'; + +void main() { + runApp(const MyApp()); +} + +class MyApp extends StatelessWidget { + const MyApp({super.key}); + + // This widget is the root of your application. + @override + Widget build(BuildContext context) { + return MaterialApp( + title: 'Flutter Demo', + theme: ThemeData( + colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), + useMaterial3: true, + ), + home: const MainPage(), + ); + } +} diff --git a/lib/main_page.dart b/lib/main_page.dart new file mode 100644 index 0000000..ba3f054 --- /dev/null +++ b/lib/main_page.dart @@ -0,0 +1,116 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_inappwebview/flutter_inappwebview.dart'; +import 'package:web_synchronization_tool/web_widget.dart'; + +import 'JavaScriptString.dart'; + +class MainPage extends StatefulWidget { + const MainPage({super.key}); + + @override + State createState() => _MainPageState(); +} + +class _MainPageState extends State { + + late InAppWebViewController mainController; + late InAppWebViewController controller; + + @override + void initState() { + super.initState(); + + + } + + /// 注入点击监听 + addClickEventJS(){ + + final clickJsUS = UserScript(groupName: 'click',source: JavaScriptString.clickEventkJSString, injectionTime: UserScriptInjectionTime.AT_DOCUMENT_START); + + mainController.addUserScript(userScript: clickJsUS); + + mainController.addJavaScriptHandler(handlerName: 'Click', callback: (args){ + controller.evaluateJavascript(source: JavaScriptString.clickJSString(args.first, args.last) ); + }); + + } + + @override + Widget build(BuildContext context) { + return Scaffold( + body: Column( + children: [ + Container( + height: 50, + color: Colors.white, + padding: const EdgeInsets.symmetric(horizontal: 50), + child: Row( + children: [ + TextButton( + onPressed: () { + controller.evaluateJavascript(source: JavaScriptString.clickJSString(600, 280) ); + }, + child: const Text('模拟点击测试')), + TextButton( + onPressed: () { + mainController.evaluateJavascript(source: JavaScriptString.inputJsString(45) ); + controller.evaluateJavascript(source: JavaScriptString.inputJsString(45) ); + }, + child: const Text('模拟输入测试')), + const SizedBox( + width: 50, + child: TextField( + keyboardType: TextInputType.number, + decoration: InputDecoration(prefixText: '网页数量'), + ), + ) + ], + ), + ), + Expanded(child: pageViewWidget()), + ], + ), + ); + } + + Widget pageViewWidget() { + return PageView( + children: [ + KeepAlivePage( + child: WebWidget(controlerCallBack: (_mainController) { + mainController = _mainController; + + addClickEventJS(); + }), + ), + KeepAlivePage( + child: WebWidget(controlerCallBack: (_controller) { + controller = _controller; + }), + ) + ], + ); + } + +} + +class KeepAlivePage extends StatefulWidget { + final Widget child; + + KeepAlivePage({super.key , required this.child}); + + @override + _KeepAlivePageState createState() => _KeepAlivePageState(); +} + +class _KeepAlivePageState extends State with AutomaticKeepAliveClientMixin { + @override + bool get wantKeepAlive => true; + + @override + Widget build(BuildContext context) { + super.build(context); + return widget.child; + } +} diff --git a/lib/web_widget.dart b/lib/web_widget.dart new file mode 100644 index 0000000..40fecc3 --- /dev/null +++ b/lib/web_widget.dart @@ -0,0 +1,31 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_inappwebview/flutter_inappwebview.dart'; +import 'package:web_synchronization_tool/JavaScriptString.dart'; + +class WebWidget extends StatefulWidget { + WebWidget({super.key, required this.controlerCallBack}); + + final Function(InAppWebViewController) controlerCallBack; + + @override + State createState() => _WebWidgetState(); +} + +class _WebWidgetState extends State { + @override + void initState() { + super.initState(); + } + + @override + Widget build(BuildContext context) { + return InAppWebView( + initialUrlRequest: + URLRequest(url: WebUri('http://www.df6831.com/game/')), + initialSettings: InAppWebViewSettings(initialScale: 200,loadWithOverviewMode: false,useWideViewPort: false), + onWebViewCreated: (_controller) { + widget.controlerCallBack(_controller); + }, + ); + } +} diff --git a/pubspec.yaml b/pubspec.yaml new file mode 100644 index 0000000..2008e4d --- /dev/null +++ b/pubspec.yaml @@ -0,0 +1,57 @@ +name: web_synchronization_tool +description: "A new Flutter project." +publish_to: 'none' # Remove this line if you wish to publish to pub.dev + +version: 1.0.0+1 + +environment: + sdk: '>=3.2.3 <4.0.0' + +dependencies: + flutter: + sdk: flutter + + cupertino_icons: ^1.0.2 + flutter_inappwebview: ^6.0.0 + webview_dart: ^1.0.1 + +dev_dependencies: + flutter_test: + sdk: flutter + + flutter_lints: ^2.0.0 + +flutter: + + uses-material-design: true + + # To add assets to your application, add an assets section, like this: + # assets: + # - images/a_dot_burr.jpeg + # - images/a_dot_ham.jpeg + + # An image asset can refer to one or more resolution-specific "variants", see + # https://flutter.dev/assets-and-images/#resolution-aware + + # For details regarding adding assets from package dependencies, see + # https://flutter.dev/assets-and-images/#from-packages + + # To add custom fonts to your application, add a fonts section here, + # in this "flutter" section. Each entry in this list should have a + # "family" key with the font family name, and a "fonts" key with a + # list giving the asset and other descriptors for the font. For + # example: + # fonts: + # - family: Schyler + # fonts: + # - asset: fonts/Schyler-Regular.ttf + # - asset: fonts/Schyler-Italic.ttf + # style: italic + # - family: Trajan Pro + # fonts: + # - asset: fonts/TrajanPro.ttf + # - asset: fonts/TrajanPro_Bold.ttf + # weight: 700 + # + # For details regarding fonts from package dependencies, + # see https://flutter.dev/custom-fonts/#from-packages