diff --git a/README.md b/README.md index 67fb4df0..740f9913 100644 --- a/README.md +++ b/README.md @@ -40,8 +40,9 @@ - Simply click `Sign in to LeetCode` in the `LeetCode Explorer` will let you **sign in** with your LeetCode account. -- You can also use the following command to sign in/out: +- You can also use the following command to sign in/sign in (by cookie)/out: - **LeetCode: Sign in** + - **LeetCode: Sign in (by cookie)** - **LeetCode: Sign out** --- diff --git a/docs/README_zh-CN.md b/docs/README_zh-CN.md index 4c9348a1..f7570685 100644 --- a/docs/README_zh-CN.md +++ b/docs/README_zh-CN.md @@ -40,8 +40,9 @@ - 点击 `LeetCode Explorer` 中的 `Sign in to LeetCode` 即可登入。 -- 你也可以使用下来命令登入或登出: +- 你也可以使用下来命令登入或利用cookie登入或登出: - **LeetCode: Sign in** + - **LeetCode: Sign in (by cookie)** - **LeetCode: Sign out** --- diff --git a/package.json b/package.json index 91bc6e59..149059de 100644 --- a/package.json +++ b/package.json @@ -38,11 +38,17 @@ "onCommand:leetcode.testSolution", "onCommand:leetcode.submitSolution", "onCommand:leetcode.switchDefaultLanguage", + "onCommand:leetcode.signinByCookie", "onView:leetCodeExplorer" ], "main": "./out/src/extension", "contributes": { "commands": [ + { + "command": "leetcode.signinByCookie", + "title": "Sign In by Cookie", + "category": "LeetCode" + }, { "command": "leetcode.deleteCache", "title": "Delete Cache", @@ -683,6 +689,6 @@ "markdown-it": "^8.4.2", "require-from-string": "^2.0.2", "unescape-js": "^1.1.1", - "vsc-leetcode-cli": "2.6.16" + "vsc-leetcode-cli": "2.6.17" } } diff --git a/src/extension.ts b/src/extension.ts index 9bb3ad41..45de4a70 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -51,6 +51,7 @@ export async function activate(context: vscode.ExtensionContext): Promise vscode.commands.registerCommand("leetcode.deleteCache", () => cache.deleteCache()), vscode.commands.registerCommand("leetcode.toggleLeetCodeCn", () => plugin.switchEndpoint()), vscode.commands.registerCommand("leetcode.signin", () => leetCodeManager.signIn()), + vscode.commands.registerCommand("leetcode.signinByCookie", () => leetCodeManager.signIn(true)), vscode.commands.registerCommand("leetcode.signout", () => leetCodeManager.signOut()), vscode.commands.registerCommand("leetcode.manageSessions", () => session.manageSessions()), vscode.commands.registerCommand("leetcode.previewProblem", (node: LeetCodeNode) => show.previewProblem(node)), diff --git a/src/leetCodeManager.ts b/src/leetCodeManager.ts index 96e3c0b0..0b70080e 100644 --- a/src/leetCodeManager.ts +++ b/src/leetCodeManager.ts @@ -34,7 +34,11 @@ class LeetCodeManager extends EventEmitter { } } - public async signIn(): Promise { + public async signIn(isByCookie: boolean = false): Promise { + const loginArg: string = "-l"; + const cookieArg: string = "-c"; + const commandArg: string = isByCookie ? cookieArg : loginArg; + const inMessage: string = isByCookie ? "sign in by cookie" : "sign in"; try { const userName: string | undefined = await new Promise(async (resolve: (res: string | undefined) => void, reject: (e: Error) => void): Promise => { let result: string = ""; @@ -42,8 +46,8 @@ class LeetCodeManager extends EventEmitter { const leetCodeBinaryPath: string = await leetCodeExecutor.getLeetCodeBinaryPath(); const childProc: cp.ChildProcess = wsl.useWsl() - ? cp.spawn("wsl", [leetCodeExecutor.node, leetCodeBinaryPath, "user", "-l"], { shell: true }) - : cp.spawn(leetCodeExecutor.node, [leetCodeBinaryPath, "user", "-l"], { + ? cp.spawn("wsl", [leetCodeExecutor.node, leetCodeBinaryPath, "user", commandArg], { shell: true }) + : cp.spawn(leetCodeExecutor.node, [leetCodeBinaryPath, "user", commandArg], { shell: true, env: createEnvOption(), }); @@ -67,9 +71,9 @@ class LeetCodeManager extends EventEmitter { } childProc.stdin.write(`${name}\n`); const pwd: string | undefined = await vscode.window.showInputBox({ - prompt: "Enter password.", + prompt: isByCookie ? "Enter cookie" : "Enter password.", password: true, - validateInput: (s: string): string | undefined => s ? undefined : "Password must not be empty", + validateInput: (s: string): string | undefined => s ? undefined : isByCookie ? "Cookie must not be empty" : "Password must not be empty", }); if (!pwd) { childProc.kill(); @@ -78,22 +82,22 @@ class LeetCodeManager extends EventEmitter { childProc.stdin.write(`${pwd}\n`); childProc.stdin.end(); childProc.on("close", () => { - const match: RegExpMatchArray | null = result.match(/(?:.*) Successfully login as (.*)/i); - if (match && match[1]) { - resolve(match[1]); + const match: RegExpMatchArray | null = result.match(/(?:.*) Successfully (login|cookie login) as (.*)/i); + if (match && match[2]) { + resolve(match[2]); } else { - reject(new Error("Failed to sign in.")); + reject(new Error(`Failed to ${inMessage}.`)); } }); }); if (userName) { - vscode.window.showInformationMessage("Successfully signed in."); + vscode.window.showInformationMessage(`Successfully ${inMessage}.`); this.currentUser = userName; this.userStatus = UserStatus.SignedIn; this.emit("statusChanged"); } } catch (error) { - promptForOpenOutputChannel("Failed to sign in. Please open the output channel for details", DialogType.error); + promptForOpenOutputChannel(`Failed to ${inMessage}. Please open the output channel for details`, DialogType.error); } }