javascript - Android 在重定向 webview 中执行 JavaScript

标签 javascript android webview android-webview

我正在调用主 URL,如“https://foo.com/default?companyId=1929770&authorizationSource=FTP”, 当我加载主 URL 时,我将被重定向到辅助 URL,如“https://foo.com/default?companyId=1929770&authorizationSource=FTP_123456789”。二级 URL 总是随机生成末尾带有数字的 FTP。如何为辅助 URL 执行 javascript?

public class ActivityWebViewPay extends Activity {
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String privatURL = "https://foo.com/default?companyId=1929770&authorizationSource=FTP&account=" + getIntent().getStringExtra(ACCOUNT_LS);

    WebView urlWebView = new WebView(this);
    setContentView(urlWebView);
    urlWebView.getSettings().setJavaScriptEnabled(true);
    urlWebView.getSettings().setDomStorageEnabled(true);
    WebSettings settingsURL = urlWebView.getSettings();
    settingsURL.setMinimumFontSize(18);
    settingsURL.setBuiltInZoomControls(true);
    settingsURL.setDisplayZoomControls(true);
    settingsURL.setJavaScriptEnabled(true);
    settingsURL.setDomStorageEnabled(true);
    urlWebView.clearHistory();
    urlWebView.clearCache(true);
    urlWebView.loadUrl(privatURL);

    urlWebView.setWebViewClient(new WebViewClient(){
        @Override
        public void onPageFinished(WebView view, String url) {
            view.loadUrl("javascript:function a(){window.document.getElementsByClassName('property-info-wrap')[0].style.display='none';}; a()");
        }
    });
}

日志:

[INFO:CONSOLE(1)] "Uncaught TypeError: Cannot read property 'style' of undefined", source:  (1)
[INFO:CONSOLE(1)] "Uncaught TypeError: Cannot read property 'style' of undefined", source:  (1)
[INFO:CONSOLE(22)] "Start Referrer is >>> ", source: link without FTP (22)
[INFO:CONSOLE(1)] "Uncaught TypeError: Cannot read property 'style' of undefined", source:  (1)
[INFO:CONSOLE(5)] "The key "target-densitydpi" is not supported.", source: link with FTP (5)
[INFO:CONSOLE(30)] "referer ", source: link with FTP (30)
[INFO:CONSOLE(1)] "Uncaught TypeError: Cannot read property 'style' of undefined", source:  (1)

最佳答案

你可以在onPageFinished里面执行这样的js代码

String jsScript = "javascript:alert(" + msg + ");";
webView.loadUrl(jsScript);

另外,对我来说:我添加了一个开关来知道我打开的 url 的数量:

public void onPageFinished(WebView view, String url)
{
    super.onPageFinished(view, url);
    count++;
    switch (count)
    {
        case 1:
            String jsScriptInit;// do that you want
            webView.loadUrl(jsScriptInit);
            break;
        case 2:
            String jsScript;
            webView.loadUrl(jsScript);
            break;
    }
}

关于javascript - Android 在重定向 webview 中执行 JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46645406/

相关文章:

javascript - JS - 骰子掷两次,想要检查循环中是否有双掷

javascript - 使用带有语言环境的 moment.js 解析日期

Android 设备在 Ubuntu 下的 Eclipse 中不可见

android - 如何滑动图像

java - WebView - 使用凭据的正确方法

javascript - ng-显示未更新

javascript - 在无框 Electron 窗口中使子窗口与父窗口一起移动

Android 谷歌地图 API :Force Close Error

webview - 没有远程模块的 webview 和主窗口之间的通信

Android Webview GIF 堆随着时间的推移急剧增长