diff --git a/lib/plugins/leetcode.cn.js b/lib/plugins/leetcode.cn.js index 14b16e1..d05a726 100644 --- a/lib/plugins/leetcode.cn.js +++ b/lib/plugins/leetcode.cn.js @@ -39,11 +39,19 @@ plugin.init = function() { // FIXME: refactor those // update options with user credentials -function signOpts(opts, user) { +function setupHeaders(opts, user) { opts.headers.Cookie = 'LEETCODE_SESSION=' + user.sessionId + ';csrftoken=' + user.sessionCSRF + ';'; opts.headers['X-CSRFToken'] = user.sessionCSRF; + opts.headers['x-csrftoken'] = user.sessionCSRF; opts.headers['X-Requested-With'] = 'XMLHttpRequest'; + + opts.headers['User-Agent'] = 'Mozilla/5.0 (X11; Linux x86_64; rv:123.0) Gecko/20100101 Firefox/123.0'; + opts.headers['Referer'] = 'https://leetcode.cn'; + opts.headers['Origin'] = 'https://leetcode.cn/'; + opts.headers['Host'] = 'leetcode.cn'; + opts.headers['Content-Type'] = 'application/json'; + opts.headers['Accept'] = 'application/json'; } function makeOpts(url) { @@ -51,8 +59,8 @@ function makeOpts(url) { opts.url = url; opts.headers = {}; - if (session.isLogin()) - signOpts(opts, session.getUser()); + setupHeaders(opts, session.getUser()); + return opts; } diff --git a/lib/plugins/leetcode.js b/lib/plugins/leetcode.js index 93d6605..c8f8d78 100644 --- a/lib/plugins/leetcode.js +++ b/lib/plugins/leetcode.js @@ -23,7 +23,15 @@ plugin.signOpts = function(opts, user) { opts.headers.Cookie = 'LEETCODE_SESSION=' + user.sessionId + ';csrftoken=' + user.sessionCSRF + ';'; opts.headers['X-CSRFToken'] = user.sessionCSRF; + opts.headers['x-csrftoken'] = user.sessionCSRF; opts.headers['X-Requested-With'] = 'XMLHttpRequest'; + + opts.headers['User-Agent'] = 'Mozilla/5.0 (X11; Linux x86_64; rv:123.0) Gecko/20100101 Firefox/123.0'; + opts.headers['Referer'] = 'https://leetcode.com'; + opts.headers['Origin'] = 'https://leetcode.com/'; + opts.headers['Host'] = 'leetcode.com'; + opts.headers['Content-Type'] = 'application/json'; + opts.headers['Accept'] = 'application/json'; }; plugin.makeOpts = function(url) { @@ -31,8 +39,8 @@ plugin.makeOpts = function(url) { opts.url = url; opts.headers = {}; - if (session.isLogin()) - plugin.signOpts(opts, session.getUser()); + plugin.signOpts(opts, session.getUser()); + return opts; }; diff --git a/lib/plugins/solution.discuss.js b/lib/plugins/solution.discuss.js index d5f07c1..3d79045 100644 --- a/lib/plugins/solution.discuss.js +++ b/lib/plugins/solution.discuss.js @@ -16,6 +16,21 @@ var plugin = new Plugin(200, 'solution.discuss', '2019.02.03', var URL_DISCUSSES = 'https://leetcode.com/graphql'; var URL_DISCUSS = 'https://leetcode.com/problems/$slug/discuss/$id'; +function setupHeaders(opts, user) { + opts.headers.Cookie = 'LEETCODE_SESSION=' + user.sessionId + + ';csrftoken=' + user.sessionCSRF + ';'; + opts.headers['X-CSRFToken'] = user.sessionCSRF; + opts.headers['x-csrftoken'] = user.sessionCSRF; + opts.headers['X-Requested-With'] = 'XMLHttpRequest'; + + opts.headers['User-Agent'] = 'Mozilla/5.0 (X11; Linux x86_64; rv:123.0) Gecko/20100101 Firefox/123.0'; + opts.headers['Referer'] = 'https://leetcode.cn'; + opts.headers['Origin'] = 'https://leetcode.cn/'; + opts.headers['Host'] = 'leetcode.cn'; + opts.headers['Content-Type'] = 'application/json'; + opts.headers['Accept'] = 'application/json'; +} + function getSolution(problem, lang, cb) { if (!problem) return cb(); @@ -60,6 +75,11 @@ function getSolution(problem, lang, cb) { }) } }; + + opts.headers = {}; + + setupHeaders(opts, session.getUser()); + request(opts, function(e, resp, body) { if (e) return cb(e); if (resp.statusCode !== 200)