-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent-script.js
179 lines (149 loc) · 4.93 KB
/
content-script.js
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright (c) 2019 SafetyCulture Pty Ltd. All Rights Reserved.
/* global chrome */
const kMessageSource = "__GRPCWEB_DEVTOOLS__";
let port;
function injectors(msgSource) {
const MethodType = {
UNARY: 'unary',
SERVER_STREAMING: 'server_streaming'
};
const requestMethodName = (req) => req?.getMethodDescriptor().name ?? 'Unknown'
const postMessage = (type, name, reqMsg, resMsg, error) => {
const request = reqMsg?.toObject();
const response = error ? undefined : resMsg?.toObject();
error = error ? (({code, message}) => ({ code, message }))(error) : error;
const msg = {
source: msgSource,
methodType: type,
method: name,
request,
response,
error
};
window.postMessage(msg, '*');
}
class DevToolsUnaryInterceptor {
type = MethodType.UNARY;
postResponse = (name, req, res) => {
postMessage(this.type, name, req?.getRequestMessage(), res?.getResponseMessage());
return res;
}
postError = (name, req, error) => {
if (error.code === 0) return error;
postMessage(this.type, name, req?.getRequestMessage(), undefined, error);
return error;
}
intercept = (request, invoker) => {
const name = requestMethodName(request);
return invoker(request)
.then((response) => this.postResponse(name, request, response))
.catch((error) => { throw this.postError(name, request, error) });
}
}
class DevToolsStreamInterceptor {
intercept(request, invoker) {
class InterceptedStream {
type = MethodType.SERVER_STREAMING;
name;
stream;
constructor(request, invoker) {
this.name = requestMethodName(request);
this.stream = invoker(this.postStreamRequest(request));
};
postStreamRequest = (req) => {
postMessage(this.type, this.name, req?.getRequestMessage());
return req;
}
postStreamData = (data) => {
postMessage(this.type, this.name, undefined, data);
return data;
}
postStreamError = (error) => {
if (error.code === 0) return error;
postMessage(this.type, this.name, undefined, undefined, error);
return error;
}
postStreamStatus = (status) => {
if (status.code !== 0) return status;
// FIXME: this is a semi-ugly hack. We add
// toObject() => 'EOF' to the status object
// to mimic an reqMes and make postMessage
// happy. In the future, maybe status could
// be handled properly.
status.toObject = () => 'EOF';
return this.postStreamData(status);
}
on = (eventType, callback) => {
if (eventType === 'data') {
const dataCallback = (data) => {
callback(this.postStreamData(data));
}
this.stream.on(eventType, dataCallback);
} else if (eventType === 'error') {
const errorCallback = (error) => {
callback(this.postStreamError(error));
}
this.stream.on('error', errorCallback);
} else if (eventType === 'metadata') {
this.stream.on('metadata', callback);
} else if (eventType === 'status') {
const statusCallback = (status) => {
callback(this.postStreamStatus(status));
}
this.stream.on('status', statusCallback);
} else if (eventType === 'end') {
this.stream.on('end', callback);
}
return this;
};
removeListener(eventType, callback) {
}
cancel() {
}
}
return new InterceptedStream(request, invoker);
};
}
return {
// this is how it should work
devToolsUnaryInterceptor: new DevToolsUnaryInterceptor(),
devToolsStreamInterceptor: new DevToolsStreamInterceptor(),
};
}
const injectContent = `window.__GRPCWEB_DEVTOOLS__ = () => ${injectors}("${kMessageSource}");`
let s = document.createElement('script');
s.type = 'text/javascript';
const scriptNode = document.createTextNode(injectContent);
s.appendChild(scriptNode);
(document.head || document.documentElement).appendChild(s);
s.parentNode.removeChild(s);
function setupPortIfNeeded() {
if (!port && chrome?.runtime) {
port = chrome.runtime.connect(null, {name: "content"});
port.postMessage({action: "init"});
port.onDisconnect.addListener(handleDisconnect);
}
}
function sendGRPCNetworkCall(data) {
setupPortIfNeeded();
if (port) {
port.postMessage({
action: "gRPCNetworkCall",
target: "panel",
data,
});
}
}
function handleMessageEvent(event) {
if (
event.source === window &&
event?.data.source === kMessageSource
) {
sendGRPCNetworkCall(event.data);
}
}
function handleDisconnect() {
port = null;
window.removeEventListener("message", handleMessageEvent, false);
}
window.addEventListener("message", handleMessageEvent, false);