Repository navigation
Expand file tree
/
Copy pathinterceptor.js
More file actions
287 lines (257 loc) · 9.26 KB
/
Copy pathinterceptor.js
File metadata and controls
287 lines (257 loc) · 9.26 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
// PrepPush - interceptor.js (MAIN world)
// Observes HackerRank's own /submissions/{id} polls — no extra API requests from us.
// Contest/prep-kit pages: XHR only (fetch trap breaks compile on those flows).
(function () {
if (window.__preppushInjected) return;
window.__preppushInjected = true;
const frameLabel = window.top === window ? 'top' : 'child';
const path = window.top?.location?.pathname || window.location.pathname;
const isContestTrack = /\/contests\/[^/]+\/challenges\//.test(path);
console.log(
'%c[PrepPush] Interceptor active (' +
frameLabel +
(isContestTrack ? ', contest track, XHR-only' : '') +
')',
'color: #22c55e; font-weight: bold;'
);
function resolveUrl(url) {
if (!url) return '';
const s = String(url);
try {
return s.startsWith('http') ? s : new URL(s, location.origin).href;
} catch {
return s;
}
}
function isSubmissionPollURL(url) {
const full = resolveUrl(url);
if (!full || full.includes('/submissions/code')) return false;
const pathOnly = full.split('?')[0];
return /\/submissions\/\d+\/?$/.test(pathOnly);
}
/** Run Code: few sample tests, no full solution code in poll body. */
function isRunEvaluation(model) {
const code = (model.code ?? '').trim();
const n = model.testcase_status?.length ?? 0;
const statusStr = String(model.status ?? '').trim();
const statusCode = model.status_code ?? model.statusCode;
if (/^accepted$/i.test(statusStr) && code.length > 20) return false;
if (statusCode === 1 && code.length > 20) return false;
if ((model.solved === 1 || model.solved === true) && code.length > 20 && n > 3) {
return false;
}
if (n > 3) return false;
if (!code && n > 0 && n <= 3) return true;
if (statusStr === '1' && statusCode !== 1 && code.length < 20) return true;
return false;
}
/** Submit Code: full evaluation with solution code (prep-kit + classic). */
function isFullSubmitEvaluation(model) {
if (!model || typeof model !== 'object') return false;
if (isRunEvaluation(model)) return false;
const status = String(model.status ?? '').trim();
const statusCode = model.status_code ?? model.statusCode;
const code = (model.code ?? '').trim();
if (code.length < 20) return false;
if (/processing|compil|running|queue|pending|wrong|fail|error|timeout/i.test(status)) {
return false;
}
if (/^accepted$/i.test(status) && (model.solved === 1 || model.solved === true)) return true;
if (statusCode === 1 && (model.solved === 1 || model.solved === true)) return true;
if (statusCode === 2 && /^processed$/i.test(status) && model.solved === 1) return true;
return false;
}
function modelTimestamp(model) {
const raw = model.updated_at || model.created_at;
return raw ? new Date(raw).getTime() : Date.now();
}
function buildSubmission(model) {
const code = model.code ?? '';
if (!code || code.trim().length < 20) return null;
const pagePath = window.top?.location?.pathname || window.location.pathname;
const slug =
model.challenge_slug ??
model.slug ??
(pagePath.match(/\/challenges\/([^/]+)/) || [])[1] ??
'unknown-problem';
const contestMatch = pagePath.match(/\/contests\/([^/]+)\//);
return {
status: model.status || 'Accepted',
language: model.language ?? null,
problemName: model.name ?? null,
problemSlug: slug,
contestSlug:
model.contest_slug ??
(contestMatch ? contestMatch[1] : 'master'),
submissionId: model.id ?? null,
timestamp: modelTimestamp(model),
code,
solved: model.solved,
statusCode: model.status_code ?? model.statusCode,
testcaseCount: model.testcase_status?.length ?? 0,
isFullSubmit: true,
};
}
function emitAccepted(submission) {
const id = String(submission.submissionId ?? '');
if (!id) return;
if (!window.__preppushEmittedIds) window.__preppushEmittedIds = new Set();
if (window.__preppushEmittedIds.has(id)) return;
window.__preppushEmittedIds.add(id);
console.log(
'[PrepPush] Captured Accepted id=%s (%d chars) [%s]',
submission.submissionId,
submission.code.length,
frameLabel
);
const msg = { source: 'preppush', type: 'PREPPUSH_ACCEPTED', submission };
try {
window.postMessage(msg, '*');
if (window.top && window.top !== window) window.top.postMessage(msg, '*');
} catch {
/* ignore */
}
document.dispatchEvent(
new CustomEvent('preppush:accepted', { detail: submission, bubbles: true })
);
}
function emitWatch(submissionId) {
if (!submissionId) return;
const id = String(submissionId);
if (!window.__preppushWatchEmitted) window.__preppushWatchEmitted = new Set();
if (window.__preppushWatchEmitted.has(id)) return;
window.__preppushWatchEmitted.add(id);
const msg = { source: 'preppush', type: 'PREPPUSH_WATCH', submissionId: id };
try {
window.postMessage(msg, '*');
if (window.top && window.top !== window) window.top.postMessage(msg, '*');
} catch {
/* ignore */
}
}
function tryHandlePollResponse(url, responseText) {
if (!isSubmissionPollURL(url) || !responseText) return;
try {
const data = JSON.parse(responseText);
const model = data?.model ?? data;
if (!model) return;
const id = model.id ?? (url.match(/\/submissions\/(\d+)/) || [])[1];
const status = String(model.status ?? '').trim();
if (/processing|running|in queue|compil/i.test(status)) {
if (id) emitWatch(id);
return;
}
if (!isFullSubmitEvaluation(model)) return;
const submission = buildSubmission(model);
if (!submission) return;
emitAccepted(submission);
} catch {
/* ignore */
}
}
function installXhrObserver() {
if (window.__preppushXhrObserver) return;
window.__preppushXhrObserver = true;
const origOpen = XMLHttpRequest.prototype.open;
const origSend = XMLHttpRequest.prototype.send;
XMLHttpRequest.prototype.open = function (method, url, ...rest) {
this.__preppushUrl = url;
return origOpen.call(this, method, url, ...rest);
};
XMLHttpRequest.prototype.send = function (...args) {
const url = this.__preppushUrl;
if (isSubmissionPollURL(url)) {
this.addEventListener(
'load',
function () {
tryHandlePollResponse(url, this.responseText);
},
{ once: true }
);
}
return origSend.apply(this, args);
};
}
/** Classic /challenges/ only — contest pages skip this (breaks prep-kit compile). */
function installFetchTrap() {
if (isContestTrack || window.__preppushFetchTrap) return;
window.__preppushFetchTrap = true;
let underlying = window.fetch.bind(window);
async function trappedFetch(...args) {
const response = await underlying.apply(window, args);
try {
const url = typeof args[0] === 'string' ? args[0] : args[0]?.url ?? '';
if (isSubmissionPollURL(url) && response?.clone) {
response
.clone()
.text()
.then((text) => tryHandlePollResponse(url, text))
.catch(() => {});
}
} catch {
/* never block HackerRank */
}
return response;
}
try {
Object.defineProperty(window, 'fetch', {
configurable: true,
enumerable: true,
get() {
return trappedFetch;
},
set(fn) {
if (typeof fn === 'function') underlying = fn.bind(window);
},
});
console.log('[PrepPush] Fetch trap installed ✅ (' + frameLabel + ')');
} catch {
window.fetch = trappedFetch;
}
}
/** Assignment-based fetch observer (safe on contest track; no defineProperty). */
function installFetchPollObserver() {
if (window.__preppushFetchPollObserver) return;
window.__preppushFetchPollObserver = true;
function attach() {
const prev = window.fetch;
if (!prev || typeof prev !== 'function') return;
if (prev.__preppushPollAttached) return;
const bound = prev.bind(window);
async function wrapped(...args) {
const response = await bound(...args);
try {
const url = typeof args[0] === 'string' ? args[0] : args[0]?.url ?? '';
if (isSubmissionPollURL(url) && response?.clone) {
response
.clone()
.text()
.then((text) => tryHandlePollResponse(url, text))
.catch(() => {});
}
} catch {
/* never block HackerRank */
}
return response;
}
wrapped.__preppushPollAttached = true;
window.fetch = wrapped;
}
attach();
let ticks = 0;
const reattach = setInterval(() => {
if (!window.fetch?.__preppushPollAttached) attach();
if (++ticks >= 45) clearInterval(reattach);
}, 2000);
console.log('[PrepPush] Fetch poll observer installed ✅ (' + frameLabel + ')');
}
installXhrObserver();
installFetchPollObserver();
if (!isContestTrack) {
const scheduleFetchTrap = () => {
if (document.readyState === 'complete') installFetchTrap();
else window.addEventListener('load', () => installFetchTrap(), { once: true });
};
scheduleFetchTrap();
}
})();