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

Commit 78232d3

Browse files
committed
[cookie.chrome] support profile config.
refs skygragon#7 Signed-off-by: Eric Wang <skygragon@gmail.com>
1 parent 18605dd commit 78232d3

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

docs/cookie.chrome.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,28 @@ Make sure build environment is ready before installing plugin:
2626
$ npm install -g windows-build-tools
2727
$ npm config set msvs_version 2015 -g
2828

29+
## Config
30+
31+
* `profile`: chrome profile in use, default value is "Default".
32+
33+
*Set Config*
34+
35+
$ leetcode config plugins:cookie.chrome:profile "Your Profile"
36+
37+
*Unset Config*
38+
39+
$ leetcode config -d plugins:cookie.chrome
40+
41+
*Example*
42+
43+
{
44+
"plugins": {
45+
"cookie.chrome": {
46+
"profile": "Profile 2"
47+
}
48+
}
49+
}
50+
2951
## Usage
3052

3153
If enabled, the login will try to reuse existing chrome cookies. You can verify it by printing debug output as below.

plugins/cookie.chrome.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ var session = require('../session');
99
//
1010
// https://github.com/skygragon/leetcode-cli-plugins/blob/master/docs/cookie.chrome.md
1111
//
12-
var plugin = new Plugin(13, 'cookie.chrome', '2017.12.23',
12+
var plugin = new Plugin(13, 'cookie.chrome', '2018.05.30',
1313
'Plugin to reuse Chrome\'s leetcode cookie.',
1414
['ffi:win32', 'keytar:darwin', 'ref:win32', 'ref-struct:win32', 'sqlite3']);
1515

@@ -28,7 +28,9 @@ plugin.help = function() {
2828
var Chrome = {};
2929

3030
var ChromeMAC = {
31-
db: process.env.HOME + '/Library/Application Support/Google/Chrome/Default/Cookies',
31+
getDBPath: function() {
32+
return `${process.env.HOME}/Library/Application Support/Google/Chrome/${this.profile}/Cookies`;
33+
},
3234
iterations: 1003,
3335
getPassword: function(cb) {
3436
var keytar = require('keytar');
@@ -37,7 +39,9 @@ var ChromeMAC = {
3739
};
3840

3941
var ChromeLinux = {
40-
db: process.env.HOME + '/.config/google-chrome/Default/Cookies',
42+
getDBPath: function() {
43+
return `${process.env.HOME}/.config/google-chrome/${this.profile}/Cookies`;
44+
},
4145
iterations: 1,
4246
getPassword: function(cb) {
4347
// FIXME: keytar failed to read gnome-keyring on ubuntu??
@@ -48,7 +52,9 @@ var ChromeLinux = {
4852
};
4953

5054
var ChromeWindows = {
51-
db: path.resolve(process.env.APPDATA || '', '../Local/Google/Chrome/User Data/Default/Cookies'),
55+
getDBPath: function() {
56+
return path.resolve(process.env.APPDATA || '', `../Local/Google/Chrome/User Data/${this.profile}/Cookies`);
57+
},
5258
getPassword: function(cb) { cb(); }
5359
};
5460

@@ -124,7 +130,7 @@ function doDecode(key, queue, cb) {
124130

125131
Chrome.getCookies = function(cb) {
126132
var sqlite3 = require('sqlite3');
127-
var db = new sqlite3.Database(my.db);
133+
var db = new sqlite3.Database(my.getDBPath());
128134
var KEYS = ['csrftoken', 'LEETCODE_SESSION'];
129135

130136
db.serialize(function() {
@@ -149,9 +155,12 @@ Chrome.getCookies = function(cb) {
149155
plugin.signin = function(user, cb) {
150156
log.debug('running cookie.chrome.signin');
151157
log.debug('try to copy leetcode cookies from chrome ...');
158+
159+
my.profile = plugin.config.profile || 'Default';
152160
my.getCookies(function(e, cookies) {
153161
if (e) {
154-
log.error('failed to copy cookies: ' + e);
162+
log.error(`Failed to copy cookies from profile "${my.profile}"`);
163+
log.error(e);
155164
return plugin.next.signin(user, cb);
156165
}
157166

0 commit comments

Comments
 (0)