-
Notifications
You must be signed in to change notification settings - Fork 1
/
extension.html
132 lines (117 loc) · 3.61 KB
/
extension.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<!doctype html>
<html lang="en" data-manifest="" data-framework="typescript">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
</head>
<style type="text/css">
@import "/blb/semantic.css";
</style>
<style>
body {
color: white !important;
}
#logs {
position: absolute;
left: 0;
width: 100%;
border: 0;
font-size: 0.6rem;
}
#logs img {
max-width: 18rem;
}
</style>
<script>
var extId = window.location.hash.substr(1);
var logs;
var idToType = {};
function receiveMessage(ev) {
var msg = ev.data;
logs.innerText += formatResponse(msg);
delete idToType[msg.id];
}
function formatResponse(resp) {
if (!resp.id)
return '\n' + resp.event + ', ' + JSON.stringify(resp.body, null, 2).replace('\n', '');
var action = idToType[resp.id];
return "\n< " + action + " (response) s=" + resp.success + " "
+ (resp.error ? resp.error : "") + (resp.resp ? JSON.stringify(resp.resp) : "");
}
function sendRequest(action) {
var msg = mkRequest(action);
switch (action) {
case "extdatastream":
msg.body = {
serial: true
};
break;
case "extquerypermission":
break;
case "extrequestpermission":
msg.body = {
serial: true
};
break;
case "extusercode":
break;
case "extreadcode":
break;
case "extwritecode":
msg.body = {
code: "let x = 0;",
json: '{ "whatever": "sure" }'
}
break;
case "extinit":
break;
}
logs.innerText += "\n> " + msg.action;
window.parent.postMessage(msg, "*");
}
function mkRequest(action) {
var id = Math.random().toString();
idToType[id] = action;
return {
type: "pxtpkgext",
action: action,
extId: extId,
response: true,
id: id
}
}
function bindButtons() {
var elt = document.getElementById("buttons");
for (let i = 0; i < elt.childNodes.length; i++) {
var btn = elt.childNodes.item(i);
if (btn.nodeType === Node.ELEMENT_NODE) {
let action = btn.getAttribute("action");
if (action) {
btn.addEventListener("click", function () {
sendRequest(action);
});
}
}
}
}
window.addEventListener("message", receiveMessage, false);
window.onload = function () {
bindButtons();
logs = document.getElementById("logs");
this.sendRequest("extinit");
logs.innerText += "\n<< got id " + extId;
}
</script>
<body>
<h1>Test Extension</h1>
<div id="buttons" class="ui buttons">
<button class="ui button" action="extrequestpermission">ask permission</button>
<button class="ui button" action="extquerypermission">query permissions</button>
<button class="ui button" action="extreadcode">read code</button>
<button class="ui button" action="extwritecode">write code</button>
<button class="ui button" action="extdatastream">console</button>
<button class="ui button" action="extusercode">user code</button>
</div>
<pre id="logs" class="ui"></pre>
</body>
</html>