-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontent.ts
More file actions
352 lines (293 loc) · 8.54 KB
/
content.ts
File metadata and controls
352 lines (293 loc) · 8.54 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
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
import "./content.css"
export const config = {
matches: [
"https://store.steampowered.com/app/*",
"https://store.steampowered.com/agecheck/app/*"
]
}
const CHIP_ATTR = "data-steamlib-chip"
const CHIP_CLASS = "goodlib-chip"
const CHIPS_WRAP_ATTR = "data-steamlib-chip-wrap"
const GOG_ENABLED_KEY = "gogEnabled"
const GOG_GAMES_ENABLED_KEY = "gogGamesEnabled"
const FITGIRL_ENABLED_KEY = "fitgirlEnabled"
const DODI_ENABLED_KEY = "dodiEnabled"
const BYXATAB_ENABLED_KEY = "byxatabEnabled"
const STEAMRIP_ENABLED_KEY = "steamripEnabled"
const OVA_GAMES_ENABLED_KEY = "ovaGamesEnabled"
const CS_RIN_ENABLED_KEY = "csrinEnabled"
const steamTitleSelectors = [
"#appHubAppName",
".apphub_AppName",
"div.apphub_AppName"
]
const normalizeText = (value: string) => value.replace(/\s+/g, " ").trim()
const getGogSearchQuery = (query: string) =>
normalizeText(query.replace(/[™®©]/g, " ").replace(/[’]/g, "'"))
const getGameTitle = (): HTMLElement | null => {
for (const selector of steamTitleSelectors) {
const nodes = document.querySelectorAll(selector)
for (let index = 0; index < nodes.length; index += 1) {
const node = nodes[index]
if (!(node instanceof HTMLElement)) continue
const text = normalizeText(node.textContent ?? "")
if (text.length === 0) continue
if (node.offsetParent === null) continue
return node
}
}
return null
}
const getCleanGameTitle = (title: HTMLElement): string => {
const clone = title.cloneNode(true) as HTMLElement
const injectedWrap = clone.querySelector(`[${CHIPS_WRAP_ATTR}]`)
if (injectedWrap) {
injectedWrap.remove()
}
return normalizeText(clone.textContent ?? "")
}
const removeChip = () => {
const chips = document.querySelectorAll(`[${CHIP_ATTR}]`)
chips.forEach((chip) => chip.remove())
const wrap = document.querySelector(`[${CHIPS_WRAP_ATTR}]`)
if (wrap && wrap.childElementCount === 0) {
wrap.remove()
}
}
type SourceKey =
| "gog"
| "goggames"
| "fitgirl"
| "dodi"
| "byxatab"
| "steamrip"
| "ovagames"
| "csrin"
const sourceKeys: SourceKey[] = [
"gog",
"goggames",
"fitgirl",
"dodi",
"byxatab",
"steamrip",
"ovagames",
"csrin"
]
const sourceMeta: Record<
SourceKey,
{ glyph: string; label: string; storageKey: string }
> = {
gog: {
label: "GOG",
glyph: "G",
storageKey: GOG_ENABLED_KEY
},
goggames: {
label: "GOG Games",
glyph: "GG",
storageKey: GOG_GAMES_ENABLED_KEY
},
fitgirl: {
label: "FitGirl",
glyph: "FG",
storageKey: FITGIRL_ENABLED_KEY
},
dodi: {
label: "DODI",
glyph: "D",
storageKey: DODI_ENABLED_KEY },
byxatab: {
label: "ByXatab",
glyph: "BX",
storageKey: BYXATAB_ENABLED_KEY
},
steamrip: {
label: "SteamRIP",
glyph: "SR",
storageKey: STEAMRIP_ENABLED_KEY
},
ovagames: {
label: "OVA Games",
glyph: "OVA",
storageKey: OVA_GAMES_ENABLED_KEY
},
csrin: {
label: "CS.RIN.RU",
glyph: "CS",
storageKey: CS_RIN_ENABLED_KEY
}
}
const defaultEnabledBySource: Record<SourceKey, boolean> = {
gog: true,
goggames: false,
fitgirl: false,
dodi: false,
byxatab: false,
steamrip: false,
ovagames: false,
csrin: false
}
const buildSourceUrl = (source: SourceKey, query: string) => {
const encoded = encodeURIComponent(query)
if (source === "gog") {
const gogQuery = encodeURIComponent(getGogSearchQuery(query))
return `https://www.gog.com/en/games?query=${gogQuery}&order=desc:score`
}
if (source === "goggames") {
const gogGamesQuery = encodeURIComponent(getGogSearchQuery(query))
return `https://gog-games.to/?search=${gogGamesQuery}`
}
if (source === "dodi") {
return `https://dodi-repacks.site/?s=${encoded}`
}
if (source === "byxatab") {
return `https://byxatab.com/index.php?do=search&subaction=search&story=${encoded}`
}
if (source === "steamrip") {
return `https://steamrip.com/?s=${encoded}`
}
if (source === "ovagames") {
return `https://www.ovagames.com/?s=${encoded}`
}
if (source === "csrin") {
return `https://cs.rin.ru/forum/search.php?keywords=${encoded}&terms=all&author=&sc=1&sf=titleonly&sr=topics&sk=t&sd=d&st=0&ch=300&t=0&submit=Search`
}
return `https://fitgirl-repacks.site/?s=${encoded}`
}
const makeChip = (source: SourceKey, searchQuery: string) => {
const chip = document.createElement("span")
chip.setAttribute(CHIP_ATTR, source)
chip.className = `${CHIP_CLASS} ${CHIP_CLASS}--${source}`
chip.setAttribute("data-search-query", searchQuery)
const glyph = sourceMeta[source].glyph
const glyphClass =
glyph.length > 1 ? "goodlib-chip-glyph goodlib-chip-glyph--wide" : "goodlib-chip-glyph"
const icon = document.createElement("span")
icon.className = "goodlib-chip-icon"
const glyphNode = document.createElement("span")
glyphNode.className = glyphClass
glyphNode.textContent = glyph
icon.appendChild(glyphNode)
const label = document.createElement("span")
label.className = "goodlib-chip-label"
label.textContent = sourceMeta[source].label
chip.replaceChildren(icon, label)
chip.addEventListener("click", () => {
const query = chip.getAttribute("data-search-query") ?? searchQuery
window.open(buildSourceUrl(source, query), "_blank", "noopener,noreferrer")
})
return chip
}
const injectChips = (enabledBySource: Record<SourceKey, boolean>) => {
const title = getGameTitle()
if (!title) return
const gameTitle = getCleanGameTitle(title)
if (!gameTitle) return
let wrap = title.querySelector(`[${CHIPS_WRAP_ATTR}]`)
if (!(wrap instanceof HTMLElement)) {
wrap = document.createElement("span")
wrap.setAttribute(CHIPS_WRAP_ATTR, "true")
wrap.className = "goodlib-chip-wrap"
title.appendChild(wrap)
}
const orderedChips: HTMLElement[] = []
for (const source of sourceKeys) {
let chip = wrap.querySelector(`[${CHIP_ATTR}="${source}"]`)
if (!(chip instanceof HTMLElement) && enabledBySource[source]) {
chip = makeChip(source, gameTitle)
}
if (chip instanceof HTMLElement && enabledBySource[source]) {
chip.setAttribute("data-search-query", gameTitle)
orderedChips.push(chip)
}
}
const currentOrder = Array.from(wrap.children).filter(
(node) => node instanceof HTMLElement && node.hasAttribute(CHIP_ATTR)
)
const needsReorder =
currentOrder.length !== orderedChips.length ||
currentOrder.some((node, index) => node !== orderedChips[index])
if (needsReorder) {
wrap.replaceChildren(...orderedChips)
}
}
const enabledBySource: Record<SourceKey, boolean> = { ...defaultEnabledBySource }
const syncChipToState = () => {
if (sourceKeys.every((source) => !enabledBySource[source])) {
removeChip()
return
}
injectChips(enabledBySource)
}
const initializeEnabledState = () => {
chrome.storage.sync.get(
sourceKeys.map((source) => sourceMeta[source].storageKey),
(result) => {
for (const source of sourceKeys) {
const storedValue = result[sourceMeta[source].storageKey]
enabledBySource[source] =
typeof storedValue === "boolean"
? storedValue
: defaultEnabledBySource[source]
}
syncChipToState()
}
)
}
let injectTimeout: ReturnType<typeof setTimeout> | null = null
let lastUrl = window.location.href
const handleDomChange = () => {
if (sourceKeys.every((source) => !enabledBySource[source])) {
return
}
if (injectTimeout) {
clearTimeout(injectTimeout)
}
injectTimeout = setTimeout(() => {
if (lastUrl !== window.location.href) {
lastUrl = window.location.href
removeChip()
}
injectChips(enabledBySource)
}, 120)
}
const startObservers = () => {
if (!(document.body instanceof HTMLElement)) {
return
}
const observer = new MutationObserver(handleDomChange)
observer.observe(document.body, { childList: true, subtree: true })
setInterval(() => {
if (lastUrl !== window.location.href) {
handleDomChange()
}
}, 500)
}
initializeEnabledState()
if (document.readyState === "loading") {
document.addEventListener(
"DOMContentLoaded",
() => {
startObservers()
handleDomChange()
},
{ once: true }
)
} else {
startObservers()
handleDomChange()
}
chrome.storage.onChanged.addListener((changes, areaName) => {
if (areaName !== "sync") return
for (const source of sourceKeys) {
const change = changes[sourceMeta[source].storageKey]
if (!change) {
continue
}
enabledBySource[source] =
typeof change.newValue === "boolean"
? change.newValue
: defaultEnabledBySource[source]
}
syncChipToState()
})