QPython3 problems with Javascript in HTML GUI

2019-09-11 15:15发布

QPython3, Android 6.0; I guess JS cann't instance var droid = new Android(); Any hints to solve the problem?

Python Code (the same in both cases!):

import android

droid=android.Android()

droid.webViewShow('file:///storage/emulated/0/qpython/scripts3/test10_le_HTMLGUI_01.html')

while True:
    result=droid.eventWaitFor('sag').result
    droid.ttsSpeak(result['data'])

HTML-Code: test10_le_HTMLGUI_01.html (Case 1):

<html>
  <head>
    <title>Text To Speech</title>
    <script>
      var n = prompt("Hello JavaScript","");
      var droid = new Android();
      var speak = function() { 
        var n = prompt("Hello JavaScript","");
        droid.eventPost("sag", document.getElementById("sag").value, 1);
      }
    </script>
  </head>
  <body>
    <form onsubmit="speak(); return false;">
      <label for="sag">What is your message?</label>
      <input type="text" id="sag" />
      <input type="submit" value="Speak" />
    </form>
  </body>
</html>

HTML-Code (Case 2):

<html>
  <head>
    <title>Text To Speech</title>
    <script>
      var droid = new Android();
      var n = prompt("Hello JavaScript","");
      var speak = function() { 
        var n = prompt("Hello JavaScript","");
        droid.eventPost("sag", document.getElementById("sag").value, 1);
      }
    </script>
  </head>
  <body>
    <form onsubmit="speak(); return false;">
      <label for="sag">What is your message?</label>
      <input type="text" id="sag" />
      <input type="submit" value="Speak" />
    </form>
  </body>
</html>

While in case 1 the prompt("Hello JavaScript",""); is executed it is not in case 2!

QPython3 (Android 6) SL4A eventPost() and eventWaitFor() works fine on the following example (for add. info refer to http://www.mithril.com.au/android/doc/):

import android, time
droid=android.Android()
t = 'Geschafft!'
print('debug 1')
droid.eventPost('Event1', t, 1)
print('debug 2')
print('debug 3')
result=droid.eventWaitFor('Event1').result
droid.ttsSpeak(result['data'])
print('debug 4')
print(result['data'])

Proof: If you comment out the line droid.eventPost('Event1', t, 1) then eventWaitFor() will block forever!

Maybe we 've a similar Problem in QPython3 webViewShow - Script doesn't receiving data and in https://stackoverflow.com/questions/36478310/qpython-webview-javascript-android-object

Regards Gustav (2017-03-21)

2条回答
女痞
2楼-- · 2019-09-11 15:55

I encountered same problem. As far as I found, at least in recent version you're required to add an Android interface when using webview by calling JavascriptAddInterface, and from the SL4A sources seems like it doesn't. Too bad...

查看更多
\"骚年 ilove
3楼-- · 2019-09-11 15:58

Use bottle() instead of JavaScript. It's already integrated in QPython3 and provides solutions for pretty the same tasks. Find a tutorial for bottle() at https://bottlepy.org/docs/dev/bottle-docs.pdf

Regards Gustav

查看更多
登录 后发表回答