nodejs_chromium 1.1.0 → 1.1.2

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodejs_chromium",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/src/chrome.js CHANGED
@@ -590,6 +590,7 @@ module.exports = class {
590
590
  // value.redirect = await response.redirectURL();
591
591
  if (value.type === 'xhr') value.type = 'AJAX';
592
592
  value.url = await response.url();
593
+ value.headers = { request: request.headers(), response: headers };
593
594
  value.domain = parseUrl(value.url)['host'];
594
595
  value.content = headers['content-type'];
595
596
  value.length = headers['content-length'];
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
- 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
- } else if (Key === 'Max-Age') {
58
- value.expire = parseInt(arr[1]) + (Date.now() / 1000);
59
- if (value.expire > 0) value.expire_date = (value.expire * 1000).date('Y-m-d H:i:s.SSS')
60
- //有可能存在expires
61
- } else if (Key === 'Secure') {
62
- value.source = true;
63
- value.sourceScheme = 'Secure';
64
- } else {
65
- value[Key[0].toLowerCase() + Key.substring(1)] = arr[1] || true;
66
- }
67
-
68
- if (value.expires && value.expires > 0) {
69
- value.expires_date = (value.expires * 1000).date('YYYY-mm-dd HH:ii:ss.SSS')
70
- }
71
-
72
- })
73
- return value;
74
- })
75
- }
76
-
77
-
78
- /**
79
- * 获取当前页面的Cookies
80
- */
81
- async get(key) {
82
- try {
83
- await this.merge();
84
- // const cookies = await this.page.cookies();
85
- if (key === undefined) return this.cookies;
86
- return (this.cookies.filter(c => c.name === key) || [{}])[0];
87
- } catch (e) {
88
- console.log('[chrome.cookies.get.Error]', e.message);
89
- return [];
90
- }
91
- }
92
-
93
- /**
94
- * 设置Cookies
95
- * @param {Object} cookies
96
- */
97
- async set(cookies) {
98
- try {
99
- if (cookies === undefined) cookies = this.cookies;
100
- await this.page.setCookie(...cookies.map(ck => {
101
- if (typeof ck.expires === 'string') ck.expires = new Date(ck.expires).getTime();
102
- return ck;
103
- }).filter(ck => (!!ck.domain || !!ck.url)));
104
- } catch (e) {
105
- console.log('[chrome.cookies.set.Error]', e.message);
106
- console.log(JSON.stringify(cookies));
107
- }
108
- }
109
-
110
- /**
111
- * 保存当前页面中的Cookies
112
- */
113
- async save() {
114
- try {
115
- if (!this.file) return;
116
- await this.merge();
117
- await fs.writeFileSync(this.file, JSON.stringify(this.cookies, null, 2));
118
- } catch (e) {
119
- console.log('[chrome.cookies.save.Error]', e.message);
120
- }
121
- }
122
-
123
- async merge() {
124
- try {
125
- let cookies = await this.page.cookies();
126
- // console.log('merge>>>>', cookies, '<<<<<merge');
127
- if (cookies.length === 0) return;
128
-
129
- const newMap = new Map(this.cookies.map(obj => [obj.name, obj]));
130
- cookies.map(obj => {
131
- if (obj.expires && obj.expires > 0) obj.expires_date = (obj.expires * 1000).date('yyyy-mm-dd HH:ii:ss.SSS')
132
- newMap.set(obj.name, obj)
133
- });
134
- this.cookies = await Array.from(newMap.values());
135
-
136
- } catch (e) {
137
- console.log('[chrome.cookies.merge.Error]', e.message);
138
- }
139
- }
140
-
141
- /**
142
- * 合并request时自动带上的cookies
143
- *
144
- * @param cookie
145
- * @param domain
146
- * @returns {*[]|*}
147
- */
148
- async request(cookie, domain) {
149
- if (!cookie) return [];
150
- const cookies = cookie.split(';').map(ck => {
151
- const [name, value] = ck.trim().split('=');
152
- return {name, value};
153
- });
154
- const newMap = new Map(this.cookies.map(obj => [obj.name, obj]));
155
- cookies.map(obj => {
156
- const {name, value} = obj;
157
- if (!newMap.has(obj.name)) {
158
- newMap.set(obj.name, {name, value, domain})
159
- return;
160
- }
161
- const cookies = newMap.get(obj.name);
162
- if (!cookies.domain) {
163
- newMap.set(obj.name, {name, value, domain})
164
- return;
165
- }
166
- if (cookies.value === value) return;
167
-
168
- const host = '.' + cookies.domain.split('.').slice(-2).join('.');
169
- if (host === domain) {
170
- cookies.value = value;
171
- newMap.set(obj.name, cookies)
172
- }
173
- });
174
-
175
- this.cookies = await Array.from(newMap.values());
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
  }