-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshift-handler.js
More file actions
307 lines (249 loc) · 12.6 KB
/
shift-handler.js
File metadata and controls
307 lines (249 loc) · 12.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
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
var browserApi = typeof browser !== 'undefined' ? browser : chrome;
var shiftHandlerOnMessage = async (message) => {
if (message.action === "heartbeat") {
return { status: "alive" };
}
if (message.action === "checkFinalResult") {
const pageText = document.body.textContent.toLowerCase();
const pageHtml = document.body.innerHTML.toLowerCase();
// Check if we're on the redemption results page
if (window.location.href.includes('code_redemptions') ||
window.location.href.includes('code-redemptions')) {
if (pageText.includes('successfully redeemed') ||
pageText.includes('your code was successfully') ||
pageText.includes('code was redeemed') ||
pageHtml.includes('successfully redeemed')) {
return { success: true, state: 'redeemed' };
}
if (pageText.includes('already been redeemed') ||
pageText.includes('already redeemed') ||
pageText.includes('shift code has already been redeemed')) {
return { success: false, state: 'checked' };
}
if (pageText.includes('expired')) {
return { success: false, state: 'expired' };
}
if (pageText.includes('invalid') || pageText.includes('not valid') || pageText.includes('does not exist')) {
return { success: false, state: 'invalid' };
}
}
// Check for alert messages on rewards page
const alertDiv = document.querySelector('.alert.notice');
if (alertDiv) {
const alertText = alertDiv.textContent.toLowerCase();
if (alertText.includes('successfully redeemed')) {
return { success: true, state: 'redeemed' };
}
if (alertText.includes('already been redeemed')) {
return { success: false, state: 'checked' };
}
if (alertText.includes('expired')) {
return { success: false, state: 'expired' };
}
if (alertText.includes('invalid') || alertText.includes('does not exist')) {
return { success: false, state: 'invalid' };
}
}
// Default to error if we can't determine the result
return { success: false, state: 'error', error: 'Could not determine result' };
}
if (message.action === "redeemCode") {
const code = message.code;
const game = message.game || 'tinytina';
const platforms = message.platforms || ['steam']; // Array of platforms to redeem
// Platform button selectors
const platformSelectors = {
steam: ['steam'],
xbox: ['xbox', 'microsoft'],
nintendo: ['nintendo', 'switch'],
epic: ['epic'],
psn: ['playstation', 'psn', 'ps4', 'ps5'],
stadia: ['stadia']
};
try {
// Clear previous results
let results = document.getElementById("code_results");
let currentHtml = results ? results.innerHTML.trim() : "";
if (results && currentHtml) {
results.innerHTML = "";
results.style.display = 'none';
}
// Step 1: Put the code in
const inputField = document.getElementById("shift_code_input");
if (!inputField) {
return { success: false, error: "Input field not found", state: "error" };
}
inputField.value = code;
inputField.dispatchEvent(new Event('input', { bubbles: true }));
inputField.dispatchEvent(new Event('change', { bubbles: true }));
inputField.dispatchEvent(new KeyboardEvent('keyup', { bubbles: true }));
// Step 2: Press check
const checkButton = document.getElementById("shift_code_check");
if (!checkButton) {
return { success: false, error: "Check button not found", state: "error" };
}
checkButton.removeAttribute('disabled');
checkButton.click();
// Step 3: Wait for result and see if there's an error or if it can be redeemed
const checkResult = await new Promise((resolve, reject) => {
let attempts = 0;
const checkForResult = () => {
attempts++;
if (attempts > 30) {
reject({ error: "Check took too long", state: "error" });
return;
}
const results = document.getElementById("code_results");
const currentHtml = results ? results.innerHTML.trim() : "";
if (!results || !currentHtml) {
setTimeout(checkForResult, 300);
return;
}
const resultText = results.innerHTML.toLowerCase();
// Check for various states
if (resultText.includes("expired")) {
resolve({ state: "expired" });
} else if (resultText.includes("already been redeemed")) {
resolve({ state: "checked" });
} else if (resultText.includes("invalid") || resultText.includes("not valid") || resultText.includes("does not exist")) {
resolve({ state: "invalid" });
} else if (results.querySelectorAll("h2").length > 0) {
resolve({ state: "can_redeem" });
} else if (resultText.includes("error")) {
resolve({ state: "error" });
} else {
setTimeout(checkForResult, 300);
}
};
checkForResult();
});
// If can't redeem, return the state immediately (no redirect happens)
if (checkResult.state !== "can_redeem") {
return { success: false, state: checkResult.state };
}
// Step 4: Find the game section
const elements = document.getElementById("code_results").querySelectorAll("h2");
// Resolve game label from config
const gameConfigs = (typeof SHIFT_CONFIG !== 'undefined' ? SHIFT_CONFIG.games : []);
const targetGame = gameConfigs.find(g => g.id === game) || gameConfigs.find(g => g.id === 'tinytina');
// Fallback for tests or missing config - though we expect config to be valid
const searchTerms = targetGame ? [targetGame.label] : ['Tiny Tina\'s Wonderlands'];
let gameElement = null;
for (const element of elements) {
const gameMatches = searchTerms.some(term =>
element.innerText.toLowerCase().includes(term.toLowerCase())
);
if (gameMatches) {
gameElement = element;
break;
}
}
if (!gameElement) {
return { success: false, error: "Game section not found", state: "error" };
}
// Step 5: Redeem for each selected platform
const redemptionResults = [];
for (const platform of platforms) {
const platformSearchTerms = platformSelectors[platform] || [platform];
let platformButton = null;
// Look for platform button in siblings after the game element
let currentElement = gameElement.nextElementSibling;
while (currentElement && !platformButton) {
// Find all potential buttons in this container
const buttons = currentElement.querySelectorAll('.redeem_button, input[type="submit"]');
for (const btn of buttons) {
const form = btn.closest('form');
const dataPlatform = form ? form.getAttribute('data-platform') : null;
const btnValue = btn.value || '';
// Check data-platform
if (dataPlatform && platformSearchTerms.some(term => dataPlatform.toLowerCase().includes(term.toLowerCase()))) {
platformButton = btn;
break;
}
// Check button value
if (platformSearchTerms.some(term => btnValue.toLowerCase().includes(term.toLowerCase()))) {
platformButton = btn;
break;
}
}
if (platformButton) break;
currentElement = currentElement.nextElementSibling;
}
if (!platformButton) {
redemptionResults.push({ platform, success: false, error: "Platform not available" });
continue;
}
// Keep clicking the platform button until it's gone
let clickAttempts = 0;
let buttonFound = true;
while (clickAttempts < 10 && buttonFound) {
clickAttempts++;
// Re-find the button (it might change after clicking)
currentElement = gameElement.nextElementSibling;
platformButton = null;
while (currentElement && !platformButton) {
const elementText = currentElement.innerHTML?.toLowerCase() || '';
const platformMatches = platformSearchTerms.some(term =>
elementText.includes(term.toLowerCase())
);
if (platformMatches) {
platformButton = currentElement.querySelector('.redeem_button, input[type="submit"]');
break;
}
currentElement = currentElement.nextElementSibling;
}
if (!platformButton) {
buttonFound = false;
break;
}
// Click the button
const form = platformButton.closest('form');
if (form) {
form.submit();
} else {
platformButton.click();
}
// Wait 1 second between pressing
await new Promise(resolve => setTimeout(resolve, 500));
}
redemptionResults.push({ platform, success: true, attempts: clickAttempts });
}
const anyPlatformSucceeded = redemptionResults.some(result => result.success);
if (!anyPlatformSucceeded) {
return {
success: false,
state: "invalid",
error: "Platform not available",
platforms: redemptionResults
};
}
// Step 6: All platforms processed - let popup handle the result checking
return { success: true, state: "submitted", platforms: redemptionResults };
} catch (error) {
console.error("Error redeeming code:", error);
return {
success: false,
error: error.error || error.message,
state: error.state || "error"
};
}
}
};
if (!globalThis.__shiftHandlerListenerAdded) {
globalThis.__shiftHandlerListenerAdded = true;
browserApi.runtime.onMessage.addListener((message, sender, sendResponse) => {
Promise.resolve(shiftHandlerOnMessage(message, sender))
.then((result) => {
sendResponse(result);
})
.catch((error) => {
sendResponse({
success: false,
state: "error",
error: error?.message || String(error)
});
});
// Keep the response channel open for async work (required by Chrome).
return true;
});
}