Programing

WebViewClient에서 일반 JavaScript 활성화

lottogame 2020. 11. 27. 07:36
반응형

WebViewClient에서 일반 JavaScript 활성화


구글에서 답을 찾는 동안 해결이 불가능 해 보이는 문제에 갇힌 것은 나만이 아닌 것 같다.

사용자 지정 WebViewClient를 사용하여 WebView를 만들었습니다. 이렇게하면 URL을로드 할 수없는 경우 프로세스 대화 상자가 있고 오류 메시지를 표시 할 수 있습니다.

그러나 이것은 JavaScript에 문제를 일으 킵니다. 내가로드하는 URL에는 일부 HTML 요소 CSS 스타일 (요소 표시 또는 숨기기)을 변경하거나 클릭시 다른 위치로 리디렉션하거나 경고 상자를 표시하려는 JavaScript가 있습니다. 그러나 WebViewClient를 사용하면 아무것도 작동하지 않습니다.

페이지를로드하는 방법은 다음과 같습니다.

public void loadUrl(String url)
{
    final ProgressDialog dialog = ProgressDialog.show(myActivity.this, "",  getString(R.string.loading), true);
            final WebView myWebView = (WebView) findViewById(R.id.webview);
            myWebView.setVerticalScrollBarEnabled(false);
            myWebView.setHorizontalScrollBarEnabled(false);
            WebSettings webSettings = myWebView.getSettings();
            webSettings.setJavaScriptEnabled(true);                                         


            myWebView.setWebViewClient(new WebViewClient() 
            {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) 
                {
                    Toast.makeText(myActivity.this, url, Toast.LENGTH_SHORT).show(); //Debugging purposes
                if (url.endsWith(".mp4")) 
                {
                        Intent intent = new Intent(Intent.ACTION_VIEW);
                        intent.setDataAndType(Uri.parse(url), "video/mp4");
                        view.getContext().startActivity(intent);  
                }
                else
                {                           
                        view.loadUrl(url);
                    }

                    return true;
                }

                public void onPageFinished(WebView view, String url) 
                {
                    //Toast.makeText(myActivity.this, "Oh no!", Toast.LENGTH_SHORT).show();
                    dialog.dismiss();
                }

                public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) 
                {
                    Toast.makeText(myActivity.this, description, Toast.LENGTH_SHORT).show();
                    String summary = "<html><body><strong>" + getString(R.string.lost_connection) + "</body></html>";
                    myWebView.loadData(summary, "text/html", "utf-8");   
                }              

             }); //End WebViewClient

            myWebView.loadUrl(url);     
        }

이것은 아마도 더 현명한 방법으로 수행 될 수 있지만 저는 Java와 Android 개발 모두에 익숙하지 않습니다.

Is it possible for me to enable the JavaScript for the WebViewClient at all? Removing the WebViewClient solves the problem, but then I can't catch events when the page errors or is finished loading.


I don't know what your exact problem is, but i can enable the JavaScript and a custom WebViewclient without any problem:

WebView vistaWeb = (WebView) findViewById(R.id.webview);
vistaWeb.setWebChromeClient(new MyCustomChromeClient(this));
vistaWeb.setWebViewClient(new MyCustomWebViewClient(this));
vistaWeb.clearCache(true);
vistaWeb.clearHistory();
vistaWeb.getSettings().setJavaScriptEnabled(true);
vistaWeb.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

Try this to enable javascript

WebView myWebView = (WebView) findViewById(R.id.webView);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebViewClient());
myWebView.loadUrl(url);

The proper way to enable JavaScript is by add the below two lines:

mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

Even if after adding its not working then try adding the below line.

mWebView.getSettings().setDomStorageEnabled(true);

Now It should work. :)


Do "Javascript-URLs" get routed through shouldOverrideUrlLoading? Try checking that and if so, return false for links like that (so that the webview handles the link, not your WebViewClient)


What happened in my case : I was serving local html files and when applying

    web.getSettings().setJavaScriptEnabled(true);
    web.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

but then my urls stopped working. Solution to make both JS and local href work was to add

    web.getSettings().setAllowFileAccess(true);
    web.getSettings().setAllowFileAccessFromFileURLs(true);

where

    web = (WebView) findViewById(R.id.embedded_web);

Similar to @mdelolmo answer, but in Kotlin:

    webview.setWebChromeClient(WebChromeClient())
    webview.setWebViewClient(WebViewClient())
    webview.clearCache(true)
    webview.clearHistory()
    webview.getSettings().setJavaScriptEnabled(true)
    webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true)

참고URL : https://stackoverflow.com/questions/5089578/enabling-general-javascript-in-webviewclient

반응형