Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inject a simple code in Flutter WebView to hide a part of the website (footer)?

I'm new to Flutter, and currently, I'm making a very simple app, which is just a WebView. My question is how can I insert this code into my Flutter WebView?

footer#footer, div#st_notification_1, #sidebar_box {
    display: none!important;
}

As of the moment, I'm trying to use WebView plugin by the Flutter Team on one of my application tabs. The website I'm trying to load and hiding the footer after is:

Syncshop

below is my code for that tab Webview that I'm trying to hide the footer

UPDATED: FIXED IT. The code below is working for me Note: I also re-inspect the website and changed the getElementsById to getElementsByClassName corresponding to the class name of the footer on the website above.

Note2: There are plenty of WebView apps in Flutter packages, I'm using the Flutter Webview by the Flutter team.

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

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

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

class _ProfileAccountState extends State<ProfileAccount> {
    WebViewController _myController;
    final Completer<WebViewController> _controller =
     Completer<WebViewController>();
  @override
  Widget build(BuildContext context) {
    return SafeArea(
          child: Scaffold(
            body: WebView(
              initialUrl: 'https://syncshop.online/en/login',
              javascriptMode: JavascriptMode.unrestricted,
              onWebViewCreated: (controller) {
                _myController = controller;
              },
          onPageFinished: (initialUrl) {
            _myController.evaluateJavascript("document.getElementsByClassName('footer-container')[0].style.display='none';");
          },
        )
      ),
    );
  }
}
like image 573
KDC Avatar asked Dec 10 '19 06:12

KDC


People also ask

What is WebView in flutter?

WebView is a view that display web pages inside your application. So in this article, We will learn about How to Add WebView In Flutter? What is WebView? WebView is nothing but an Embeddable browser that a native application can use to display web content. How to Add WebView In Flutter? Use a plugin Webview_flutter.

How do I display a webpage in my flutter application?

After interacting with the Open Webpage button, you will see the DigitalOcean website displayed in your Flutter application. In this tutorial, you used the WebView package to display a webpage in your Flutter application. If you’d like to learn more about Flutter, check out our Flutter topic page for exercises and programming projects.

How to display DigitalOcean website in flutter application?

After interacting with the Open Webpage button, you will see the DigitalOcean website displayed in your Flutter application. In this tutorial, you used the WebView package to display a webpage in your Flutter application.

What is flutter and how to learn it?

Flutter, as well as Dart, are directly covered by GOOGLE. Flutter also allows creating web and desktop applications, but our focus is currently on mobile development. In this first part of our Flutter series of tutorials, we will focus on creating a WebView for any website available on the internet and running (embedding) it in our own application.


1 Answers

You can try

flutterWebviewPlugin.evalJavascript('alert("Hello World")')

Remember that the evalJavascript() expects the JS not HTML, So you can't use like

flutterWebviewPlugin.evalJavascript('<script language="JavaScript" type="text/javascript">alert("Hello World")</script>')

Here is the complete example for your reference,

import 'package:flutter/material.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';

class JSInWebView extends StatefulWidget {
  @override
  JSInWebViewState createState() {
    return new JSInWebViewState();
  }
}

class JSInWebViewState extends State<JSInWebView> {
  final flutterWebviewPlugin = new FlutterWebviewPlugin();
  // alternatively you can define variable as var js = "YOUR_SCRIPT"; and use it inside evalJavascript

  @override
  void initState(){
    super.initState();
    flutterWebviewPlugin.evalJavascript("alert('Hi, I just executed')");
  }

  @override
  void dispose() {
    flutterWebviewPlugin.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return WebviewScaffold(
      url: 'https://google.com',
      hidden: true,
      appBar: AppBar(title: Text("Elite")),
    );
  }
}
like image 119
Kiran Maniya Avatar answered Oct 16 '22 09:10

Kiran Maniya