Skip to content

Commit c6c5833

Browse files
committed
refs #27: fix 'failed to load problem' error
* leetcode.com has changed the html thus we have to update accordingly Signed-off-by: Eric Wang <[email protected]>
1 parent b58cba7 commit c6c5833

File tree

3 files changed

+1111
-11
lines changed

3 files changed

+1111
-11
lines changed

lib/leetcode_client.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -133,16 +133,17 @@ leetcodeClient.getProblem = function(problem, cb) {
133133
problem.desc = $('meta[property="og:description"]').attr('content');
134134
problem.desc = he.decode(problem.desc);
135135

136-
var raw = $('div[ng-controller="AceCtrl as aceCtrl"]').attr('ng-init');
137-
if (!raw)
136+
var pageData;
137+
var r = /(var pageData[^;]+;)/m;
138+
var result = body.match(r);
139+
if (!result)
138140
return cb('failed to load' + (problem.locked ? ' locked ' : ' ') +
139141
'problem!');
140142

141-
raw = raw.replace(/\n/g, '');
142-
var args = eval(raw);
143-
problem.templates = args[0];
144-
problem.testcase = args[8];
145-
problem.testable = args[10];
143+
eval(result[1]);
144+
problem.templates = pageData.codeDefinition;
145+
problem.testcase = pageData.sampleTestCase;
146+
problem.testable = pageData.enableRunCode;
146147

147148
return cb(null, problem);
148149
});

test/mock/find-the-difference.html.20170424

+1,097
Large diffs are not rendered by default.

test/test_leetcode_client.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,12 @@ describe('leetcode_client', function() {
123123
it('should ok', function(done) {
124124
nock('https://leetcode.com')
125125
.get('/problems/find-the-difference')
126-
.replyWithFile(200, './test/mock/find-the-difference.html.20160911');
126+
.replyWithFile(200, './test/mock/find-the-difference.html.20170424');
127127

128128
client.getProblem(PROBLEM, function(e, problem) {
129129
assert.equal(e, null);
130-
assert.equal(problem.totalAC, 15674);
131-
assert.equal(problem.totalSubmit, 32141);
130+
assert.equal(problem.totalAC, 63380);
131+
assert.equal(problem.totalSubmit, 123178);
132132
assert.equal(problem.desc,
133133
[
134134
'',
@@ -187,7 +187,8 @@ describe('leetcode_client', function() {
187187
' :type s: str',
188188
' :type t: str',
189189
' :rtype: str',
190-
' """'
190+
' """',
191+
' '
191192
].join('\r\n'));
192193

193194
assert.equal(problem.templates[3].value, 'c');
@@ -566,3 +567,4 @@ describe('leetcode_client', function() {
566567
});
567568
}); // #login
568569
});
570+

0 commit comments

Comments
 (0)