2015-08-14 38 views
0

我有以下的WebView腳本:注入的JavaScript的WebView的Android

這個腳本加載JavaScript和網站的網址,當應用程序檢測短語access_token應該從access_token 例如重定向到的數據的另一個網頁:

@Override 
 
     protected void onCreate(Bundle savedInstanceState) { 
 
      super.onCreate(savedInstanceState); 
 
      setContentView(R.layout.activity_homepage); 
 
      mWebView = (WebView) findViewById(R.id.activity_main_webview); 
 
      // Enable Javascript 
 
      WebSettings webSettings = mWebView.getSettings(); 
 
      webSettings.setJavaScriptEnabled(true); 
 
    
 
     String javascriptCodeUrl= "javascript:var token = \n"+ 
 
     "top.location.href.split('access_token=')[1]; \n"+ 
 
     "if (token) { \n"+ 
 
     "top.location.href = 'http://example.com/?user=' + token;}"; 
 

 
        mWebView.loadUrl(javascriptCodeUrl); 
 

 
      mWebView.loadUrl("http://default-webpage.com"); 
 
      // Force links and redirects to open in the WebView instead of in a browser 
 
      mWebView.setWebViewClient(new WebViewClient()); 
 
     }

回答

0

您監視shouldOverrideURL方法的URL。沒有必要使用Javascript注入。它可以用Java來完成。

相關問題