-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnet_hook.js
More file actions
103 lines (96 loc) · 2.91 KB
/
Copy pathnet_hook.js
File metadata and controls
103 lines (96 loc) · 2.91 KB
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
(() => {
try {
const FLAG = "__JM_SLOW_CRAWLER_NET_MONITOR__";
if (window[FLAG]) return;
window[FLAG] = true;
const normUrl = (u) => {
try {
return new URL(String(u || ""), location.href).href;
} catch {
return String(u || "");
}
};
const report = (rec) => {
try {
window.postMessage({ __jmSlowCrawler: true, type: "net_error", rec }, "*");
} catch {}
};
// fetch
try {
const origFetch = window.fetch;
if (typeof origFetch === "function") {
window.fetch = function (input, init) {
let url = "";
let method = "GET";
try {
url =
typeof input === "string"
? input
: input && input.url
? input.url
: "";
method = (init && init.method) || (input && input.method) || "GET";
} catch {}
const absUrl = normUrl(url);
const m = String(method || "GET").toUpperCase();
return origFetch
.apply(this, arguments)
.then((res) => {
try {
const st = res && typeof res.status === "number" ? res.status : 0;
if (st >= 400 && absUrl.includes("/thread/")) {
report({ time: Date.now(), url: absUrl, status: st, method: m });
}
} catch {}
return res;
})
.catch((err) => {
try {
if (absUrl.includes("/thread/")) {
report({
time: Date.now(),
url: absUrl,
status: 0,
method: m,
err: String(err),
});
}
} catch {}
throw err;
});
};
}
} catch {}
// XHR
try {
const origOpen = XMLHttpRequest.prototype.open;
const origSend = XMLHttpRequest.prototype.send;
XMLHttpRequest.prototype.open = function (method, url) {
try {
this.__jm_method = method;
this.__jm_url = url;
} catch {}
return origOpen.apply(this, arguments);
};
XMLHttpRequest.prototype.send = function () {
try {
this.addEventListener(
"loadend",
() => {
try {
const abs = normUrl(this.__jm_url || "");
const st = typeof this.status === "number" ? this.status : 0;
const m = String(this.__jm_method || "GET").toUpperCase();
if (st >= 400 && abs.includes("/thread/")) {
report({ time: Date.now(), url: abs, status: st, method: m });
}
} catch {}
},
{ once: true }
);
} catch {}
return origSend.apply(this, arguments);
};
} catch {}
} catch {}
})();