Внедрение кода для скрытия частей сайта в WebView не работает - PullRequest
0 голосов
/ 06 января 2020

Привет, я сделал приложение для просмотра веб-страниц Flutter, но уведомление о cookies ( первое изображение ) не скрывается, когда я вводил этот код:

(await _controller.future).evaluateJavascript("document.getElementById('st_notification_1')[0].style.display='none';");

enter image description here

Код одной из моих страниц, которую я должен ввести два кода:

import 'dart:async';
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';

class HomePage extends StatefulWidget {
  HomePage({Key key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {

  WebViewController _myController;
      final Completer<WebViewController> _controller =
      Completer<WebViewController>();
  @override
  Widget build(BuildContext context) {
    return SafeArea(
            child: Scaffold(
                  body: WebView(
                  initialUrl: 'https://syncshop.online/en/',
                  javascriptMode: JavascriptMode.unrestricted,
                  onWebViewCreated: (controller) {
                  _controller.complete(controller);
                },
          onPageFinished: (controller) async {
          (await _controller.future).evaluateJavascript("document.getElementsByClassName('footer-container')[0].style.display='none';"); //THIS ONE WORKS 
          (await _controller.future).evaluateJavascript("document.getElementById('st_notification_1')[0].style.display='none';");
          (await _controller.future).evaluateJavascript("document.getElementById('sidebar_box')[0].style.display='none';");
          },
          ),
    floatingActionButton: FutureBuilder<WebViewController>(
        future: _controller.future,
        builder: (BuildContext context, AsyncSnapshot<WebViewController> controller) {
          if (controller.hasData) {
            return FloatingActionButton(
            onPressed: () {
              controller.data.reload();
            },
            child: Icon(Icons.refresh),
          );
          }
          return Container();
        }
        ),
          ),
      );
    }
}

1 Ответ

1 голос
/ 11 января 2020

getElementById() возвращает элемент, поэтому нет необходимости использовать [0]

...