Hack everyone!
This is a script for automatically hacking AC submissions to a given problem in a given contest. To use this script, input the target contestId, problem, testCase, and count, then paste the generated javascript into console on codeforces.com.
Notes:
Contestshould be the ID of the contest (eg. https://codeforces.com/contest/1701 has an ID of 1701).Problemshould be the index (letter) of the problem (eg. A, B, C, etc.).Countshould be the number of submissions you want to parse. Note that this corresponds to the total number of submissions before filtering for AC or problem.Test caseshould be the test case you want to submit as a hack.
js
1const contestId = 1701;
2const problem = 'A';
3const testCase = ``;
4const count = 1000;
5
6;(async () => {
7 const submissions = (await (await fetch(`https://codeforces.com/api/contest.status?contestId=${contestId}&from=1&count=${count}`)).json()).result
8 .filter((submission) => submission.verdict === 'OK' && submission.problem.index === problem);
9
10 for (const submission of submissions) {
11 const raw = await (await fetch(`https://codeforces.com/contest/1701/challenge/${submission.id}`)).text();
12 const [, csrf, body] = raw.match(/<form class="challenge-form".+?action="\/data\/challenge\?csrf_token=(.+?)".*?>([^]+)<\/form>/);
13
14 const formData = new FormData();
15
16 // Add all hidden fields
17 [...body.matchAll(/<input type="hidden".+?name="(\w+)".+?value="(\w+)"\/>/g)]
18 .forEach(([, name, value]) => formData.append(name, value));
19
20 formData.append('testcase', testCase);
21
22 await fetch(`https://codeforces.com/data/challenge?csrf_token=${csrf}`, {
23 body: formData,
24 method: 'POST'
25 });
26 }
27})();