KDC
KDC

Reputation: 611

Code injection to hide Website parts in WebView not working

Hi I made a Flutter webview app but the notification of cookies (first image) does not hide when I injected this code:

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

enter image description here

The code of one of my pages which I should Inject the two code is this:

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();
        }
        ),
          ),
      );
    }
}

Upvotes: 1

Views: 892

Answers (1)

Wenhui  Luo
Wenhui Luo

Reputation: 296

getElementById() returns the element, so no need to use [0]

Upvotes: 1

Related Questions