Ask Your Question
4

How can I delete the header of a website on Flutter WebView (latest version)?

asked 2022-10-06 11:00:00 +0000

pufferfish gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-09-07 21:00:00 +0000

woof gravatar image

You can use the following code to remove the header of a website on Flutter WebView:

class MyWebView extends StatelessWidget {
  final String url;

  MyWebView({required this.url});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: WebView(
        initialUrl: url,
        javascriptMode: JavascriptMode.unrestricted,
        onPageFinished: (String url) {
          removeHeader(context);
        },
      ),
    );
  }

  void removeHeader(BuildContext context) async {
    String removeHeaderJS = '''
      javascript:(function() {
        var header = document.getElementsByTagName('header')[0];
        header.style.display = 'none';
      })();
    ''';

    await Future.delayed(Duration(milliseconds: 500));
    await (await WebView.platformViewsRegistry.getPlatformViewsController((context.findRenderObject() as RenderBox).parentData!.size!.toRect())).evaluateJavascript(removeHeaderJS);
  }
}

In this code, we are using the Javascript getElementsByTagName method to get the first header element on the page and then setting its display property to none to hide it. We are calling the removeHeader method when the page has finished loading using the onPageFinished callback of the WebView. Finally, we are using the evaluateJavascript method to execute the removeHeaderJS code on the WebView.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-10-06 11:00:00 +0000

Seen: 13 times

Last updated: Sep 07 '22