Android在重定向webview中执行JavaScript

时间:2017-10-09 11:31:15

标签: javascript android webview android-webview

我称主要网址为“https://foo.com/default?companyId=1929770&authorizationSource=FTP”, 当我加载主网址时,我会被重定向到辅助网址,例如“https://foo.com/default?companyId=1929770&authorizationSource=FTP_123456789”。辅助URL始终随机生成带有数字的FTP。我如何为辅助URL执行javascript?

output: {
  path: path.join(__dirname, './../public'),
  filename: 'bundle.js',
  publicPath: '/',
  chunkFilename: '[name].[chunkhash].js'
},

logcat的:

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

1 个答案:

答案 0 :(得分:1)

你可以在onPageFinished

中执行这样的js代码
String jsScript = "javascript:alert(" + msg + ");";
webView.loadUrl(jsScript);

另外,对我来说:我添加一个开关来了解我打开的网址数量:

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