Skip to content

Commit 4979a0f

Browse files
yihong0618jdneo
authored andcommitted
feat: add leetcode.signinByCookie but simple change (#487)
1 parent eb77cd9 commit 4979a0f

File tree

5 files changed

+27
-14
lines changed

5 files changed

+27
-14
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@
4040

4141
- Simply click `Sign in to LeetCode` in the `LeetCode Explorer` will let you **sign in** with your LeetCode account.
4242

43-
- You can also use the following command to sign in/out:
43+
- You can also use the following command to sign in/sign in (by cookie)/out:
4444
- **LeetCode: Sign in**
45+
- **LeetCode: Sign in (by cookie)**
4546
- **LeetCode: Sign out**
4647

4748
---

docs/README_zh-CN.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@
4040

4141
- 点击 `LeetCode Explorer` 中的 `Sign in to LeetCode` 即可登入。
4242

43-
- 你也可以使用下来命令登入或登出:
43+
- 你也可以使用下来命令登入或利用cookie登入或登出:
4444
- **LeetCode: Sign in**
45+
- **LeetCode: Sign in (by cookie)**
4546
- **LeetCode: Sign out**
4647

4748
---

package.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,17 @@
3838
"onCommand:leetcode.testSolution",
3939
"onCommand:leetcode.submitSolution",
4040
"onCommand:leetcode.switchDefaultLanguage",
41+
"onCommand:leetcode.signinByCookie",
4142
"onView:leetCodeExplorer"
4243
],
4344
"main": "./out/src/extension",
4445
"contributes": {
4546
"commands": [
47+
{
48+
"command": "leetcode.signinByCookie",
49+
"title": "Sign In by Cookie",
50+
"category": "LeetCode"
51+
},
4652
{
4753
"command": "leetcode.deleteCache",
4854
"title": "Delete Cache",
@@ -683,6 +689,6 @@
683689
"markdown-it": "^8.4.2",
684690
"require-from-string": "^2.0.2",
685691
"unescape-js": "^1.1.1",
686-
"vsc-leetcode-cli": "2.6.16"
692+
"vsc-leetcode-cli": "2.6.17"
687693
}
688694
}

src/extension.ts

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
5151
vscode.commands.registerCommand("leetcode.deleteCache", () => cache.deleteCache()),
5252
vscode.commands.registerCommand("leetcode.toggleLeetCodeCn", () => plugin.switchEndpoint()),
5353
vscode.commands.registerCommand("leetcode.signin", () => leetCodeManager.signIn()),
54+
vscode.commands.registerCommand("leetcode.signinByCookie", () => leetCodeManager.signIn(true)),
5455
vscode.commands.registerCommand("leetcode.signout", () => leetCodeManager.signOut()),
5556
vscode.commands.registerCommand("leetcode.manageSessions", () => session.manageSessions()),
5657
vscode.commands.registerCommand("leetcode.previewProblem", (node: LeetCodeNode) => show.previewProblem(node)),

src/leetCodeManager.ts

+15-11
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,20 @@ class LeetCodeManager extends EventEmitter {
3434
}
3535
}
3636

37-
public async signIn(): Promise<void> {
37+
public async signIn(isByCookie: boolean = false): Promise<void> {
38+
const loginArg: string = "-l";
39+
const cookieArg: string = "-c";
40+
const commandArg: string = isByCookie ? cookieArg : loginArg;
41+
const inMessage: string = isByCookie ? "sign in by cookie" : "sign in";
3842
try {
3943
const userName: string | undefined = await new Promise(async (resolve: (res: string | undefined) => void, reject: (e: Error) => void): Promise<void> => {
4044
let result: string = "";
4145

4246
const leetCodeBinaryPath: string = await leetCodeExecutor.getLeetCodeBinaryPath();
4347

4448
const childProc: cp.ChildProcess = wsl.useWsl()
45-
? cp.spawn("wsl", [leetCodeExecutor.node, leetCodeBinaryPath, "user", "-l"], { shell: true })
46-
: cp.spawn(leetCodeExecutor.node, [leetCodeBinaryPath, "user", "-l"], {
49+
? cp.spawn("wsl", [leetCodeExecutor.node, leetCodeBinaryPath, "user", commandArg], { shell: true })
50+
: cp.spawn(leetCodeExecutor.node, [leetCodeBinaryPath, "user", commandArg], {
4751
shell: true,
4852
env: createEnvOption(),
4953
});
@@ -67,9 +71,9 @@ class LeetCodeManager extends EventEmitter {
6771
}
6872
childProc.stdin.write(`${name}\n`);
6973
const pwd: string | undefined = await vscode.window.showInputBox({
70-
prompt: "Enter password.",
74+
prompt: isByCookie ? "Enter cookie" : "Enter password.",
7175
password: true,
72-
validateInput: (s: string): string | undefined => s ? undefined : "Password must not be empty",
76+
validateInput: (s: string): string | undefined => s ? undefined : isByCookie ? "Cookie must not be empty" : "Password must not be empty",
7377
});
7478
if (!pwd) {
7579
childProc.kill();
@@ -78,22 +82,22 @@ class LeetCodeManager extends EventEmitter {
7882
childProc.stdin.write(`${pwd}\n`);
7983
childProc.stdin.end();
8084
childProc.on("close", () => {
81-
const match: RegExpMatchArray | null = result.match(/(?:.*) Successfully login as (.*)/i);
82-
if (match && match[1]) {
83-
resolve(match[1]);
85+
const match: RegExpMatchArray | null = result.match(/(?:.*) Successfully (login|cookie login) as (.*)/i);
86+
if (match && match[2]) {
87+
resolve(match[2]);
8488
} else {
85-
reject(new Error("Failed to sign in."));
89+
reject(new Error(`Failed to ${inMessage}.`));
8690
}
8791
});
8892
});
8993
if (userName) {
90-
vscode.window.showInformationMessage("Successfully signed in.");
94+
vscode.window.showInformationMessage(`Successfully ${inMessage}.`);
9195
this.currentUser = userName;
9296
this.userStatus = UserStatus.SignedIn;
9397
this.emit("statusChanged");
9498
}
9599
} catch (error) {
96-
promptForOpenOutputChannel("Failed to sign in. Please open the output channel for details", DialogType.error);
100+
promptForOpenOutputChannel(`Failed to ${inMessage}. Please open the output channel for details`, DialogType.error);
97101
}
98102

99103
}

0 commit comments

Comments
 (0)