Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbd80b3a

Browse files
committed
expose more code to plugins.
* leetcode.cn/lintcode could reuse this.Signed-off-by: Eric Wang <skygragon@gmail.com>
1 parentb5a5cfa commitbd80b3a

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed

‎lib/plugins/leetcode.js

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,24 @@ const plugin = new Plugin(10, 'leetcode', '',
2020
varspin;
2121

2222
// update options with user credentials
23-
functionsignOpts(opts,user){
23+
plugin.signOpts=function(opts,user){
2424
opts.headers.Cookie='LEETCODE_SESSION='+user.sessionId+
2525
';csrftoken='+user.sessionCSRF+';';
2626
opts.headers['X-CSRFToken']=user.sessionCSRF;
2727
opts.headers['X-Requested-With']='XMLHttpRequest';
28-
}
28+
};
2929

30-
functionmakeOpts(url){
30+
plugin.makeOpts=function(url){
3131
constopts={};
3232
opts.url=url;
3333
opts.headers={};
3434

3535
if(session.isLogin())
36-
signOpts(opts,session.getUser());
36+
plugin.signOpts(opts,session.getUser());
3737
returnopts;
38-
}
38+
};
3939

40-
functioncheckError(e,resp,expectedStatus){
40+
plugin.checkError=function(e,resp,expectedStatus){
4141
if(!e&&resp&&resp.statusCode!==expectedStatus){
4242
constcode=resp.statusCode;
4343
log.debug('http error: '+code);
@@ -49,7 +49,7 @@ function checkError(e, resp, expectedStatus) {
4949
}
5050
}
5151
returne;
52-
}
52+
};
5353

5454
plugin.init=function(){
5555
config.app='leetcode';
@@ -80,11 +80,11 @@ plugin.getProblems = function(cb) {
8080

8181
plugin.getCategoryProblems=function(category,cb){
8282
log.debug('running leetcode.getCategoryProblems: '+category);
83-
constopts=makeOpts(config.sys.urls.problems.replace('$category',category));
83+
constopts=plugin.makeOpts(config.sys.urls.problems.replace('$category',category));
8484

8585
spin.text='Downloading category '+category;
8686
request(opts,function(e,resp,body){
87-
e=checkError(e,resp,200);
87+
e=plugin.checkError(e,resp,200);
8888
if(e)returncb(e);
8989

9090
constjson=JSON.parse(body);
@@ -123,7 +123,7 @@ plugin.getProblem = function(problem, cb) {
123123
constuser=session.getUser();
124124
if(problem.locked&&!user.paid)returncb('failed to load locked problem!');
125125

126-
constopts=makeOpts(config.sys.urls.graphql);
126+
constopts=plugin.makeOpts(config.sys.urls.graphql);
127127
opts.headers.Origin=config.sys.urls.base;
128128
opts.headers.Referer=problem.link;
129129

@@ -149,7 +149,7 @@ plugin.getProblem = function(problem, cb) {
149149
constspin=h.spin('Downloading '+problem.slug);
150150
request.post(opts,function(e,resp,body){
151151
spin.stop();
152-
e=checkError(e,resp,200);
152+
e=plugin.checkError(e,resp,200);
153153
if(e)returncb(e);
154154

155155
constq=body.data.question;
@@ -191,7 +191,7 @@ function runCode(opts, problem, cb) {
191191
constspin=h.spin('Sending code to judge');
192192
request(opts,function(e,resp,body){
193193
spin.stop();
194-
e=checkError(e,resp,200);
194+
e=plugin.checkError(e,resp,200);
195195
if(e)returncb(e);
196196

197197
if(body.error){
@@ -224,7 +224,7 @@ function verifyResult(task, queue, cb) {
224224
constspin=h.spin('Waiting for judge result');
225225
request(opts,function(e,resp,body){
226226
spin.stop();
227-
e=checkError(e,resp,200);
227+
e=plugin.checkError(e,resp,200);
228228
if(e)returncb(e);
229229

230230
letresult=JSON.parse(body);
@@ -273,7 +273,7 @@ function formatResult(result) {
273273

274274
plugin.testProblem=function(problem,cb){
275275
log.debug('running leetcode.testProblem');
276-
constopts=makeOpts(config.sys.urls.test.replace('$slug',problem.slug));
276+
constopts=plugin.makeOpts(config.sys.urls.test.replace('$slug',problem.slug));
277277
opts.body={data_input:problem.testcase};
278278

279279
runCode(opts,problem,function(e,task){
@@ -292,7 +292,7 @@ plugin.testProblem = function(problem, cb) {
292292

293293
plugin.submitProblem=function(problem,cb){
294294
log.debug('running leetcode.submitProblem');
295-
constopts=makeOpts(config.sys.urls.submit.replace('$slug',problem.slug));
295+
constopts=plugin.makeOpts(config.sys.urls.submit.replace('$slug',problem.slug));
296296
opts.body={judge_type:'large'};
297297

298298
runCode(opts,problem,function(e,task){
@@ -308,11 +308,11 @@ plugin.submitProblem = function(problem, cb) {
308308

309309
plugin.getSubmissions=function(problem,cb){
310310
log.debug('running leetcode.getSubmissions');
311-
constopts=makeOpts(config.sys.urls.submissions.replace('$slug',problem.slug));
311+
constopts=plugin.makeOpts(config.sys.urls.submissions.replace('$slug',problem.slug));
312312
opts.headers.Referer=config.sys.urls.problem.replace('$slug',problem.slug);
313313

314314
request(opts,function(e,resp,body){
315-
e=checkError(e,resp,200);
315+
e=plugin.checkError(e,resp,200);
316316
if(e)returncb(e);
317317

318318
// FIXME: this only return the 1st 20 submissions, we should get next if necessary.
@@ -326,10 +326,10 @@ plugin.getSubmissions = function(problem, cb) {
326326

327327
plugin.getSubmission=function(submission,cb){
328328
log.debug('running leetcode.getSubmission');
329-
constopts=makeOpts(config.sys.urls.submission.replace('$id',submission.id));
329+
constopts=plugin.makeOpts(config.sys.urls.submission.replace('$id',submission.id));
330330

331331
request(opts,function(e,resp,body){
332-
e=checkError(e,resp,200);
332+
e=plugin.checkError(e,resp,200);
333333
if(e)returncb(e);
334334

335335
letre=body.match(/submissionCode:\s('[^']*')/);
@@ -343,7 +343,7 @@ plugin.getSubmission = function(submission, cb) {
343343

344344
plugin.starProblem=function(problem,starred,cb){
345345
log.debug('running leetcode.starProblem');
346-
constopts=makeOpts();
346+
constopts=plugin.makeOpts();
347347
opts.headers.Origin=config.sys.urls.base;
348348
opts.headers.Referer=problem.link;
349349

@@ -364,7 +364,7 @@ plugin.starProblem = function(problem, starred, cb) {
364364
}
365365

366366
request(opts,function(e,resp,body){
367-
e=checkError(e,resp,204);
367+
e=plugin.checkError(e,resp,204);
368368
if(e)returncb(e);
369369

370370
cb(null,starred);
@@ -373,12 +373,12 @@ plugin.starProblem = function(problem, starred, cb) {
373373

374374
plugin.getFavorites=function(cb){
375375
log.debug('running leetcode.getFavorites');
376-
constopts=makeOpts(config.sys.urls.favorites);
376+
constopts=plugin.makeOpts(config.sys.urls.favorites);
377377

378378
constspin=h.spin('Retrieving user favorites');
379379
request(opts,function(e,resp,body){
380380
spin.stop();
381-
e=checkError(e,resp,200);
381+
e=plugin.checkError(e,resp,200);
382382
if(e)returncb(e);
383383

384384
constfavorites=JSON.parse(body);
@@ -388,7 +388,7 @@ plugin.getFavorites = function(cb) {
388388

389389
plugin.getUserInfo=function(cb){
390390
log.debug('running leetcode.getUserInfo');
391-
constopts=makeOpts(config.sys.urls.graphql);
391+
constopts=plugin.makeOpts(config.sys.urls.graphql);
392392
opts.headers.Origin=config.sys.urls.base;
393393
opts.headers.Referer=config.sys.urls.base;
394394
opts.json=true;
@@ -407,7 +407,7 @@ plugin.getUserInfo = function(cb) {
407407
constspin=h.spin('Retrieving user profile');
408408
request.post(opts,function(e,resp,body){
409409
spin.stop();
410-
e=checkError(e,resp,200);
410+
e=plugin.checkError(e,resp,200);
411411
if(e)returncb(e);
412412

413413
constuser=body.data.user;
@@ -416,15 +416,15 @@ plugin.getUserInfo = function(cb) {
416416
};
417417

418418
functionrunSession(method,data,cb){
419-
constopts=makeOpts(config.sys.urls.session);
419+
constopts=plugin.makeOpts(config.sys.urls.session);
420420
opts.json=true;
421421
opts.method=method;
422422
opts.body=data;
423423

424424
constspin=h.spin('Waiting session result');
425425
request(opts,function(e,resp,body){
426426
spin.stop();
427-
e=checkError(e,resp,200);
427+
e=plugin.checkError(e,resp,200);
428428
if(e&&e.statusCode===302)e=session.errors.EXPIRED;
429429

430430
returne ?cb(e) :cb(null,body.sessions);
@@ -459,7 +459,7 @@ plugin.signin = function(user, cb) {
459459
constspin=h.spin('Signing in leetcode.com');
460460
request(config.sys.urls.login,function(e,resp,body){
461461
spin.stop();
462-
e=checkError(e,resp,200);
462+
e=plugin.checkError(e,resp,200);
463463
if(e)returncb(e);
464464

465465
user.loginCSRF=h.getSetCookieValue(resp,'csrftoken');

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp