Android: how to initiate Android attacks via javascript

I have some code to express text passed from html. Which I was able to get it to work using the code below.

Here is the code:

    tts = new TextToSpeech(this, this);
    myWebView = (WebView) findViewById(R.id.webviewid);
    //The html text is from the server.
    myWebView.loadDataWithBaseURL(null,
            "<html>" +
            "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">" +
            "</head>" +
            "<body>" +
            "<input class=\"textBox\" id=\"pass\" type=\"text\" maxlength=\"30\" required/>" +
            "<input type=\"button\" value=\"Say hello\" onClick=\"androidSpeak('Hello Android!')\" />" +
            "<script type=\"text/javascript\">" +
            "    function androidSpeak(texttospeak) {" +
            "        Android.textToSpeak(texttospeak);" +
            "    }" +
            "</script>" +
            "</body>" +
            "</html>", "text/html", "UTF-8", null);
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");

      

Here is my Java script interface:

    protected class WebAppInterface {
        Context mContext;

        /** Instantiate the interface and set the context */
        WebAppInterface(Context c) {
            mContext = c;
        }

        /** Show a toast from the web page */
        @JavascriptInterface
        public void textToSpeak(String texttospeak) {
            Toast.makeText(mContext, texttospeak, Toast.LENGTH_SHORT).show();
            speakOut(texttospeak);
        }
    }


    private void speakOut(String text) {
        tts.speak(text, TextToSpeech.QUEUE_ADD, null);
    }

      

The above code works as expected. My question is, how can I call TTS or any other function (like alarm or call, etc.) via javascript. This means you can call the tts.speak () method from javascript. Or to put it another way, I wanted to call TTS androids via javascript instead of creating a Java script interface (like above). This way I don't have to give the user an update for every feature I am about to add.

Example: Please note: Below is an example which I know is not correct. This will give you an overview of what I need.

    //The html text is from the server.
    myWebView.loadDataWithBaseURL(null,
            "<html>" +
            "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">" +
            "</head>" +
            "<body>" +
            "<input class=\"textBox\" id=\"pass\" type=\"text\" maxlength=\"30\" required/>" +
            "<input type=\"button\" value=\"Say hello\" onClick=\"androidSpeak('Hello Android!')\" />" +
            "<script type=\"text/javascript\">" +
            "    function androidSpeak(texttospeak) {" +
            "        tts = new TextToSpeech(this, this);" +
            "        tts.speak(text, TextToSpeech.QUEUE_ADD, null);" +  <<< directly invoke the android TTS.
            "    }" +
            "</script>" +
            "</body>" +
            "</html>", "text/html", "UTF-8", null);

      

Is there any plugin I need to add. Please let me know. Any help is appreciated.

+3


source to share


3 answers


Cordova will let you call custom Android native code from your javascript, or you can use plugins from its library that expose common hardware elements (like menu button, custom notifications, etc.). Cordova creates a complete application environment, so you don't have to worry about any Android application details and build all your application logic in html5 / javascript. Phonegap is a Cordova implementation that provides really good build tools - you don't even need to set up android build tools for your development machine - just upload your html site and it will build your android app.: P



+1


source


You can take a look at Intel XDK: https://software.intel.com/en-us/html5/tools and download it from http://xdk-software.intel.com/ . It provides the cordova build option you mentioned in your answer to the previous answer: https://software.intel.com/en-us/html5/articles/using-the-cordova-for-android-ios-etc-build- option



thank

0


source


Yes, view the method addJavascriptInterface

in the WebView. See more here and here

0


source







All Articles