机器人的WebView +使用loadURL用JavaScript + onPageFinished =滞后机器人、WebView、loadURL、JavaScript

2023-09-06 02:33:10 作者:100%无添加纯沙雕

我的WebView它加载一些页面,当它完成了我申请一些JavaScript魔术乱用DOM。一切都很好,页面加载和 onPageFinished 我只是叫 wv.loadUrl(JavaScript的);

I have WebView which loads some page, when it's finished I apply some javascript magic to mess up with DOM. Everything is fine, page loads and onPageFinished I just call wv.loadUrl(javascript);

但我不希望看到的加载过程,以及如何JavaScript是工作,我只需要结果,所以我做了我的观点看不到与 wv.setVisibility(View.INVISIBLE); 从一开始,并使其再次可见,当一切都做。这是会发生的问题。

But I don't want to see loading process, and how javascript is working, I just need result, so I made my view invisible with wv.setVisibility(View.INVISIBLE); from the start, and make it visible again when everything is done. This is where problem occurs.

这一块code应该视图中的javascript完成后,但JavaScript的前 wv.setVisibility(View.VISIBLE)火灾。因此,对于一个时刻,我看到网页以及它是如何由JavaScript的改变。这仅仅是丑陋的。

This piece of code should make view visible after javascript is finished, but wv.setVisibility(View.VISIBLE) fires before javascript. So for a moment I see page and how it is being changed by javascript. This is just ugly.

  public void onPageFinished (WebView view, String url) {
         wv.loadUrl(javascript);
         view.getSettings().setLoadsImagesAutomatically(true);
         wv.setVisibility(View.VISIBLE);

    }

我得到的使用loadURL以异步方式工作,所以我想再拍WebViewClient只有做第一个视图中显示方法中 onPageFinished ,并用它来调用JS。但它只是不断崩溃与NPE错误。

I got that loadUrl works asynchronously, so I tried to make another WebViewClient with just "make first view visible" method inside onPageFinished, and use it to call JS. But it just keeps crashing with NPE error.

wv2.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageFinished (WebView view, String url) {
     wv.setVisibility(View.VISIBLE);
}
});

有关的JavaScript后,现在我只是添加的延迟( SystemClock.sleep(5000)),但是这就像..是啊。

For now I just added delay after javascript (SystemClock.sleep(5000)), but this is like.. yeah.

推荐答案

好吧,我想我想通了。我们可以通过JavaScript的结束回调,这将触发显示视图 - 如这里或此处(与处理程序,因为我想更新UI)。 但它不会改变任何东西,这个问题似乎是在渲染速度。这只是缓慢。所以,现在我只要坚持使用JS烧成后的1秒的延迟。

Ok, I think I figured it out. We can have callback from the end of javascript that will fire "show view" - like here or here (with handler, because I wanted to update ui). But it wont change anything, the problem seems to be in rendering speed. It's just slow. So for now I'll just stick with 1 second delay after firing JS.