-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinject.js
137 lines (112 loc) · 4.43 KB
/
inject.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
(function () {
const DONE = 4;
const IGNORED_TYPES = [
"MapShouldUpdateMessage",
"PlanCompletedMessage",
"EquipmentChangeMessage",
];
const ALWAYS_SHOWN_TYPES = [
"DifficultyRollFailureMessage",
"DifficultyRollSuccessMessage",
"ActionsRefreshedMessage",
"AreaChangeMessage",
"FateBranchCurrencyUsedMessage",
"FatePointChangeMessage",
"STORE_ITEM_CURRENCY_USED_MESSAGE", /// ??? WTF, Seamus?
"DeckRefreshedMessage",
];
const seenQualities = new Set();
function debug(message) {
console.debug(`[FL Quality Lantern] ${message}`);
}
function revealQualities(entry) {
let wasModified = false;
for (const quality of entry.qualityRequirements) {
if (quality.category === "Hidden") {
quality.category = "Story";
quality.name = "Hidden Quality";
const qualityId = quality.qualityId || quality.id;
if (!seenQualities.has(qualityId)) {
debug(`Hidden quality: ${quality.qualityName} (ID: ${qualityId})`);
seenQualities.add(qualityId);
}
wasModified = true;
}
}
return wasModified;
}
function parseResponse(response) {
if (this.readyState !== DONE) {
return;
}
let targetUrl = response.currentTarget.responseURL;
if (!targetUrl.includes("fallenlondon")) {
return;
}
if (!(targetUrl.includes("/storylet")
|| targetUrl.includes("/choosebranch"))) {
return;
}
let isModified = false;
let data = JSON.parse(response.target.responseText);
if (targetUrl.endsWith("/choosebranch")) {
if ("messages" in data) {
for (const message of data.messages) {
if (IGNORED_TYPES.includes(message["type"])) {
debug(`Ignoring message with type ${message["type"]}`);
continue;
}
const isHidden = !("priority" in message) || message.priority > 2;
if (isHidden && !ALWAYS_SHOWN_TYPES.includes(message["type"])) {
message.priority = 2;
message.message += "<em> (hidden)</em>";
isModified = true;
}
if (message["type"] === "QualityExplicitlySetMessage"
|| message["type"] === "StandardQualityChangeMessage") {
message.message += `<br>${message.possession.nameAndLevel}`;
isModified = true;
}
}
}
}
if (targetUrl.endsWith("/api/storylet")
|| targetUrl.endsWith("/api/storylet/goback")
|| targetUrl.endsWith("/api/storylet/begin")) {
if ("storylet" in data) {
isModified = revealQualities(data.storylet) || isModified;
if ("childBranches" in data.storylet) {
for (const branch of data.storylet.childBranches) {
isModified = revealQualities(branch) || isModified;
}
}
}
if ("storylets" in data) {
for (const storylet of data.storylets) {
isModified = revealQualities(storylet) || isModified;
}
}
}
if (isModified) {
setFakeXhrResponse(this, 200, JSON.stringify(data));
}
}
function openBypass(original_function) {
return function (method, url, async) {
this._targetUrl = url;
this.addEventListener("readystatechange", parseResponse);
return original_function.apply(this, arguments);
};
}
function setFakeXhrResponse(request, status, responseText) {
Object.defineProperty(request, 'responseText', {writable: true});
Object.defineProperty(request, 'readyState', {writable: true});
Object.defineProperty(request, 'status', {writable: true});
request.responseText = responseText;
request.readyState = DONE;
request.status = status;
request.onreadystatechange();
}
debug("Setting up API interceptors.");
XMLHttpRequest.prototype.open = openBypass(XMLHttpRequest.prototype.open);
}())