-
Notifications
You must be signed in to change notification settings - Fork 0
[New]: #3 #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[New]: #3 #13
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| import { fetchText, fetchTextPost } from "./fetch"; | ||
|
|
||
| export interface LanguageOption { | ||
| id: string; | ||
| label: string; | ||
| } | ||
|
|
||
| export interface SubmitPage { | ||
| contest: string; | ||
| csrfToken: string; | ||
| tasks: Array<{ value: string; label: string }>; | ||
| languages: LanguageOption[]; | ||
| } | ||
|
|
||
| export interface SubmitStatus { | ||
| success: boolean; | ||
| message: string; | ||
| url?: string; | ||
| } | ||
|
|
||
| export async function fetchSubmitPage(contest: string): Promise<SubmitPage> { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| const url = `https://atcoder.jp/contests/${contest}/submit`; | ||
| const html = await fetchText(url); | ||
|
|
||
| const csrfMatch = html.match(/name="csrf_token"[^>]*value="([^"]*)"/i); | ||
| const csrfToken = csrfMatch ? csrfMatch[1] : ""; | ||
|
|
||
| const tasks: Array<{ value: string; label: string }> = []; | ||
| const taskRegex = /<option[^>]*value="([^"]*)"[^>]*>([^<]*)<\/option>/gi; | ||
| let taskMatch: RegExpExecArray | null; | ||
| for (;(taskMatch = taskRegex.exec(html)) !== null;) { | ||
| const value = taskMatch[1]; | ||
| if (value) tasks.push({ value, label: taskMatch[2].trim() }); | ||
| } | ||
|
|
||
| const languages: LanguageOption[] = []; | ||
| const langRegex = /<option[^>]*value="(\d+)"[^>]*>([^<]*)<\/option>/gi; | ||
| let langMatch: RegExpExecArray | null; | ||
| for (;(langMatch = langRegex.exec(html)) !== null;) { | ||
| const value = langMatch[1]; | ||
| if (value) languages.push({ id: value, label: langMatch[2].trim() }); | ||
| } | ||
|
|
||
| return { contest, csrfToken, tasks, languages }; | ||
| } | ||
|
Transparent-fish marked this conversation as resolved.
|
||
|
|
||
| export async function submitCode(contest: string, taskScreenName: string, languageId: string, sourceCode: string, csrfToken: string): Promise<SubmitStatus> { | ||
| const url = `https://atcoder.jp/contests/${contest}/submit`; | ||
| const body = | ||
| `csrf_token=${encodeURIComponent(csrfToken)}` + | ||
| `&data.TaskScreenName=${encodeURIComponent(taskScreenName)}` + | ||
| `&data.LanguageId=${encodeURIComponent(languageId)}` + | ||
| `&sourceCode=${encodeURIComponent(sourceCode)}`; | ||
|
|
||
| const responseHtml = await fetchTextPost(url, body); | ||
|
|
||
| const errMatch = responseHtml.match( | ||
| /<div[^>]*class="[^"]*(?:alert-danger|alert-warning|alert-error)[^"]*"[^>]*>([\s\S]*?)<\/div>/i | ||
| ); | ||
| if (errMatch) { | ||
| const errMsg = errMatch[1].replace(/<[^>]+>/g, "").trim(); | ||
| return { success: false, message: errMsg }; | ||
| } | ||
|
|
||
| const successed = responseHtml.match( | ||
| /<div[^>]*class="[^"]*alert-success[^"]*"[^>]*>([\s\S]*?)<\/div>/i | ||
| ); | ||
| if (successed) { | ||
| const msg = successed[1].replace(/<[^>]+>/g, "").trim(); | ||
| return { success: true, message: msg, url: `https://atcoder.jp/contests/${contest}/submissions/me` }; | ||
| } | ||
|
|
||
| if (responseHtml.includes("/submissions/me") || responseHtml.includes("Submission")) { | ||
| return { | ||
| success: true, | ||
| message: "代码提交成功", | ||
| url: `https://atcoder.jp/contests/${contest}/submissions/me`, | ||
| }; | ||
| } | ||
|
|
||
| return { success: false, message: "提交失败,请检查 Cookie 是否有效" }; | ||
| } | ||
|
Comment on lines
+47
to
+82
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| export async function submitCodeWithRedirect(contest: string, taskScreenName: string, languageId: string, sourceCode: string): Promise<SubmitStatus> { | ||
| const pageData = await fetchSubmitPage(contest); | ||
| if (!pageData.csrfToken) { | ||
| return { success: false, message: "无法获取 CSRF Token,请检查 Cookie 是否有效" }; | ||
| } | ||
| return await submitCode(contest, taskScreenName, languageId, sourceCode, pageData.csrfToken); | ||
| } | ||
|
Transparent-fish marked this conversation as resolved.
|
||
Uh oh!
There was an error while loading. Please reload this page.