Skip to content

Commit a2c7d28

Browse files
authored
Update devtools html protocol interaction example (#411)
* updated example demonstrating the devtools protocol interaction * removed nodejs example which is basically duplicate of html example
1 parent c5d2e95 commit a2c7d28

File tree

4 files changed

+46
-89
lines changed

4 files changed

+46
-89
lines changed

design.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Source
1515

1616
- [src/ios_webkit_debug_proxy.c](src/ios_webkit_debug_proxy.c)
1717
\- WebInspector to WebKit Remote Debugging Protocol translator
18-
\- See [examples/wdp_client.js](examples/wdp_client.js) and <http://localhost:9221>
18+
\- See [examples/wdp_client.html](examples/wdp_client.html) and <http://localhost:9221>
1919

2020
- [src/webinspector.c](src/webinspector.c)
2121
\- iOS WebInspector library

examples/README.md

-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ Example Proxy Clients
1313
- Browser JS
1414
\- [wdp_client.html](wdp_client.html) demonstrates DevTools command I/O, e.g. "Page.navigate".
1515

16-
- NodeJS
17-
\- [wdp_client.js](wdp_client.js) is the command-line equivalent of [wdp_client.html](wdp_client.html).
18-
1916
- C
2017
\- [wi_client.c](wi_client.c) a minimal webinspector client.
2118

examples/wdp_client.html

+45-38
Original file line numberDiff line numberDiff line change
@@ -5,51 +5,30 @@
55
An example browser-based client.
66
-->
77
<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() {
1310
ol_clear("send_ol");
1411
ol_clear("recv_ol");
12+
ws && ws.close();
1513

16-
var form = document.getElementById('f');
14+
var form = document.getElementById("f");
1715
var port = form.elements["port"].value;
1816
var page_num = form.elements["page_num"].value;
1917
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);
4719

4820
ws.onopen = function() {
49-
send_next_command("opened "+url);
21+
ol_append("recv_ol", "opened "+url);
5022
};
5123
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();
5332
};
5433
ws.onclose = function() {
5534
ol_append("recv_ol", "closed");
@@ -58,6 +37,35 @@
5837
ol_append("recv_ol", "error: "+e.data);
5938
};
6039
}
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+
};
6169

6270
function ol_clear(id) {
6371
var o_ol = document.getElementById(id);
@@ -88,16 +96,15 @@
8896
/><sup><a href="http://localhost:9221" target="_new">?</a></sup
8997
>/devtools/page/<input name="page_num" class="right" size="5" value="1"
9098
/><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>
92101
<textarea id="commands" rows="5" cols="80">
93102
{"id":1,"method":"Page.navigate","params": {"url":"http://www.google.com/"}}
94103
</textarea>
95104
</form>
96105
<table>
97106
<tr><td width="50%" class="top">
98-
<form onSubmit="return send();">
99107
<ol id="send_ol"></ol>
100-
<form>
101108
</td><td class="top">
102109
<ol id="recv_ol"></ol>
103110
</td>

examples/wdp_client.js

-47
This file was deleted.

0 commit comments

Comments
 (0)