-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathinject.ts
51 lines (49 loc) · 1.72 KB
/
inject.ts
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
//@ts-ignore
(function (xhr) {
// TODO: find a way to make this cleaner
const RequestRegex =
/^https?:\/\/(?:\w+\.)?(?:twitter|x)\.com\/[\w\/\.\-\_\=]+\/(HomeLatestTimeline|HomeTimeline|Followers|Following|SearchTimeline|UserTweets|Favoriters|Retweeters|UserCreatorSubscriptions|FollowersYouKnow|BlueVerifiedFollowers|UserByScreenName|timeline\/home\.json|TweetDetail|ModeratedTimeline|recommendations\.json|search\/typeahead\.json|search\/adaptive\.json|blocks\/destroy\.json|mutes\/users\/destroy\.json)(?:$|\?)/;
let XHR = <BlueBlockerXLMRequest>XMLHttpRequest.prototype;
let open = XHR.open;
let send = XHR.send;
let setRequestHeader = XHR.setRequestHeader;
XHR.open = function (method, url) {
this._method = method;
this._url = url.toString();
this._requestHeaders = {};
this._startTime = new Date().toISOString();
// TODO: remove this ignore
//@ts-ignore
return open.apply(this, arguments);
};
XHR.setRequestHeader = function (header, value) {
this._requestHeaders[header] = value;
// TODO: remove this ignore
//@ts-ignore
return setRequestHeader.apply(this, arguments);
};
XHR.send = function (postData) {
this.addEventListener('load', () => {
// determine if request is a timeline/tweet-returning request
const parsedUrl = RequestRegex.exec(this._url);
if (this._url && parsedUrl && parsedUrl.length > 0) {
document.dispatchEvent(
new CustomEvent('blue-blocker-event', {
detail: {
parsedUrl,
url: this._url,
body: this.response,
request: {
headers: this._requestHeaders,
},
status: this.status,
},
}),
);
}
});
// TODO: remove this ignore
//@ts-ignore
return send.apply(this, arguments);
};
})(XMLHttpRequest);