在flutter中删除webview中的html元素

oiopk7p5  于 2023-03-19  发布在  Flutter
关注(0)|答案(4)|浏览(202)

我试图动态删除元素,但删除的元素仍然显示在屏幕上,并在单击元素时删除。
这是我使用的代码

onPageFinished: (url){
        removeAds(url);
      },
 void removeAds(String url){
      print("asdjkhjahsdk");
      //_controller.loadUrl('javascript: var ad= document.getElementById("_b04pct_2709847"); ad.parentNode.removeChild(ad);');
      _controller.evaluateJavascript('var ad= document.getElementById("_b04pct_2709847"); ad.parentNode.removeChild(ad);');

  }
v6ylcynt

v6ylcynt1#

evaluateJavascript("document.getElementById('_b04pct_2709847').style.display='none';")

iq3niunx

iq3niunx2#

试试这个!

evaluateJavascript("document.getElementsByClassName('_b04pct_2709847')[0].style.display='none';");

我猜您忘记了“[0]”。我尝试在加载页面之前隐藏该元素。是否成功?

3htmauhk

3htmauhk3#

webview_flutter 3.0.0开始我就是这么做的
作为一个controller = Completer<WebViewController>();的未来

onProgress: (progress) {

        if (progress > 30) {
          //remove the element with class name
          controller.future.then((webviewController) => webviewController
              .runJavascript(
                  'document.getElementsByClassName("td-header-template-wrap")[0].remove();')
              .then(
                  (value) => debugPrint('Page finished running Javascript'))
              .catchError((onError) => debugPrint('$onError')));
        }
      },

我在onProgress中这样做是因为我希望javascript在整个页面加载完成之前只运行一次。这也可以在onPageFinished中完成。有时在javascript运行之前标记并不存在,这就是为什么首选在onPageFinished中这样做。

bnlyeluc

bnlyeluc4#


THIS WORKS FOR ME (FLUTTER 3)网络视图_抖动4.0.6 https://pub.dev/packages/webview_flutter
我只是想合作,到目前为止文档中没有太多内容,我创建了自己的解决方案,基于控制器的事件,我创建了一个呈现条件,并在承诺解决时更新状态,以删除CircularProgressIndicator()并显示WebViewWidget(控制器:控制器),但在我做javascript更改之前。

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

class MyExample extends StatefulWidget {
  const MyExample({super.key});

  @override
  State<MyExample> createState() => _MyExampleState();
}

class _MyExampleState extends State<MyExample> {
  bool _loading = true;

  late final WebViewController controller;
  @override
  void initState() {
    super.initState();

    controller = WebViewController()
      ..setJavaScriptMode(JavaScriptMode.unrestricted)
      ..setBackgroundColor(const Color(0x00000000))
      ..setNavigationDelegate(
        NavigationDelegate(
          onProgress: (int progress) {
            // Update loading bar.
          },
          onPageStarted: (String url) {
            print("start..........");
          },
          onPageFinished: (_) => {
            _onAddToJs(controller),
            print("end.........."),
            setState(() {
              _loading = false;
            })
          },
          onWebResourceError: (WebResourceError error) {},
          onNavigationRequest: (NavigationRequest request) {
            if (request.url.startsWith('https://www.youtube.com/')) {
              return NavigationDecision.prevent;
            }
            return NavigationDecision.navigate;
          },
        ),
      )
      ..loadRequest(Uri.parse("https://example.com"));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Example AppBar')),
      body: _loading
          ? const Center(child: CircularProgressIndicator())
          : WebViewWidget(controller: controller),
    );
  }
}

_onAddToJs(webViewController) {
  webViewController.runJavaScript(
    '''let aNewHeadElement = document.createElement("h1");
      aNewHeadElement.id = "newHeadElement";
      document.body.appendChild(aNewHeadElement);
      document.getElementById("newHeadElement").innerHTML = "Hello, I was created with javascript";''',
  );
}

相关问题