From b495812689351b5bdd043187ed143968510d799a Mon Sep 17 00:00:00 2001 From: tomoyachen <42526054+tomoyachen@users.noreply.github.com> Date: Fri, 26 Jul 2024 15:43:28 +0800 Subject: [PATCH 1/5] fix: fix login issue in VS Code Insiders (#966) (#968) ogin issue on VS Code Insiders #968 --- src/shared.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/shared.ts b/src/shared.ts index 8c6a825..2301f7f 100644 --- a/src/shared.ts +++ b/src/shared.ts @@ -125,13 +125,15 @@ export enum SortingStrategy { export const PREMIUM_URL_CN = "https://leetcode.cn/premium-payment/?source=vscode"; export const PREMIUM_URL_GLOBAL = "https://leetcode.com/subscribe/?ref=lp_pl&source=vscode"; +const protocol = vscode.env.appName.includes('Insiders') ? "vscode-insiders" : "vscode" + export const urls = { // base urls base: "https://leetcode.com", graphql: "https://leetcode.com/graphql", userGraphql: "https://leetcode.com/graphql", login: "https://leetcode.com/accounts/login/", - authLoginUrl: "https://leetcode.com/authorize-login/vscode/?path=leetcode.vscode-leetcode", + authLoginUrl: `https://leetcode.com/authorize-login/${protocol}/?path=leetcode.vscode-leetcode`, }; export const urlsCn = { @@ -140,7 +142,7 @@ export const urlsCn = { graphql: "https://leetcode.cn/graphql", userGraphql: "https://leetcode.cn/graphql/noj-go/", login: "https://leetcode.cn/accounts/login/", - authLoginUrl: "https://leetcode.cn/authorize-login/vscode/?path=leetcode.vscode-leetcode", + authLoginUrl: `https://leetcode.cn/authorize-login/${protocol}/?path=leetcode.vscode-leetcode`, }; export const getUrl = (key: string) => { From bbc041da250ccd2032d36318de2e71bc76e60ca6 Mon Sep 17 00:00:00 2001 From: tomoyachen <42526054+tomoyachen@users.noreply.github.com> Date: Tue, 30 Jul 2024 14:15:10 +0800 Subject: [PATCH 2/5] feat: re-add cookie-based login method (#969) * feat: re-add cookie-based login method * chore: optimize tslint config * chore : change build.yml --------- Co-authored-by: leo.zhao --- .github/workflows/build.yml | 21 ---------- .vscode/settings.json | 2 +- src/leetCodeManager.ts | 83 ++++++++++++++++++++++++++++++------- src/utils/httpUtils.ts | 2 +- src/utils/toolUtils.ts | 2 +- src/utils/trackingUtils.ts | 2 +- tslint.json | 44 ++++++++------------ 7 files changed, 89 insertions(+), 67 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 2bdeaa4..dd9a1c2 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -48,24 +48,3 @@ jobs: - name: VSCE Packge run: npx vsce package - - darwin: - name: macOS - runs-on: macos-latest - timeout-minutes: 30 - steps: - - uses: actions/checkout@v2 - - - name: Setup Node.js environment - uses: actions/setup-node@v2 - with: - node-version: 14 - - - name: Install Node.js modules - run: npm install - - - name: Lint - run: npm run lint - - - name: VSCE Packge - run: npx vsce package diff --git a/.vscode/settings.json b/.vscode/settings.json index b5f7231..5e5ca48 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,5 @@ { - "editor.formatOnSave": true, + "editor.formatOnSave": false, "editor.insertSpaces": true, "editor.tabSize": 4, "files.insertFinalNewline": true, diff --git a/src/leetCodeManager.ts b/src/leetCodeManager.ts index 1b2d4af..16ec378 100644 --- a/src/leetCodeManager.ts +++ b/src/leetCodeManager.ts @@ -6,14 +6,14 @@ import { EventEmitter } from "events"; import * as vscode from "vscode"; import { leetCodeChannel } from "./leetCodeChannel"; import { leetCodeExecutor } from "./leetCodeExecutor"; -import { Endpoint, loginArgsMapping, urls, urlsCn, UserStatus } from "./shared"; +import { Endpoint, IQuickItemEx, loginArgsMapping, urls, urlsCn, UserStatus } from "./shared"; import { createEnvOption } from "./utils/cpUtils"; import { DialogType, openUrl, promptForOpenOutputChannel } from "./utils/uiUtils"; import * as wsl from "./utils/wslUtils"; import { getLeetCodeEndpoint } from "./commands/plugin"; import { globalState } from "./globalState"; import { queryUserData } from "./request/query-user-data"; -import { parseQuery, sleep } from "./utils/toolUtils"; +import { parseQuery } from "./utils/toolUtils"; class LeetCodeManager extends EventEmitter { private currentUser: string | undefined; @@ -42,6 +42,19 @@ class LeetCodeManager extends EventEmitter { } } + private async updateUserStatusWithCookie(cookie: string): Promise { + globalState.setCookie(cookie); + const data = await queryUserData(); + globalState.setUserStatus(data); + await this.setCookieToCli(cookie, data.username); + if (data.username) { + vscode.window.showInformationMessage(`Successfully ${data.username}.`); + this.currentUser = data.username; + this.userStatus = UserStatus.SignedIn; + this.emit("statusChanged"); + } + } + public async handleUriSignIn(uri: vscode.Uri): Promise { try { await vscode.window.withProgress({ location: vscode.ProgressLocation.Notification }, async (progress: vscode.Progress<{}>) => { @@ -52,24 +65,61 @@ class LeetCodeManager extends EventEmitter { promptForOpenOutputChannel(`Failed to get cookie. Please log in again`, DialogType.error); return; } - globalState.setCookie(cookie); - const data = await queryUserData(); - globalState.setUserStatus(data); - await this.setCookieToCli(cookie, data.username); - if (data.username) { - vscode.window.showInformationMessage(`Successfully ${data.username}.`); - this.currentUser = data.username; - this.userStatus = UserStatus.SignedIn; - this.emit("statusChanged"); - } + + await this.updateUserStatusWithCookie(cookie) + }); } catch (error) { promptForOpenOutputChannel(`Failed to log in. Please open the output channel for details`, DialogType.error); } } + public async handleInputCookieSignIn(): Promise { + const cookie: string | undefined = await vscode.window.showInputBox({ + prompt: 'Enter LeetCode Cookie', + password: true, + ignoreFocusOut: true, + validateInput: (s: string): string | undefined => + s ? undefined : 'Cookie must not be empty', + }) + + await this.updateUserStatusWithCookie(cookie || '') + } + public async signIn(): Promise { - openUrl(this.getAuthLoginUrl()); + const picks: Array> = [] + picks.push( + { + label: 'Web Authorization', + detail: 'Open browser to authorize login on the website', + value: 'WebAuth', + description: '[Recommended]' + }, + { + label: 'LeetCode Cookie', + detail: 'Use LeetCode cookie copied from browser to login', + value: 'Cookie', + } + ) + + const choice: IQuickItemEx | undefined = await vscode.window.showQuickPick(picks) + if (!choice) { + return + } + const loginMethod: string = choice.value + + if (loginMethod === 'WebAuth') { + openUrl(this.getAuthLoginUrl()) + return + } + + try { + await vscode.window.withProgress({ location: vscode.ProgressLocation.Notification, title: "Fetching user data..." }, async () => { + await this.handleInputCookieSignIn() + }); + } catch (error) { + promptForOpenOutputChannel(`Failed to log in. Please open the output channel for details`, DialogType.error); + } } public async signOut(): Promise { @@ -136,15 +186,16 @@ class LeetCodeManager extends EventEmitter { } else if (data.match(this.failRegex)) { childProc.stdin?.end(); return reject(new Error("Faile to login")); + } else if (data.match(/login: /)) { + childProc.stdin?.write(`${name}\n`); + } else if (data.match(/cookie: /)) { + childProc.stdin?.write(`${cookie}\n`); } }); childProc.stderr?.on("data", (data: string | Buffer) => leetCodeChannel.append(data.toString())); childProc.on("error", reject); - childProc.stdin?.write(`${name}\n`); - await sleep(800); - childProc.stdin?.write(`${cookie}\n`); }); } } diff --git a/src/utils/httpUtils.ts b/src/utils/httpUtils.ts index aa6e205..b777173 100644 --- a/src/utils/httpUtils.ts +++ b/src/utils/httpUtils.ts @@ -16,7 +16,7 @@ export function LcAxios(path: string, settings?: AxiosRequestConfig): A } return axios(path, { headers: { - referer: referer, + referer, "content-type": "application/json", cookie, ...(settings && settings.headers), diff --git a/src/utils/toolUtils.ts b/src/utils/toolUtils.ts index 6437226..ce37e18 100644 --- a/src/utils/toolUtils.ts +++ b/src/utils/toolUtils.ts @@ -9,7 +9,7 @@ export function parseQuery(query: string): { [key: string]: string } { return queryObject; } - let keyValuePairs = query.split("&"); + const keyValuePairs = query.split("&"); keyValuePairs.forEach((pair) => { const firstEqualsIndex = pair.indexOf("="); if (firstEqualsIndex !== -1) { diff --git a/src/utils/trackingUtils.ts b/src/utils/trackingUtils.ts index 4333bfa..56b1a6f 100644 --- a/src/utils/trackingUtils.ts +++ b/src/utils/trackingUtils.ts @@ -90,7 +90,7 @@ class TrackData implements ITrackData { if (!Array.isArray(reportItems)) { reportItems = [reportItems]; } - let randomId = getRandomString(60); + const randomId = getRandomString(60); reportItems.forEach((item) => { this.reportCache.push({ ...item, diff --git a/tslint.json b/tslint.json index 34776be..a686a2c 100644 --- a/tslint.json +++ b/tslint.json @@ -1,36 +1,28 @@ { "defaultSeverity": "error", - "extends": [ - "tslint:recommended" - ], + "extends": ["tslint:recommended"], "jsRules": {}, "rules": { - "forin": false, "object-literal-sort-keys": false, - "indent": [ - true, - "spaces" - ], + "ordered-imports": [false], + "indent": [true, "spaces"], "no-string-literal": false, "no-namespace": false, - "max-line-length": [ - false, - 120 - ], - "typedef": [ - true, - "call-signature", - "arrow-call-signature", - "parameter", - "arrow-parameter", - "property-declaration", - "variable-declaration", - "member-variable-declaration" - ], - "variable-name": [ - true, - "allow-leading-underscore" - ] + "max-line-length": [false, 120], + "typedef": false, + "no-implicit-dependencies": [true, ["vscode"]], + "trailing-comma": false, + "no-any": false, + "object-literal-key-quotes": [true, "consistent-as-needed"], + "prefer-object-spread": false, + "no-unnecessary-await": false, + "semicolon": [false], + "quotemark": [false], + "member-ordering": [false], + "variable-name": [false], + "curly": false, + "interface-over-type-literal": [false], + "no-unused-expression": false }, "rulesDirectory": [] } From 67101c96dc41feee1700bee854642037734511b1 Mon Sep 17 00:00:00 2001 From: "leo.zhao" Date: Tue, 30 Jul 2024 14:34:46 +0800 Subject: [PATCH 3/5] chore(package.json): add build and publish for vscode in scripts config --- package.json | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 702956c..b270a2d 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "vscode-leetcode", "displayName": "LeetCode", "description": "Solve LeetCode problems in VS Code", - "version": "0.18.2", + "version": "0.18.3", "author": "LeetCode", "publisher": "LeetCode", "license": "MIT", @@ -714,7 +714,9 @@ "vscode:prepublish": "npm run compile", "compile": "tsc -p ./", "watch": "tsc -watch -p ./", - "lint": "tslint --project tsconfig.json -e src/*.d.ts -t verbose" + "lint": "tslint --project tsconfig.json -e src/*.d.ts -t verbose", + "build": "vsce package", + "vs-publish": "vsce publish" }, "devDependencies": { "@types/fs-extra": "^9.0.11", From 6f4292544185b0406673bf2669747aa72dcdc49a Mon Sep 17 00:00:00 2001 From: "leo.zhao" Date: Tue, 30 Jul 2024 15:18:15 +0800 Subject: [PATCH 4/5] chore: add change log --- CHANGELOG.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0de174f..a7d482a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,18 @@ All notable changes to the "leetcode" extension will be documented in this file. Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file. +## [0.18.3] +### Added +- re-add cookie-based login method [PR#969](https://github.com/LeetCode-OpenSource/vscode-leetcode/pull/969) + +## [0.18.2] +### Fixed +- fix login issue on VS Code Insiders [PR#968](https://github.com/LeetCode-OpenSource/vscode-leetcode/pull/968) + +## [0.18.1] +### Changed +- change login way and add tracking logic option [PR#944](https://github.com/LeetCode-OpenSource/vscode-leetcode/pull/944) + ## [0.18.0] ### Added - Add `star` command in shortcuts [PR#601](https://github.com/LeetCode-OpenSource/vscode-leetcode/pull/601) From 586b3e45fe0af299775436f2e8ea2a6ed5cdb8c6 Mon Sep 17 00:00:00 2001 From: "leo.zhao" Date: Fri, 13 Sep 2024 14:47:56 +0800 Subject: [PATCH 5/5] fix: change graphql api path --- CHANGELOG.md | 4 ++++ package.json | 2 +- src/shared.ts | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a7d482a..b9d7d46 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,10 @@ All notable changes to the "leetcode" extension will be documented in this file. Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file. +## [0.18.4] +### Added +- change graphql path + ## [0.18.3] ### Added - re-add cookie-based login method [PR#969](https://github.com/LeetCode-OpenSource/vscode-leetcode/pull/969) diff --git a/package.json b/package.json index b270a2d..53552b7 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "vscode-leetcode", "displayName": "LeetCode", "description": "Solve LeetCode problems in VS Code", - "version": "0.18.3", + "version": "0.18.4", "author": "LeetCode", "publisher": "LeetCode", "license": "MIT", diff --git a/src/shared.ts b/src/shared.ts index 2301f7f..e8b59d8 100644 --- a/src/shared.ts +++ b/src/shared.ts @@ -140,7 +140,7 @@ export const urlsCn = { // base urls base: "https://leetcode.cn", graphql: "https://leetcode.cn/graphql", - userGraphql: "https://leetcode.cn/graphql/noj-go/", + userGraphql: "https://leetcode.cn/graphql/", login: "https://leetcode.cn/accounts/login/", authLoginUrl: `https://leetcode.cn/authorize-login/${protocol}/?path=leetcode.vscode-leetcode`, };