Skip to content

Commit b840fc0

Browse files
jackieliirogchap
authored andcommitted
use script content instead of injecting a script file (#41)
* use script content instead of injecting a script file * remove unecessary files
1 parent 9a7ed69 commit b840fc0

File tree

3 files changed

+102
-104
lines changed

3 files changed

+102
-104
lines changed

public/content-script.js

Lines changed: 102 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,110 @@
11
// Copyright (c) 2019 SafetyCulture Pty Ltd. All Rights Reserved.
22

3+
const injectContent = `
4+
window.__GRPCWEB_DEVTOOLS__ = function (clients) {
5+
if (clients.constructor !== Array) {
6+
return
7+
}
8+
const postType = "__GRPCWEB_DEVTOOLS__";
9+
var StreamInterceptor = function (method, request, stream) {
10+
this._callbacks = {};
11+
const methodType = "server_streaming";
12+
window.postMessage({
13+
type: postType,
14+
method,
15+
methodType,
16+
request: request.toObject(),
17+
});
18+
stream.on('data', response => {
19+
window.postMessage({
20+
type: postType,
21+
method,
22+
methodType,
23+
response: response.toObject(),
24+
});
25+
if (!!this._callbacks['data']) {
26+
this._callbacks['data'](response);
27+
}
28+
});
29+
stream.on('status', status => {
30+
if (status.code === 0) {
31+
window.postMessage({
32+
type: postType,
33+
method,
34+
methodType,
35+
response: "EOF",
36+
});
37+
}
38+
if (!!this._callbacks['status']) {
39+
this._callbacks['status'](status);
40+
}
41+
});
42+
stream.on('error', error => {
43+
if (error.code !== 0) {
44+
window.postMessage({
45+
type: postType,
46+
method,
47+
methodType,
48+
error: {
49+
code: error.code,
50+
message: error.message,
51+
},
52+
});
53+
}
54+
if (!!this._callbacks['error']) {
55+
this._callbacks['error'](error);
56+
}
57+
});
58+
}
59+
StreamInterceptor.prototype.on = function (type, callback) {
60+
this._callbacks[type] = callback;
61+
return this;
62+
}
63+
clients.map(client => {
64+
client.client_.rpcCall_ = client.client_.rpcCall;
65+
client.client_.rpcCall2 = function (method, request, metadata, methodInfo, callback) {
66+
var posted = false;
67+
var newCallback = function (err, response) {
68+
if (!posted) {
69+
window.postMessage({
70+
type: postType,
71+
method,
72+
methodType: "unary",
73+
request: request.toObject(),
74+
response: err ? undefined : response.toObject(),
75+
error: err || undefined,
76+
}, "*")
77+
posted = true;
78+
}
79+
callback(err, response)
80+
}
81+
return this.rpcCall_(method, request, metadata, methodInfo, newCallback);
82+
}
83+
client.client_.rpcCall = client.client_.rpcCall2;
84+
client.client_.unaryCall = function (method, request, metadata, methodInfo) {
85+
return new Promise((resolve, reject) => {
86+
this.rpcCall2(method, request, metadata, methodInfo, function (error, response) {
87+
error ? reject(error) : resolve(response);
88+
});
89+
});
90+
};
91+
client.client_.serverStreaming_ = client.client_.serverStreaming;
92+
client.client_.serverStreaming2 = function (method, request, metadata, methodInfo) {
93+
var stream = client.client_.serverStreaming_(method, request, metadata, methodInfo);
94+
var si = new StreamInterceptor(method, request, stream);
95+
return si;
96+
}
97+
client.client_.serverStreaming = client.client_.serverStreaming2;
98+
})
99+
}
100+
`
101+
3102
let s = document.createElement('script');
4103
s.type = 'text/javascript';
5-
s.src = chrome.extension.getURL('inject.js');
6-
s.onload = function () {
7-
this.parentNode.removeChild(this);
8-
};
104+
const scriptNode = document.createTextNode(injectContent);
105+
s.appendChild(scriptNode);
9106
(document.head || document.documentElement).appendChild(s);
107+
s.parentNode.removeChild(s);
10108

11109
var port;
12110

public/inject.js

Lines changed: 0 additions & 97 deletions
This file was deleted.

public/manifest.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@
2727
"run_at": "document_start"
2828
}
2929
],
30-
"web_accessible_resources": [
31-
"inject.js"
32-
],
3330
"permissions": [
3431
"activeTab"
3532
]

0 commit comments

Comments
 (0)