首页 > 解决方案 > 如何使用 location.href 或其他命令在 Flutter Webview 内的 Javascript 中加载页面 *syncronous*

问题描述

我想加载一个页面:

window.location.href='http://www.g1.com.br'; 

或通过使用

window.location.assign('http://www.g1.com.br');

然后对这个页面执行下一个命令。

如果我在浏览器的控制台中执行下面的代码,它将在页面加载之前发出警报:

window.location.href='http://www.g1.com.br'; 
alert('oi');

例如,在 dart lang 中,如果 href 是一个加载页面的异步函数,我会这样做:

href.load('http://www.g1.com').then(execJs(myJsVar));

我想要实现的是在 Flutter 中运行的 WebView 中使用 javascript 进行一些网络抓取。我发现在几个页面之间导航而不处理onPageFinished颤振的 WebView 方法的方式是完全在 WebView 内的 Javascript 中进行导航。

但是我对Javascript不太了解。href并且assign似乎是异步功能,但如果可能的话,我不知道如何对其应用某种方式then

真正的代码:

我加载一个页面并在该页面上执行其他命令来填写表格。比我加载第二页并做同样的事情,

window.location.assign('%targetUrl%');

document.querySelectorAll('#email')
    .forEach(
        function (field, index, arr) {
            field.value = '%username%';
            field.dispatchEvent(new Event('input'));
        }
    );

document.querySelectorAll('#pass')
    .forEach(
        function (field, index, arr) {
            field.value = '%password%';
            field.dispatchEvent(new Event('input'));
        }
    );
window.location.assign('%targetUrl2%');

/// fill other forms, submit....

标签: javascriptandroidflutterwebviewdart

解决方案


推荐阅读