From 8b22e3abf9648ce251f00bc8cbd9e14d2d9ee229 Mon Sep 17 00:00:00 2001 From: yihong Date: Thu, 18 Jun 2020 21:21:12 +0800 Subject: [PATCH 1/2] fix: leetcode-cn can not use GitHub log in bug --- lib/plugins/leetcode.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/plugins/leetcode.js b/lib/plugins/leetcode.js index 8855f689..7790bb97 100644 --- a/lib/plugins/leetcode.js +++ b/lib/plugins/leetcode.js @@ -540,10 +540,16 @@ plugin.login = function(user, cb) { }; function parseCookie(cookie, body, cb) { + const isCN = config.app === 'leetcode.cn'; const SessionPattern = /LEETCODE_SESSION=(.+?)(;|$)/; - const csrfPattern = /csrftoken=(.+?)(;|$)/; + let csrfPattern; + if (isCN) { + csrfPattern = /name="csrfmiddlewaretoken" value="(.*?)"/; + } else { + csrfPattern = /csrftoken=(.+?)(;|$)/; + } + const reCsrfResult = csrfPattern.exec(isCN? body: cookie); const reSessionResult = SessionPattern.exec(cookie); - const reCsrfResult = csrfPattern.exec(cookie); if (reSessionResult === null || reCsrfResult === null) { return cb('invalid cookie?'); } From c0f9524b6939f81d9b8ec367221a83f8cc5444b8 Mon Sep 17 00:00:00 2001 From: yihong0618 Date: Thu, 20 Aug 2020 18:50:16 +0800 Subject: [PATCH 2/2] fix: can not star bug --- lib/plugins/leetcode.js | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/lib/plugins/leetcode.js b/lib/plugins/leetcode.js index 7790bb97..c403582d 100644 --- a/lib/plugins/leetcode.js +++ b/lib/plugins/leetcode.js @@ -360,31 +360,25 @@ plugin.getSubmission = function(submission, cb) { plugin.starProblem = function(problem, starred, cb) { log.debug('running leetcode.starProblem'); - const opts = plugin.makeOpts(); + const user = session.getUser(); + const operationName = starred ? 'addQuestionToFavorite' : 'removeQuestionFromFavorite'; + const opts = plugin.makeOpts(config.sys.urls.graphql); opts.headers.Origin = config.sys.urls.base; opts.headers.Referer = problem.link; - const user = session.getUser(); - if (starred) { - opts.url = config.sys.urls.favorites; - opts.method = 'POST'; - opts.json = true; - opts.body = { - favorite_id_hash: user.hash, - question_id: problem.id - }; - } else { - opts.url = config.sys.urls.favorite_delete - .replace('$hash', user.hash) - .replace('$id', problem.id); - opts.method = 'DELETE'; - } + opts.json = true; + opts.body = { + query: `mutation ${operationName}($favoriteIdHash: String!, $questionId: String!) {\n ${operationName}(favoriteIdHash: $favoriteIdHash, questionId: $questionId) {\n ok\n error\n favoriteIdHash\n questionId\n __typename\n }\n}\n`, + variables: {favoriteIdHash: user.hash, questionId: '' + problem.id}, + operationName: operationName + }; - request(opts, function(e, resp, body) { - e = plugin.checkError(e, resp, 204); + const spin = h.spin(starred? 'star': 'unstar' + 'problem'); + request.post(opts, function(e, resp, body) { + spin.stop(); + e = plugin.checkError(e, resp, 200); if (e) return cb(e); - - cb(null, starred); + return cb(null, starred); }); };