-
Notifications
You must be signed in to change notification settings - Fork 2
/
devtools.js
250 lines (215 loc) · 8.62 KB
/
devtools.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
let entries = [];
let panel;
let endOrder = 0;
let startOrder = 0;
function redrawEntries() {
if (panel) {
panel.redrawEntries(entries);
}
}
function onPendingRequest(entry) {
entries.push(entry);
if (panel) {
panel.onPendingEntry(entry);
}
}
function onFinishedRequest(oldEntry, diff) {
Object.assign(oldEntry, diff)
if (panel) {
panel.onFinishedEntry(oldEntry);
}
}
function removeEntry(entry) {
const entryIndex = entries.indexOf(entry);
if (entryIndex !== -1) {
entries.splice(entryIndex, 1);
}
if (panel) {
panel.onRemovedEntry(entry);
}
}
function onWrongContent(entry, content, data, contentLengthHeader, err) {
const message = "Cannot read Pbf from Base64 string (" +
"content = " + content + ", " +
"array = " + data + ", size = " + data.length +
(contentLengthHeader !== -1 ? ", expectedSize = " + contentLengthHeader : "") +
") for tile {z: " + entry.z + ", x: " + entry.x + ", y: " + entry.y + "}. " +
"Probably the request was aborted while reading of response body.";
console.warn(message + (err ? " Details: " + err.stack : ""));
chrome.devtools.inspectedWindow.eval("console.warn('" + message + "')");
}
function isTileEmpty(tile) {
for (let layerName in tile.layers) {
const layer = tile.layers[layerName];
if (layer.length) {
return false
}
}
return true;
}
function combineHeaders(headers/*[{name, value}]*/) {
return headers
? headers.reduce((collector, nameValue) => {
collector[nameValue.name] = nameValue.value;
return collector;
}, {})
: {};
}
let trackEmptyResponse, trackOnlySuccessfulResponse, mvtRequestPatternRegExp
chrome.storage.local.onChanged.addListener(function (changes) {
if (changes['trackEmptyResponse']) {
trackEmptyResponse = !!changes['trackEmptyResponse'].newValue;
}
if (changes['trackOnlySuccessfulResponse']) {
trackOnlySuccessfulResponse = !!changes['trackOnlySuccessfulResponse'].newValue;
}
if (changes['mvtRequestPattern']) {
let mvtRequestPattern = changes['mvtRequestPattern'].newValue;
try {
mvtRequestPatternRegExp = new RegExp(mvtRequestPattern, "i");
} catch (e) {
console.log("Mvt Request Pattern is invalid", mvtRequestPattern);
}
}
});
chrome.storage.local.get(['trackEmptyResponse', 'trackOnlySuccessfulResponse', 'mvtRequestPattern'], function (r) {
trackEmptyResponse = r.trackEmptyResponse;
trackOnlySuccessfulResponse = r.trackOnlySuccessfulResponse;
try {
mvtRequestPatternRegExp = new RegExp(r.mvtRequestPattern, "i");
} catch (e) {
console.log("Mvt Request Pattern is invalid", r.mvtRequestPattern);
}
chrome.devtools.panels.create("Mapbox Vector Tiles", "images/16.png", "mvt-tiles-panel.html", function (p) {
p.onShown.addListener((w) => {
panel = w;
panel.onClear = () => {
entries = [];
endOrder = 0;
startOrder = 0;
redrawEntries();
};
redrawEntries();
});
p.onHidden.addListener((w) => {
panel = undefined;
});
});
chrome.devtools.network.onRequestFinished.addListener(
function (httpEntry) {
const urlParseResult = httpEntry.request.url.match(mvtRequestPatternRegExp);
if (!urlParseResult) {
return;
}
const z = urlParseResult.groups && urlParseResult.groups.z || urlParseResult[1];
const x = urlParseResult.groups && urlParseResult.groups.x || urlParseResult[2];
const y = urlParseResult.groups && urlParseResult.groups.y || urlParseResult[3];
if (!z || !x || !y) {
return;
}
let nStarted = ++startOrder;
//http://qnimate.com/detecting-end-of-scrolling-in-html-element/
//https://stackoverflow.com/questions/8773921/how-to-automatically-scroll-down-a-html-page
const time = httpEntry.time;
const startedDateTime = httpEntry.startedDateTime;
const url = httpEntry.request.url;
const requestHeaders = combineHeaders(httpEntry.request.headers);
const pendingEntry = {
x: x,
y: y,
z: z,
status: -1,
url: url,
headers: requestHeaders,
startOrder: nStarted,
startedDateTime: startedDateTime,
time: time,
statistics: undefined,
tile: undefined,
endOrder: undefined,
extra: {isPending: true, isValid: false, isEmpty: false}
};
onPendingRequest(pendingEntry);
httpEntry.getContent(function (content, encoding) {
const responseHeaders = combineHeaders(httpEntry.response.headers);
const pendingEntryIndex = entries.indexOf(pendingEntry);
if (pendingEntryIndex === -1) {
return;
}
const isOk = httpEntry.response.status === 200;
const isNoContent = httpEntry.response.status === 204;
const isValid = isOk || isNoContent;
const layersStatistics = {};
const statistics = {layersCount: 0, featuresCount: 0, byLayers: layersStatistics /* featuresCount */};
const extra = {isPending: false, isValid: isValid, isEmpty: isNoContent};
function requestFinished() {
onFinishedRequest(pendingEntry, {
...pendingEntry,
statistics: statistics,
status: httpEntry.response.status,
tile: content,
tileSize: extra.isValid && data && data.length,
endOrder: ++endOrder,
extra: extra
})
}
function emptyRequestFinished() {
extra.isEmpty = true;
if (!trackEmptyResponse) {
removeEntry(pendingEntry);
}
requestFinished();
}
function notSuccessfulRequestFinished() {
extra.isValid = false;
if (trackOnlySuccessfulResponse) {
removeEntry(pendingEntry);
return;
}
requestFinished();
}
if (!extra.isValid) {
notSuccessfulRequestFinished();
return;
}
if (extra.isEmpty) {
emptyRequestFinished();
return;
}
const contentLengthHeader = Number(responseHeaders["content-length"] || responseHeaders["Content-Length"] || -1);
const data = Uint8Array.from(atob(content), c => c.charCodeAt(0));
/*Content-Length is not equal to actual bytes count*/
// noinspection EqualityComparisonWithCoercionJS
if (content == undefined || (contentLengthHeader !== -1 && data.length !== contentLengthHeader)) {
onWrongContent(pendingEntry, content, data, contentLengthHeader);
notSuccessfulRequestFinished();
return;
}
if (!data.length) {
emptyRequestFinished();
return;
}
let tile;
try {
tile = new VectorTile.VectorTile(new Pbf(data));
} catch (err) {
onWrongContent(pendingEntry, content, data, contentLengthHeader, err);
notSuccessfulRequestFinished();
return;
}
if (isTileEmpty(tile)) {
emptyRequestFinished();
return;
}
const layersNames = Object.keys(tile.layers);
layersNames.forEach((layerName) => {
const layer = tile.layers[layerName];
const layerStatistics = statistics.byLayers[layerName] = {};
layerStatistics.featuresCount = layer.length;
statistics.featuresCount += layer.length;
});
statistics.layersCount = layersNames.length;
requestFinished();
})
});
});