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.
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
Asked: 2022-10-06 11:00:00 +0000
Seen: 7 times
Last updated: Sep 07 '22
When printing from WKWebView in Swift, the background is not taken into account.
Is it feasible to utilize a Toggle Button to switch among multiple classes exceeding 2?
What is the process for generating a dynamic subdomain/URL using vue.js?
How can I create a transition on click using CSS without the need for JavaScript?