-
Notifications
You must be signed in to change notification settings - Fork 7
/
farmrpg-ext-bg.js
365 lines (343 loc) · 13.8 KB
/
farmrpg-ext-bg.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import { renderSidebar, renderSidebarFromGlobalState } from './lib/sidebar.js'
import { setupExplore } from './lib/explore.js'
import { RequestInterceptor } from './lib/pageFilter.js'
import syncFixtures from './lib/fixtures.js'
import { setupLog } from './lib/log.js'
import { setupLocations } from './lib/locations.js'
import { setupFishing } from './lib/fishing.js'
import { setupItems } from './lib/item.js'
import { fetchInventory, setupInventory } from './lib/inventory.js'
import { setupPets } from './lib/pets.js'
import { setupPlayer } from './lib/player.js'
import { setupFarm } from './lib/farm.js'
import { setupPerks, fetchPerks } from './lib/perks.js'
import { setupOrchard } from './lib/orchard.js'
import { setupWheel } from './lib/wheel.js'
import { setupWorkshop } from './lib/workshop.js'
import { setupAnimals } from './lib/animals.js'
import { setupSettings } from './lib/settings.js'
import { setupLocksmith } from './lib/locksmith.js'
import { setupProduction } from './lib/production.js'
import { setupVineyard } from './lib/vineyard.js'
import { setupQuests } from './lib/quests.js'
import { fetchEmblems, setupEmblems } from './lib/emblems.js'
import { fetchCommunityCenter, setupCommunityCenter } from './lib/communityCenter.js'
import { setupBorgens } from './lib/borgen.js'
import { fetchExchangeCenter, setupExchangeCenter } from './lib/exchange.js'
import { setupNpcFriendship } from './lib/npcFriendship.js'
/**
* @typedef {{
* db: idb.DB
* }} GlobalState
*/
class GlobalState {
constructor() {
this.requestInterceptor = new RequestInterceptor(this.logLatency.bind(this))
this.ports = []
this.clickHandlers = {}
this.postMessageHandlers = {}
}
addPageHandler(pageName, handler, options = {}) {
this.requestInterceptor.addPageHandler(pageName, async (page, url, parsedUrl) => {
if (options.parse) {
const parser = new DOMParser()
page = parser.parseFromString(page, "text/html")
url = parsedUrl
}
await handler(this, page, url, parsedUrl)
await renderSidebar(this)
})
}
addBeforePageHandler(pageName, handler) {
this.requestInterceptor.addBeforePageHandler(pageName, parsedUrl => handler(this, parsedUrl))
}
addWorkerHandler(workerGo, handler, options = {}) {
this.requestInterceptor.addWorkerHandler(workerGo, async (page, url, parsedUrl) => {
if (options.parse) {
const parser = new DOMParser()
page = parser.parseFromString(page, "text/html")
url = parsedUrl
}
await handler(this, page, url, parsedUrl)
await renderSidebar(this)
})
}
addBeforeWorkerHandler(workerGo, handler) {
this.requestInterceptor.addBeforeWorkerHandler(workerGo, parsedUrl => handler(this, parsedUrl))
}
addClickHandler(type, handler) {
this.clickHandlers[type] = handler
}
addPostMessageHandler(type, handler) {
this.postMessageHandlers[type] = handler
}
postMessage(msg) {
let lastError = null
for (const port of [...this.ports]) {
try {
port.postMessage(msg)
} catch(e) {
console.error(e)
lastError = e
// Remove this port.
port.disconnect() // Just in case?
const i = this.ports.indexOf(port)
if (i !== -1) {
this.ports.splice(i, 1)
}
// Keep trying the other ports.
}
}
if (lastError !== null) {
throw lastError
}
}
/**
* Log a latency recording.
* @param {number} ts
* @param {string} url
* @param {number} latency
*/
async logLatency(ts, url, latency) {
if (this.db !== undefined) {
if (false) {
await this.db.put("latency", {ts, url, latency})
}
}
}
/**
* Helper for other fetch* methods.
* @param {string} url
* @param {(state: GlobalState, page: string | Document, url: string | URL) => Promise<void>} handler
* @param {{parse: boolean, method?: string, beforeHandler: (state: GlobalState, url: URL) => Promise<void>}} options
*/
async fetchPage(url, handler, options = {}) {
const parsedUrl = new URL(url)
if (options.beforeHandler !== undefined) {
await options.beforeHandler(this, parsedUrl)
}
const resp = await fetch(url, {method: options.method || "GET"})
if (!resp.ok) {
throw `Error getting ${url}`
}
let page = await resp.text()
if (options.parse) {
const parser = new DOMParser()
const dom = parser.parseFromString(page, "text/html")
return await handler(this, dom, parsedUrl, parsedUrl)
} else {
return await handler(this, page, url, parsedUrl)
}
}
}
const globalState = new GlobalState()
const handleSidebarClick = async msg => {
console.log("sidebar click", msg)
const [targetType, targetArg] = msg.target.split(":", 2)
switch (targetType) {
case "farm":
if (globalState.player.farmID) {
globalState.postMessage({ action: "RELOAD_VIEW", url: `xfarm.php?id=${globalState.player.farmID}`})
} else {
console.log("Can't navigate to farm without Farm ID")
}
break
default:
if (globalState.clickHandlers[targetType]) {
await globalState.clickHandlers[targetType](globalState, targetType, targetArg, msg)
await renderSidebar(globalState)
}
break
}
}
const connectToContentScript = () =>
new Promise(resolve =>
browser.runtime.onConnect.addListener(port => {
port.onDisconnect.addListener(disPort => {
const i = globalState.ports.indexOf(disPort)
if (i !== -1) {
globalState.ports.splice(i, 1)
}
})
port.onMessage.addListener(msg => {
switch (msg.action) {
case "SIDEBAR_CLICK":
handleSidebarClick(msg)
break
default:
if (globalState.postMessageHandlers[msg.action]) {
globalState.postMessageHandlers[msg.action](globalState, msg)
}
break
}
})
globalState.ports.push(port)
// Get something rendered at least.
renderSidebar(globalState)
// The first time we get a connection, let the promise resolve.
// This means we can block until we get at least one connection.
if (resolve) {
resolve(port)
resolve = null
}
})
)
const main = async () => {
console.log("FarmRPG-Ext loaded (background)!")
window.globalState = globalState
await connectToContentScript()
// A debugging helper to quickly clear the idb state.
window.resetdb = async () => {
if (globalState.db) {
globalState.db.close()
}
await idb.deleteDB("farmrpg-ext")
}
// Make sure the DB is persisted.
const persist = await navigator.storage.persist()
if (!persist) {
throw "Unable to set persist mode for storage"
}
// Initialize the database.
globalState.db = await idb.openDB("farmrpg-ext", 8, {
upgrade(db, oldVer) {
switch(oldVer) {
case 0:
console.log("Running DB migrations for version 1")
db.createObjectStore("log", { keyPath: "id", autoIncrement: true })
const items = db.createObjectStore("items", { keyPath: "name" })
items.createIndex("byImage", "image", {unique: false})
items.createIndex("byID", "id", {unique: true})
const locations = db.createObjectStore("locations", { keyPath: ["type", "name"] })
locations.createIndex("byID", ["type", "id"], {unique: true})
case 1:
console.log("Running DB migrations for version 2")
const pets = db.createObjectStore("pets", { keyPath: "name" })
pets.createIndex("byID", "id", {unique: true})
db.createObjectStore("player", { keyPath: "id", autoIncrement: true })
case 2:
console.log("Running DB migrations for version 3")
db.createObjectStore("quests", { keyPath: "id" })
case 3:
console.log("Running DB migrations for version 4")
db.createObjectStore("latency", { keyPath: ["ts", "url"] })
case 4:
console.log("Running DB migrations for version 5")
db.createObjectStore("communityCenter", { keyPath: "date" })
db.createObjectStore("emblems", { keyPath: "id" })
case 5:
console.log("Running DB migrations for version 6")
db.createObjectStore("borgens", { keyPath: "date" })
case 6:
console.log("Running DB migrations for version 7")
db.createObjectStore("exchangeCenter", { keyPath: ["date", "giveItem", "receiveItem"] })
case 7:
console.log("Running DB migrations for version 8")
// Fix the bad index structure we had before.
db.deleteObjectStore("items")
const items2 = db.createObjectStore("items", { keyPath: "id" })
items2.createIndex("byImage", "image", {unique: false})
items2.createIndex("byName", "name", {unique: false})
}
},
})
setupLog(globalState)
await syncFixtures(globalState.db)
const itemCount = await globalState.db.count("items")
const locationCount = await globalState.db.count("locations")
const logCount = await globalState.db.count("log")
const petsCount = await globalState.db.count("pets")
const questsCount = await globalState.db.count("quests")
console.log(`Database loaded, items ${itemCount} locations ${locationCount} pets ${petsCount} quests ${questsCount} log ${logCount}`)
// Munge outgoing requests to fix the origin and referer headers.
browser.webRequest.onBeforeSendHeaders.addListener(
details => {
if (!details.originUrl.startsWith(`moz-extension://`)) {
return
}
let sawReferer = false
for(const header of details.requestHeaders) {
if (header.name.toLowerCase() === "origin") {
header.value = "https://farmrpg.com"
} else if (header.name.toLowerCase() === "referer") {
header.value = "https://farmrpg.com/index.php"
sawReferer = true
}
}
if (!sawReferer) {
details.requestHeaders.push({name: "Referer", value: "https://farmrpg.com/index.php"})
}
return {requestHeaders: details.requestHeaders}
},
{ urls: ["*://*.farmrpg.com/*"] },
["blocking", "requestHeaders"]
)
// Setup page filters for data capture.
await setupPlayer(globalState)
setupItems(globalState)
setupLocations(globalState)
setupInventory(globalState)
setupPets(globalState)
setupExplore(globalState)
setupFishing(globalState)
setupFarm(globalState)
setupPerks(globalState)
setupOrchard(globalState)
setupWheel(globalState)
setupWorkshop(globalState)
setupAnimals(globalState)
setupSettings(globalState)
setupLocksmith(globalState)
setupProduction(globalState)
setupVineyard(globalState)
setupQuests(globalState)
setupEmblems(globalState)
setupCommunityCenter(globalState)
setupBorgens(globalState)
setupExchangeCenter(globalState)
setupNpcFriendship(globalState)
// Kick off some initial data population.
renderSidebarFromGlobalState()
fetchInventory(globalState).then(renderSidebarFromGlobalState)
fetchPerks(globalState).then(() => {
console.log("Found initial perkset", globalState.player.currentPerkset)
renderSidebarFromGlobalState()
})
// Set up a periodic refresh of the inventory.
browser.alarms.create("inventory-refresh", {periodInMinutes: 1})
browser.alarms.create("perk-refresh", {periodInMinutes: 15})
// browser.alarms.create("render-sidebar", {periodInMinutes: 1})
browser.alarms.create("clear-latency", {periodInMinutes: 60})
browser.alarms.create("community-center-refresh", {
periodInMinutes: 60*24,
when: luxon.DateTime.fromObject({}, {zone: "America/Chicago"}).startOf("day").plus({day: 1}).minus({minutes: 30}).toMillis(),
})
browser.alarms.onAlarm.addListener(async alarm => {
switch (alarm.name) {
case "inventory-refresh":
await fetchInventory(globalState)
await renderSidebarFromGlobalState()
break
case "perk-refresh":
await fetchPerks(globalState)
await renderSidebarFromGlobalState()
break
case "render-sidebar":
await renderSidebarFromGlobalState()
break
case "clear-latency":
// Delete all but the last 24 hours of data.
// await globalState.db.delete("latency", IDBKeyRange.upperBound(Date.now() - 24*60*60*1000))
await fetchCommunityCenter(globalState)
await fetchExchangeCenter(globalState)
await fetchEmblems(globalState)
break
case "community-center-refresh":
await fetchCommunityCenter(globalState)
await fetchExchangeCenter(globalState)
await fetchEmblems(globalState)
break
}
})
}
main()