Repository navigation
Expand file tree
/
Copy pathbackground.ts
More file actions
129 lines (112 loc) · 3.6 KB
/
Copy pathbackground.ts
File metadata and controls
129 lines (112 loc) · 3.6 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
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
import { storageService } from "./services/storage"
import type { TradeLocationHistoryStruct } from "./types/trade-location"
let registered = false
let historyWriteChain: Promise<void> = Promise.resolve()
type PoeNinjaRequest = {
query: "poe-ninja-exchange";
game: "poe1" | "poe2";
resource: string;
};
type PoeNinjaItemRequest = {
query: "poe-ninja-item";
resource: string;
};
type LogHistoryRequest = {
query: "log-trade-history";
key: string;
entry: TradeLocationHistoryStruct;
max: number;
};
type BackgroundRequest = PoeNinjaRequest | PoeNinjaItemRequest | LogHistoryRequest;
const isSameHistoryLocation = (
a: TradeLocationHistoryStruct | undefined,
b: TradeLocationHistoryStruct
) =>
!!a &&
a.version === b.version &&
a.league === b.league &&
a.slug === b.slug &&
a.type === b.type
const isBackgroundRequest = (request: unknown): request is BackgroundRequest => {
if (!request || typeof request !== "object") {
return false;
}
const candidate = request as Record<string, unknown>;
if (candidate.query === "poe-ninja-exchange") {
return (
(candidate.game === "poe1" || candidate.game === "poe2") &&
typeof candidate.resource === "string" &&
candidate.resource.startsWith("/exchange/current/overview?")
);
}
if (candidate.query === "poe-ninja-item") {
return typeof candidate.resource === "string" &&
/^\/stash\/current\/item\/overview\?type=Unique(?:Weapon|Armour|Accessory|Flask|Jewel)&league=[^&]+$/.test(candidate.resource);
}
return candidate.query === "log-trade-history"
&& typeof candidate.key === "string"
&& candidate.key.startsWith("trade-history")
&& typeof candidate.max === "number"
&& Number.isInteger(candidate.max)
&& candidate.max > 0
&& candidate.max <= 100
&& !!candidate.entry
&& typeof candidate.entry === "object";
};
export const registerBackgroundHandlers = () => {
if (registered) {
return
}
registered = true
chrome.runtime.onMessage.addListener((request, _sender, sendResponse) => {
if (!isBackgroundRequest(request)) {
return false;
}
if (request.query === "log-trade-history") {
historyWriteChain = historyWriteChain
.then(async () => {
const history =
(await storageService.getValue<TradeLocationHistoryStruct[]>(
request.key
)) || []
if (!isSameHistoryLocation(history[0], request.entry)) {
history.unshift(reque
5714
st.entry)
await storageService.setValue(
request.key,
history.slice(0, request.max)
)
}
sendResponse({ logged: true })
})
.catch((error) => {
console.error("[Poe Trade Plus-BG] history write failed:", error)
sendResponse({ logged: false })
})
return true
}
if (request.query === "poe-ninja-exchange" || request.query === "poe-ninja-item") {
const url = request.query === "poe-ninja-exchange"
? `https://poe.ninja/${request.game}/api/economy${request.resource}`
: `https://poe.ninja/poe1/api/economy${request.resource}`
fetch(url)
.then(async (r) => {
if (!r.ok) {
throw new Error(`poe.ninja responded with status ${r.status}`)
}
return r.json()
})
.then((response) => {
sendResponse(response)
})
.catch((err) => {
console.error("[Poe Trade Plus-BG] poe.ninja exchange fetch failed:", {
url,
error: err
})
sendResponse(null)
})
return true
}
return false
})
}