|
5 | 5 | An example browser-based client.
|
6 | 6 | -->
|
7 | 7 | <html><head><script type="text/javascript">
|
8 |
| -function onSubmit() { |
9 |
| - if (!("WebSocket" in window)) { |
10 |
| - alert("Your browser doesn't support WebSockets!"); |
11 |
| - return; |
12 |
| - } |
| 8 | +var ws, target_id, cmd_id = 1000, commands = []; |
| 9 | +function onConnect() { |
13 | 10 | ol_clear("send_ol");
|
14 | 11 | ol_clear("recv_ol");
|
| 12 | + ws && ws.close(); |
15 | 13 |
|
16 |
| - var form = document.getElementById('f'); |
| 14 | + var form = document.getElementById("f"); |
17 | 15 | var port = form.elements["port"].value;
|
18 | 16 | var page_num = form.elements["page_num"].value;
|
19 | 17 | var url = "ws://localhost:"+port+"/devtools/page/"+page_num;
|
20 |
| - |
21 |
| - var text = document.getElementById('commands'); |
22 |
| - var lines = text.value.split("\n"); |
23 |
| - var commands = []; |
24 |
| - for (var i = 0; i < lines.length; i++) { |
25 |
| - line = lines[i].trim(); |
26 |
| - if (line && line.charAt(0) != '#') { |
27 |
| - commands.push(line); |
28 |
| - } |
29 |
| - } |
30 |
| - |
31 |
| - var count = 0; |
32 |
| - ol_append("send_ol", "open "+url); |
33 |
| - var ws = new WebSocket(url); |
34 |
| - |
35 |
| - function send_next_command(in_msg) { |
36 |
| - ol_append("recv_ol", in_msg); |
37 |
| - if (count < commands.length) { |
38 |
| - out_msg = commands[count++]; |
39 |
| - ol_append("send_ol", out_msg); |
40 |
| - ws.send(out_msg); |
41 |
| - } else if (count == commands.length) { |
42 |
| - count++; |
43 |
| - ol_append("send_ol", "close"); |
44 |
| - ws.close(); |
45 |
| - } |
46 |
| - }; |
| 18 | + ws = new WebSocket(url); |
47 | 19 |
|
48 | 20 | ws.onopen = function() {
|
49 |
| - send_next_command("opened "+url); |
| 21 | + ol_append("recv_ol", "opened "+url); |
50 | 22 | };
|
51 | 23 | ws.onmessage = function(evt) {
|
52 |
| - send_next_command(evt.data); |
| 24 | + ol_append("recv_ol", evt.data); |
| 25 | + |
| 26 | + var in_cmd = JSON.parse(evt.data); |
| 27 | + if (in_cmd.method === "Target.targetCreated") { |
| 28 | + target_id = in_cmd.params.targetInfo.targetId; |
| 29 | + } |
| 30 | + |
| 31 | + send_next_command(); |
53 | 32 | };
|
54 | 33 | ws.onclose = function() {
|
55 | 34 | ol_append("recv_ol", "closed");
|
|
58 | 37 | ol_append("recv_ol", "error: "+e.data);
|
59 | 38 | };
|
60 | 39 | }
|
| 40 | +function onCommand() { |
| 41 | + if (!ws) { |
| 42 | + return alert("WebSocket is not connected!"); |
| 43 | + } else if (ws.readyState !== 1) { |
| 44 | + return alert("WebSocket is not open, current state: "+ws.readyState); |
| 45 | + } else if (!target_id) { |
| 46 | + return alert("Page target was not received yet, waiting for \"Target.targetCreated\" message"); |
| 47 | + } |
| 48 | + |
| 49 | + var text = document.getElementById("commands"); |
| 50 | + var lines = text.value.split("\n"); |
| 51 | + |
| 52 | + lines.forEach((line) => { |
| 53 | + var msg = line.trim(); |
| 54 | + if (msg) { |
| 55 | + commands.push({id: cmd_id++, method: "Target.sendMessageToTarget", params: {targetId: target_id, message: msg}}); |
| 56 | + } |
| 57 | + }); |
| 58 | + |
| 59 | + send_next_command(); |
| 60 | +} |
| 61 | + |
| 62 | +function send_next_command() { |
| 63 | + if (commands.length > 0) { |
| 64 | + var out_msg = JSON.stringify(commands.shift()); |
| 65 | + ol_append("send_ol", out_msg); |
| 66 | + ws.send(out_msg); |
| 67 | + } |
| 68 | +}; |
61 | 69 |
|
62 | 70 | function ol_clear(id) {
|
63 | 71 | var o_ol = document.getElementById(id);
|
|
88 | 96 | /><sup><a href="http://localhost:9221" target="_new">?</a></sup
|
89 | 97 | >/devtools/page/<input name="page_num" class="right" size="5" value="1"
|
90 | 98 | /><sup><a href="http://localhost:9222" target="_new">?</a></sup>
|
91 |
| -<input type="button" onclick="onSubmit()" value="run"/><p> |
| 99 | +<input type="button" onclick="onConnect()" value="connect"/> |
| 100 | +<input type="button" onclick="onCommand()" value="send"/><p> |
92 | 101 | <textarea id="commands" rows="5" cols="80">
|
93 | 102 | {"id":1,"method":"Page.navigate","params": {"url":"http://www.google.com/"}}
|
94 | 103 | </textarea>
|
95 | 104 | </form>
|
96 | 105 | <table>
|
97 | 106 | <tr><td width="50%" class="top">
|
98 |
| -<form onSubmit="return send();"> |
99 | 107 | <ol id="send_ol"></ol>
|
100 |
| -<form> |
101 | 108 | </td><td class="top">
|
102 | 109 | <ol id="recv_ol"></ol>
|
103 | 110 | </td>
|
|
0 commit comments