Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes, if you've got the inspector enabled in your UIWebView by following the instructions, it should be possible to connect to it from Python. I played around with it, and figured out how to send and receive commands using a Web Socket. Here's a script for Python 2.7 using <a href="https://github.com/liris/websocket-client" rel="noreferrer">websocket-client</a></p> <pre class="lang-py prettyprint-override"><code>import json import socket from websocket import WebSocket ws = WebSocket() # if ipv6 ws.io_sock = ws.sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM) ws.connect("ws://localhost:9999/devtools/page/1") counter = 0 def send(method, params): global counter counter += 1 # separators is important, you'll get "Message should be in JSON format." otherwise message = json.dumps({"id": counter, "method": method, "params": params}, separators=(',', ':')) print "&gt; %s" % (message,) ws.send(message) def recv(): result = ws.recv() print "&lt; %s" % (result,) send('Runtime.evaluate', {'expression': 'alert("hello from python")'}) recv() </code></pre> <p>This uses the <a href="http://code.google.com/chrome/devtools/docs/protocol/0.1/runtime.html#command-evaluate" rel="noreferrer">Runtime.evaluate</a> function to show an alert.</p> <p>I tried running it against MobileSafari running in the simulator, and it worked fine. I noticed two important things:</p> <ul> <li>the remote server is bound to an IPv6 port, and websocket-client didn't connect without the line to override the socket and set the family. Not sure if it would be the same running on a device or in a UIWebView.</li> <li>it doesn't like spaces around the separators in the JSON.</li> </ul> <p>Here's what it looks like enabling the inspector in MobileSafari using gdb and running the script:</p> <pre><code>$ ps x | grep MobileSafari 4968 ?? Z 0:00.00 (MobileSafari) 6234 ?? S 0:00.69 /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.0.sdk//Applications/MobileSafari.app/MobileSafari 6238 s007 R+ 0:00.00 grep MobileSafari $ gdb GNU gdb 6.3.50-20050815 (Apple version gdb-1708) (Thu Nov 3 21:59:02 UTC 2011) ... (gdb) attach 6234 Attaching to process 6234. Reading symbols for shared libraries . done Reading symbols for shared libraries ........................................................................................................................................................ done 0x99798c22 in mach_msg_trap () (gdb) p (void *)[WebView _enableRemoteInspector] $1 = (void *) 0x2ac93ce (gdb) detach Detaching from process 6234. (gdb) quit $ python debug.py &gt; {"params":{"expression":"alert(\"hello from python\")"},"id":1,"method":"Runtime.evaluate"} &lt; {"result":{"result":{"type":"undefined","description":"undefined"}},"id":1} </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload