-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
57 lines (49 loc) · 1.55 KB
/
Copy pathscript.js
File metadata and controls
57 lines (49 loc) · 1.55 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
(function (xhr) {
let XHR = 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;
this._requestHeaders = {};
this._startTime = new Date().toISOString();
return open.apply(this, arguments);
};
XHR.setRequestHeader = function (header, value) {
this._requestHeaders[header] = value;
return setRequestHeader.apply(this, arguments);
};
XHR.send = function (postData) {
this.addEventListener("load", async function () {
if (postData) {
// Get request headers
if (typeof postData === "string") {
this._requestHeaders = postData;
}
}
try {
// Get request payload
if (
this._url.endsWith("FavoriteTweet") ||
this._url.endsWith("CreateBookmark")
) {
const requestPayload = JSON.parse(this._requestHeaders);
const tweetId = requestPayload.variables.tweet_id;
// Dispatch custom event i.e. send tweetId back to inject.js
const event = new CustomEvent("storeTweetId", {
detail: {
tweetId: tweetId,
type: this._url.endsWith("FavoriteTweet") ? "favorite" : "bookmark"
},
});
document.dispatchEvent(event);
}
} catch (err) {
console.error("Error parsing _requestHeaders.");
console.error(err);
}
});
return send.apply(this, arguments);
};
})(XMLHttpRequest);