基础库
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 

90 linhas
4.4 KiB

  1. import 'dart:convert';
  2. import 'package:flutter/material.dart';
  3. import 'package:webview_flutter/webview_flutter.dart';
  4. import 'package:zhiying_comm/zhiying_comm.dart';
  5. class TaobaoImageLoader extends StatefulWidget {
  6. final String url;
  7. String script;
  8. final ValueChanged<List<String>> onImagesLoad;
  9. TaobaoImageLoader(this.url, {Key key, this.script, this.onImagesLoad})
  10. : super(key: key) {
  11. this.script = script;
  12. if (EmptyUtil.isEmpty(this.script)) {
  13. this.script =
  14. 'CnZhciB6cj17fTsKZnVuY3Rpb24gZ2V0aW4oc3RyMSxzdHIyLHN0cjMpewogICAgc3RyMT1zdHIxKycnO3N0cjI9c3RyMisnJztzdHIzPXN0cjMrJyc7CiAgICB2YXIgdG1wPXN0cjEuc3BsaXQoc3RyMik7CiAgICBpZih0bXBbMV09PW51bGwpdG1wWzFdPScnOwogICAgdG1wPXRtcFsxXS5zcGxpdChzdHIzKTsKICAgIHJldHVybiB0bXBbMF07Cn0KZnVuY3Rpb24gY2hlY2soKXsKICAgIC8qenIudG1wPWRvY3VtZW50LmdldEVsZW1lbnRCeUlkKCdkZXNjcmlwdGlvbicpLmlubmVySFRNTDsqLwogICAgenIudG1wPWRvY3VtZW50LmdldEVsZW1lbnRCeUlkKCdKX2Z1bGxDb250ZW50JykuaW5uZXJIVE1MOwogICAgaWYoenIudG1wPT1udWxsfHx6ci50bXA9PScnfHx6ci50bXAuaW5kZXhPZigiLmpwZyIpPT0tMSl7CiAgICAgICAgd2luZG93LnNldFRpbWVvdXQoZnVuY3Rpb24oKXtjaGVjaygpO30sMzAwKTsKICAgICAgICByZXR1cm47CiAgICB9CiAgICB6ci5hbGw9enIudG1wLnNwbGl0KCdzcmM9Jyk7CiAgICB6ci5yZXR1cm5fZGF0YT1bXTsKICAgIHpyLm49LTE7CiAgICBmb3IodmFyIGkgPSAwLGxlbiA9IHpyLmFsbC5sZW5ndGg7IGkgPCBsZW47IGkrKyl7CiAgICAgICAgaWYoaT09MCljb250aW51ZTsKICAgICAgICBpZih6ci5hbGwubGVuZ3RoLTE9PWkpY29udGludWU7CiAgICAgICAgenIudG1wdmFsPXpyLmFsbFtpXTsKICAgICAgICB6ci50bXB2YWw9Z2V0aW4oenIudG1wdmFsLCciJywnIicpOwogICAgICAgIGlmKHpyLnRtcHZhbC5pbmRleE9mKCJiYXNlNjQiKSE9LTF8fHpyLnRtcHZhbC5pbmRleE9mKCJocmVmPSIpIT0tMSljb250aW51ZTsKICAgICAgICBpZih6ci50bXB2YWwuaW5kZXhPZigiaHR0cCIpPT0tMSl6ci50bXB2YWw9Imh0dHBzOiIrenIudG1wdmFsOwogICAgICAgIHpyLm4rKzsKICAgICAgICB6ci5yZXR1cm5fZGF0YVt6ci5uXT16ci50bXB2YWw7CiAgICB9CiAgICB6ci5yZXR1cm5fZGF0YT1KU09OLnN0cmluZ2lmeSh6ci5yZXR1cm5fZGF0YSk7CiAgICAvLyBkb2N1bWVudC5ib2R5LmlubmVySFRNTD16ci5yZXR1cm5fZGF0YTsKICAgIExvYWRlci5wb3N0TWVzc2FnZSh6ci5yZXR1cm5fZGF0YSk7CiAgICAvLyBqaCh6ci5yZXR1cm5fZGF0YSwiIik7Cn0KdHJ5e3dpbmRvdy5zZXRUaW1lb3V0KGZ1bmN0aW9uKCl7Y2hlY2soKTt9LDApO30KY2F0Y2goZXJyKXtkb2N1bWVudC5ib2R5LmlubmVySFRNTD1KU09OLnN0cmluZ2lmeShlcnIpO30KCg==';
  15. }
  16. }
  17. @override
  18. _TaobaoImageLoaderState createState() => _TaobaoImageLoaderState();
  19. }
  20. class _TaobaoImageLoaderState extends State<TaobaoImageLoader> {
  21. WebViewController _webViewController;
  22. List<String> _images = [];
  23. @override
  24. Widget build(BuildContext context) {
  25. return Scaffold(
  26. appBar: AppBar(
  27. title: const Text('抓取淘宝详情图'),
  28. ),
  29. // We're using a Builder here so we have a context that is below the Scaffold
  30. // to allow calling Scaffold.of(context) so we can show a snackbar.
  31. body: Builder(builder: (BuildContext context) {
  32. return WebView(
  33. initialUrl: widget.url,
  34. javascriptMode: JavascriptMode.unrestricted,
  35. onWebViewCreated: (WebViewController webViewController) {
  36. _webViewController = webViewController;
  37. },
  38. // TODO(iskakaushik): Remove this when collection literals makes it to stable.
  39. // ignore: prefer_collection_literals
  40. javascriptChannels: <JavascriptChannel>[
  41. _imageLoaderJavascriptChannel(context),
  42. ].toSet(),
  43. navigationDelegate: (NavigationRequest request) {
  44. print('allowing navigation to $request');
  45. return NavigationDecision.navigate;
  46. },
  47. onPageStarted: (String url) {
  48. print('Page started loading: $url');
  49. },
  50. onPageFinished: (String url) {
  51. _loadImages();
  52. },
  53. gestureNavigationEnabled: true,
  54. );
  55. }),
  56. );
  57. }
  58. void _loadImages() {
  59. String js = EncodeUtil.decodeBase64(widget.script);
  60. _webViewController.evaluateJavascript(js).then((result) {});
  61. }
  62. JavascriptChannel _imageLoaderJavascriptChannel(BuildContext context) {
  63. return JavascriptChannel(
  64. name: 'Loader',
  65. onMessageReceived: (JavascriptMessage message) {
  66. if (widget.onImagesLoad != null) {
  67. List list = jsonDecode(message.message);
  68. if (list.length != _images.length) {
  69. Future.delayed(Duration(milliseconds: 1000), () {
  70. _loadImages();
  71. });
  72. _images = list.map((item) => item.toString()).toList();
  73. widget.onImagesLoad(_images);
  74. } else {
  75. print('图片抓取结束');
  76. }
  77. }
  78. });
  79. }
  80. }