2017-10-09 83 views
0

我打電話的主URL,如「https://foo.com/default?companyId=1929770&authorizationSource=FTP」, 當我加載主要URL時,我將被重定向到輔助URL,如「https://foo.com/default?companyId=1929770&authorizationSource=FTP_123456789」。輔助URL總是隨機生成帶有編號的FTP。我怎樣才能執行JavaScript的輔助URL?Android執行JavaScript的重定向webview

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

logcat的:

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

回答

1

可以執行像這裏面的js代碼onPageFinished

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

您的解決方案不起作用 – mario

+0

它對我來說工作正常。嘗試添加一些日誌。在onPageFinished()和我希望你會有成功 –