1 | let network;
|
2 | export function getNetwork() {
|
3 | return network;
|
4 | }
|
5 | export function setNetwork(newNetwork) {
|
6 | network = newNetwork;
|
7 | }
|
8 | let dom;
|
9 | export function getDOM() {
|
10 | return dom;
|
11 | }
|
12 | export function setDOM(newDOM) {
|
13 | dom = newDOM;
|
14 | }
|
15 | let css;
|
16 | export function getCSS() {
|
17 | return css;
|
18 | }
|
19 | export function setCSS(newCSS) {
|
20 | css = newCSS;
|
21 | }
|
22 | export var NetworkAgent;
|
23 | (function (NetworkAgent) {
|
24 | function responseReceived(requestId, result, headers) {
|
25 | const requestIdStr = requestId.toString();
|
26 |
|
27 | const mimeType = headers['Content-Type'] || headers['content-type'] || 'application/octet-stream';
|
28 | const contentLengthHeader = headers['Content-Length'] || headers['content-length'];
|
29 | let contentLength = parseInt(contentLengthHeader, 10);
|
30 | if (isNaN(contentLength)) {
|
31 | contentLength = 0;
|
32 | }
|
33 | const response = {
|
34 | url: result.url || '',
|
35 | status: result.statusCode,
|
36 | statusText: result.statusText || '',
|
37 | headers: headers,
|
38 | mimeType: mimeType,
|
39 | fromDiskCache: false,
|
40 | connectionReused: true,
|
41 | connectionId: 0,
|
42 | encodedDataLength: contentLength,
|
43 | securityState: 'info',
|
44 | };
|
45 | const responseData = {
|
46 | requestId: requestIdStr,
|
47 | type: mimeTypeToType(response.mimeType),
|
48 | response: response,
|
49 | timestamp: getTimeStamp(),
|
50 | };
|
51 | global.__inspector.responseReceived(responseData);
|
52 | global.__inspector.loadingFinished({
|
53 | requestId: requestIdStr,
|
54 | timestamp: getTimeStamp(),
|
55 | encodedDataLength: contentLength,
|
56 | });
|
57 | const hasTextContent = responseData.type === 'Document' || responseData.type === 'Script';
|
58 | let data;
|
59 | if (!hasTextContent) {
|
60 | if (responseData.type === 'Image') {
|
61 | const bitmap = result.responseAsImage;
|
62 | if (bitmap) {
|
63 | const outputStream = new java.io.ByteArrayOutputStream();
|
64 | bitmap.compress(android.graphics.Bitmap.CompressFormat.PNG, 100, outputStream);
|
65 | const base64Image = android.util.Base64.encodeToString(outputStream.toByteArray(), android.util.Base64.DEFAULT);
|
66 | data = base64Image;
|
67 | }
|
68 | }
|
69 | }
|
70 | else {
|
71 | data = result.responseAsString;
|
72 | }
|
73 | const successfulRequestData = {
|
74 | requestId: requestIdStr,
|
75 | data: data,
|
76 | hasTextContent: hasTextContent,
|
77 | };
|
78 | global.__inspector.dataForRequestId(successfulRequestData);
|
79 | }
|
80 | NetworkAgent.responseReceived = responseReceived;
|
81 | function requestWillBeSent(requestId, options) {
|
82 | const request = {
|
83 | url: options.url,
|
84 | method: options.method,
|
85 | headers: options.headers || {},
|
86 | postData: options.content ? options.content.toString() : '',
|
87 | initialPriority: 'Medium',
|
88 | referrerPolicy: 'no-referrer-when-downgrade',
|
89 | };
|
90 | const requestData = {
|
91 | requestId: requestId.toString(),
|
92 | url: request.url,
|
93 | request: request,
|
94 | timestamp: getTimeStamp(),
|
95 | type: 'Document',
|
96 | wallTime: 0,
|
97 | };
|
98 | global.__inspector.requestWillBeSent(requestData);
|
99 | }
|
100 | NetworkAgent.requestWillBeSent = requestWillBeSent;
|
101 | function getTimeStamp() {
|
102 | const d = new Date();
|
103 | return Math.round(d.getTime() / 1000);
|
104 | }
|
105 | function mimeTypeToType(mimeType) {
|
106 | let type = 'Document';
|
107 | if (mimeType) {
|
108 | if (mimeType.indexOf('image') === 0) {
|
109 | type = 'Image';
|
110 | }
|
111 | else if (mimeType.indexOf('javascript') !== -1 || mimeType.indexOf('json') !== -1) {
|
112 | type = 'Script';
|
113 | }
|
114 | }
|
115 | return type;
|
116 | }
|
117 | })(NetworkAgent || (NetworkAgent = {}));
|
118 |
|
\ | No newline at end of file |