Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 012e4ae

Browse files
authored
Create cookies.firefox.js
1 parent c5f6b89 commit 012e4ae

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

lib/plugins/cookies.firefox.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
var log = require('../log');
2+
var Plugin = require('../plugin');
3+
var session = require('../session');
4+
5+
// [Usage]
6+
//
7+
// https://github.com/skygragon/leetcode-cli-plugins/blob/master/docs/cookie.firefox.md
8+
//
9+
var plugin = new Plugin(13, 'cookie.firefox', '2018.11.19',
10+
'Plugin to reuse firefox\'s leetcode cookie.',
11+
['glob', 'sqlite3']);
12+
13+
function getCookieFile(cb) {
14+
var f;
15+
switch (process.platform) {
16+
case 'darwin':
17+
f = process.env.HOME + '/Library/Application Support/Firefox/Profiles/*.default*/cookies.sqlite';
18+
break;
19+
case 'linux':
20+
f = process.env.HOME + '/.mozilla/firefox/*.default*/cookies.sqlite';
21+
break;
22+
case 'win32':
23+
f = (process.env.APPDATA || '') + '/Mozilla/Firefox/Profiles/*.default*/cookies.sqlite';
24+
break;
25+
}
26+
require('glob')(f, {}, cb);
27+
}
28+
29+
function getCookies(cb) {
30+
getCookieFile(function(e, files) {
31+
if (e || files.length === 0) return cb('Not found cookie file!');
32+
33+
var sqlite3 = require('sqlite3');
34+
var db = new sqlite3.Database(files[0]);
35+
var KEYS = ['csrftoken', 'LEETCODE_SESSION'];
36+
37+
db.serialize(function() {
38+
var cookies = {};
39+
var sql = 'select name, value from moz_cookies where host like "%leetcode.com"';
40+
db.each(sql, function(e, x) {
41+
if (e) return cb(e);
42+
if (KEYS.indexOf(x.name) < 0) return;
43+
cookies[x.name] = x.value;
44+
});
45+
46+
db.close(function() {
47+
return cb(null, cookies);
48+
});
49+
});
50+
});
51+
}
52+
53+
plugin.signin = function(user, cb) {
54+
log.debug('running cookie.firefox.signin');
55+
log.debug('try to copy leetcode cookies from firefox ...');
56+
getCookies(function(e, cookies) {
57+
if (e) {
58+
log.error('Failed to copy cookies: ' + e);
59+
return plugin.next.signin(user, cb);
60+
}
61+
62+
if (!cookies.LEETCODE_SESSION || !cookies.csrftoken) {
63+
log.error('Got invalid cookies: ' + JSON.stringify(cookies));
64+
return plugin.next.signin(user, cb);
65+
}
66+
67+
log.debug('Successfully copied leetcode cookies!');
68+
user.sessionId = cookies.LEETCODE_SESSION;
69+
user.sessionCSRF = cookies.csrftoken;
70+
session.saveUser(user);
71+
return cb(null, user);
72+
});
73+
};
74+
75+
plugin.login = function(user, cb) {
76+
log.debug('running cookie.firefox.login');
77+
plugin.signin(user, function(e, user) {
78+
if (e) return cb(e);
79+
plugin.getUser(user, cb);
80+
});
81+
};
82+
83+
module.exports = plugin;

0 commit comments

Comments
 (0)