gpt4 book ai didi

javascript - Webview不加载javascript获取内容

转载 作者:行者123 更新时间:2023-11-30 04:56:29 25 4
gpt4 key购买 nike

我想获取 url 正文标记中的所有文本,但它不起作用。我搜索了很多,但找不到任何东西。我还添加了 android.permission.INTERNET

那么问题是什么?

这是我的代码:

public class Activity_Main  extends Activity {

@SuppressLint("JavascriptInterface")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final WebView webView = (WebView) findViewById(R.id.webView);

webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);

final TextView contentView = (TextView) findViewById(R.id.contentView);

class MyJavaScriptInterface
{
private TextView contentView;

public MyJavaScriptInterface(TextView aContentView)
{
contentView = aContentView;
}

@SuppressWarnings("unused")

public void processContent(String aContent)
{
final String content = aContent;
contentView.setText(content);
}
}

webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new MyJavaScriptInterface(contentView), "INTERFACE");
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url)
{
webView.loadUrl("javascript:window.INTERFACE.processContent(document.getElementsByTagName('body')[0].innerText);");
}
});

webView.loadUrl("https://stackoverflow.com");
}
}

最佳答案

为你的接口(interface)函数更新@JavascriptInterface并检查

class MyJavaScriptInterface
{
private TextView contentView;

public MyJavaScriptInterface(TextView aContentView)
{
contentView = aContentView;
}

@JavascriptInterface
public void processContent(String aContent)
{
final String content = aContent;
contentView.setText(content);
}
}

对于上面的 KITKAT,还有另一种方法可以做到这一点

 webView.evaluateJavascript("alert('pass here some ...')", new ValueCallback<String>() {
@Override
public void onReceiveValue(String s) {

}
});

这是在android中执行js的更新方案

完整代码在这里

final WebView webView = (WebView) findViewById(R.id.webView);

webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);

final TextView contentView = (TextView) findViewById(R.id.contentView);

class MyJavaScriptInterface
{
private TextView contentView;

public MyJavaScriptInterface(TextView aContentView)
{
contentView = aContentView;
}

@JavascriptInterface
public void processContent(String aContent)
{
final String content = aContent;
contentView.setText(content);
}
}

webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new MyJavaScriptInterface(contentView), "INTERFACE");
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url)
{
super.onPageFinished(view,url);


if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.KITKAT) {
webView.evaluateJavascript("document.getElementsByTagName('body')[0].innerText", new ValueCallback<String>() {
@Override
public void onReceiveValue(String s) {
contentView.setText(s);
}
});
}
else {
webView.loadUrl("javascript:window.INTERFACE.processContent(document.getElementsByTagName('body')[0].innerText)");

}


}
});

webView.loadUrl("https://stackoverflow.com");

关于javascript - Webview不加载javascript获取内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59049448/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com