Android WebView +使用 JavaScript 加载 URL + onPageFinished = 滞后

发布于 2024-11-03 13:55:00 字数 1046 浏览 0 评论 0原文

我有 WebView 加载一些页面,当它完成时,我应用一些 javascript 魔法来搞乱 DOM。一切都很好,页面加载和 onPageFinished 我只是调用 wv.loadUrl(javascript);

但我不想看到加载过程,以及 javascript 是如何工作的,我只需要结果,因此我从一开始就使用 wv.setVisibility(View.INVISIBLE); 将视图设为不可见,并在一切完成后使其再次可见。这就是问题发生的地方。

这段代码应该使视图在 javascript 完成后可见,但 wv.setVisibility(View.VISIBLE) 在 javascript 之前触发。因此,我暂时看到了页面以及它是如何被 javascript 更改的。这太丑了。

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

    }

我发现 loadUrl 是异步工作的,所以我尝试在 onPageFinished 中使用“使第一个视图可见”方法创建另一个 WebViewClient,并使用它来调用 JS。但它总是因 NPE 错误而崩溃。

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

现在我只是在 javascript (SystemClock.sleep(5000)) 之后添加了延迟,但这就像......是的。

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);

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.

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);

    }

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);
}
});

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅唱々樱花落 2024-11-10 13:55:00

好吧,我想我明白了。我们可以从 javascript 的末尾进行回调,该回调将触发“显示视图” - 就像这里 或 这里(带有处理程序,因为我想更新用户界面)。
但这不会改变任何东西,问题似乎出在渲染速度上。只是很慢。所以现在我将在触发 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.

寻找我们的幸福 2024-11-10 13:55:00

我认为您不会找到简单的解决方案。 Android 可以检测到(像大多数浏览器一样)的是页面何时完全加载,这意味着所有页面都已下载。 JavaScript 也会等待它被下载,然后开始运行。这就是它的工作原理。我能想到的唯一解决方案可能就是 JQuery。我对此了解不多,但其中通常有此类问题的解决方案。

I don't think you're going to get an easy solution to this. All that Android can detect (like most browsers) is when the page is fully loaded, which means, all of it is downloaded. Javascript waits for it to be downloaded also, and THEN starts running. It's just the way it works. The only solution I can think of might just be in JQuery. I don't know much about it but there's usually solutions to things like this in it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文