nodejs_chromium 1.1.0 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/cookies.js +187 -178
package/package.json
CHANGED
package/src/cookies.js
CHANGED
|
@@ -1,179 +1,188 @@
|
|
|
1
|
-
const fs = require("fs");
|
|
2
|
-
const {parse: parseUrl} = require("url");
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
module.exports = class {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
const { parse: parseUrl } = require("url");
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
module.exports = class {
|
|
6
|
+
page = void 0;
|
|
7
|
+
file = void 0;
|
|
8
|
+
cookies = [];
|
|
9
|
+
|
|
10
|
+
constructor(page, cookies) {
|
|
11
|
+
this.page = page;
|
|
12
|
+
if (cookies === false) return;
|
|
13
|
+
|
|
14
|
+
this.file = cookies;
|
|
15
|
+
if (!fs.existsSync(cookies)) return;
|
|
16
|
+
// console.log('cookies file:', cookies);
|
|
17
|
+
let cookiesVal = fs.readFileSync(cookies, 'utf8');
|
|
18
|
+
if (cookiesVal === '{}') cookiesVal = '[]';
|
|
19
|
+
this.cookies = JSON.parse(cookiesVal);
|
|
20
|
+
// this.set(this.cookies);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
json(str, host) {
|
|
24
|
+
if (!str) return [];
|
|
25
|
+
return JSON.parse(str).map(cook => {
|
|
26
|
+
let { name, value, domain } = cook;
|
|
27
|
+
if (!domain) domain = host;
|
|
28
|
+
return { name, value, domain };
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
|
|
34
|
+
// const url = parseUrl(this.page.url());
|
|
35
|
+
// const host = '.' + url.host.split('.').slice(-2).join('.');
|
|
36
|
+
// let cookiesVal = read(file, 'utf8');
|
|
37
|
+
// cookiesVal = this.jsonArray(cookiesVal, host);
|
|
38
|
+
|
|
39
|
+
*/
|
|
40
|
+
/**
|
|
41
|
+
* 解析网页set-cookies的值
|
|
42
|
+
*
|
|
43
|
+
* @param {Object} strCookies
|
|
44
|
+
*/
|
|
45
|
+
async parse(strCookies) {
|
|
46
|
+
return await strCookies.split("\n").map((ls) => {
|
|
47
|
+
let value = {};
|
|
48
|
+
ls.split(';').map((ln, j) => {
|
|
49
|
+
// console.log(ln);
|
|
50
|
+
const arr = ln.split('=');
|
|
51
|
+
const Key = (arr[0]).trim();
|
|
52
|
+
if (!Key) return;
|
|
53
|
+
|
|
54
|
+
if (j === 0) {
|
|
55
|
+
value.name = Key;
|
|
56
|
+
value.value = arr[1];
|
|
57
|
+
}
|
|
58
|
+
else if (Key === 'Max-Age') {
|
|
59
|
+
value.expire = parseInt(arr[1]) + (Date.now() / 1000);
|
|
60
|
+
if (value.expire > 0) value.expire_date = (value.expire * 1000).date('Y-m-d H:i:s.SSS')
|
|
61
|
+
//有可能存在expires
|
|
62
|
+
}
|
|
63
|
+
else if (Key === 'Secure') {
|
|
64
|
+
value.source = true;
|
|
65
|
+
value.sourceScheme = 'Secure';
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
value[Key[0].toLowerCase() + Key.substring(1)] = arr[1] || true;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (value.expires && value.expires > 0) {
|
|
72
|
+
value.expires_date = (value.expires * 1000).date('YYYY-mm-dd HH:ii:ss.SSS')
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
})
|
|
76
|
+
return value;
|
|
77
|
+
})
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* 获取当前页面的Cookies
|
|
83
|
+
*/
|
|
84
|
+
async get(key) {
|
|
85
|
+
try {
|
|
86
|
+
await this.merge();
|
|
87
|
+
// const cookies = await this.page.cookies();
|
|
88
|
+
if (key === undefined) return this.cookies;
|
|
89
|
+
return (this.cookies.filter(c => c.name === key) || [{}])[0];
|
|
90
|
+
}
|
|
91
|
+
catch (e) {
|
|
92
|
+
console.log('[chrome.cookies.get.Error]', e.message);
|
|
93
|
+
return [];
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* 设置Cookies
|
|
99
|
+
* @param {Object} cookies
|
|
100
|
+
*/
|
|
101
|
+
async set(cookies) {
|
|
102
|
+
try {
|
|
103
|
+
if (cookies === undefined) cookies = this.cookies;
|
|
104
|
+
await this.page.setCookie(...cookies.map(ck => {
|
|
105
|
+
if (typeof ck.expires === 'string') ck.expires = new Date(ck.expires).getTime();
|
|
106
|
+
return ck;
|
|
107
|
+
}).filter(ck => (!!ck.domain || !!ck.url)));
|
|
108
|
+
}
|
|
109
|
+
catch (e) {
|
|
110
|
+
console.log('[chrome.cookies.set.Error]', e.message);
|
|
111
|
+
console.log(JSON.stringify(cookies));
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* 保存当前页面中的Cookies
|
|
117
|
+
*/
|
|
118
|
+
async save() {
|
|
119
|
+
try {
|
|
120
|
+
if (!this.file) return [];
|
|
121
|
+
await this.merge();
|
|
122
|
+
await fs.writeFileSync(this.file, JSON.stringify(this.cookies, null, 2));
|
|
123
|
+
return this.cookies;
|
|
124
|
+
}
|
|
125
|
+
catch (e) {
|
|
126
|
+
console.log('[chrome.cookies.save.Error]', e.message);
|
|
127
|
+
return [];
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
async merge() {
|
|
132
|
+
try {
|
|
133
|
+
let cookies = await this.page.cookies();
|
|
134
|
+
// console.log('merge>>>>', cookies, '<<<<<merge');
|
|
135
|
+
if (cookies.length === 0) return;
|
|
136
|
+
|
|
137
|
+
const newMap = new Map(this.cookies.map(obj => [obj.name, obj]));
|
|
138
|
+
cookies.map(obj => {
|
|
139
|
+
if (obj.expires && obj.expires > 0) obj.expires_date = (obj.expires * 1000).date('yyyy-mm-dd HH:ii:ss.SSS')
|
|
140
|
+
newMap.set(obj.name, obj)
|
|
141
|
+
});
|
|
142
|
+
this.cookies = await Array.from(newMap.values());
|
|
143
|
+
|
|
144
|
+
}
|
|
145
|
+
catch (e) {
|
|
146
|
+
console.log('[chrome.cookies.merge.Error]', e.message);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* 合并request时自动带上的cookies
|
|
152
|
+
*
|
|
153
|
+
* @param cookie
|
|
154
|
+
* @param domain
|
|
155
|
+
* @returns {*[]|*}
|
|
156
|
+
*/
|
|
157
|
+
async request(cookie, domain) {
|
|
158
|
+
if (!cookie) return [];
|
|
159
|
+
const cookies = cookie.split(';').map(ck => {
|
|
160
|
+
const [name, value] = ck.trim().split('=');
|
|
161
|
+
return { name, value };
|
|
162
|
+
});
|
|
163
|
+
const newMap = new Map(this.cookies.map(obj => [obj.name, obj]));
|
|
164
|
+
cookies.map(obj => {
|
|
165
|
+
const { name, value } = obj;
|
|
166
|
+
if (!newMap.has(obj.name)) {
|
|
167
|
+
newMap.set(obj.name, { name, value, domain })
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
const cookies = newMap.get(obj.name);
|
|
171
|
+
if (!cookies.domain) {
|
|
172
|
+
newMap.set(obj.name, { name, value, domain })
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
if (cookies.value === value) return;
|
|
176
|
+
|
|
177
|
+
const host = '.' + cookies.domain.split('.').slice(-2).join('.');
|
|
178
|
+
if (host === domain) {
|
|
179
|
+
cookies.value = value;
|
|
180
|
+
newMap.set(obj.name, cookies)
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
this.cookies = await Array.from(newMap.values());
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
|
|
179
188
|
}
|