简体   繁体   English

在网络视图中覆盖javascript函数

[英]overriding javascript function in a webview

I want to integrate an existing website into my webview but also override a javascript function that exists on this website with my own version? 我想将现有网站集成到我的webview中,还想用我自己的版本覆盖该网站上存在的javascript函数? Then I can use the java-javascript bridge. 然后,我可以使用java-javascript桥。 I have tried using the following but it seems to execute the function only 我尝试使用以下内容,但似乎仅执行该功能

webView.setWebViewClient(new WebViewClient() {
  @Override
  public void onPageFinished(WebView view, String url) {
    super.onPageFinished(view, url);
    webView.loadUrl("javascript: (function myFunc() { "
        + "alert('overriden alert ha!');"
        + "})()");
  }
});

You need 你需要

  1. grab html page source 抓取HTML页面源代码
  2. Edit it. 编辑它。 (add into html source you grabbed, easy way : replace("<head>","<head>"+"your code"); (添加到您抓取的html源中,简单的方法: replace("<head>","<head>"+"your code");
  3. load edited String of HTML using webview.loadDataWithBaseURL("", string, "text/html", "utf-8", ""); 使用webview.loadDataWithBaseURL("", string, "text/html", "utf-8", "");加载经过编辑的HTML webview.loadDataWithBaseURL("", string, "text/html", "utf-8", "");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM