nodejs_chromium 1.0.1 → 1.0.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/chrome.js +63 -54
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodejs_chromium",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/src/chrome.js CHANGED
@@ -45,8 +45,12 @@ module.exports = class {
45
45
  this.responseCall = call;
46
46
  }
47
47
 
48
+ async setCookie(cookies) {
49
+ await this.page.setCookie(...cookies);
50
+ }
51
+
48
52
  async open(url) {
49
- return await this.page.goto(url, { waitUntil: 'load' });
53
+ await this.page.goto(url, { waitUntil: 'load' });
50
54
  }
51
55
 
52
56
  /**
@@ -54,15 +58,14 @@ module.exports = class {
54
58
  *
55
59
  * @param url
56
60
  * @param data
57
- * @returns {Promise<*>}
58
61
  */
59
62
  async post(url, data) {
60
63
  //JSON.stringify(data)
61
- return await this.page.goto(url, { method: 'POST', body: data, waitUntil: 'load' });
64
+ await this.page.goto(url, { method: 'POST', body: data, waitUntil: 'load' });
62
65
  }
63
66
 
64
67
  async input(el, value) {
65
- return await this.page.type(el, value);
68
+ await this.page.type(el, value);
66
69
  }
67
70
 
68
71
  async click(el) {
@@ -87,7 +90,7 @@ module.exports = class {
87
90
  if (ele) {
88
91
  return await this.waitSelector(ele, timeout, tryCount);
89
92
  }
90
- return await this.page.waitForNavigation();
93
+ await this.page.waitForNavigation();
91
94
  }
92
95
 
93
96
  async waitSelector(ele, timeout = 0, tryCount = 0) {
@@ -104,52 +107,6 @@ module.exports = class {
104
107
  }
105
108
  }
106
109
 
107
- async parseResponse(response) {
108
- // const response = await this.page.waitForResponse(res => res);
109
-
110
- const value = {};
111
- const request = await response.request();
112
- const headers = await response.headers();
113
- value.method = await request.method();
114
- if (value.method === 'OPTIONS') return;
115
-
116
- value.type = await request.resourceType();
117
- // value.redirect = await response.redirectURL();
118
- if (value.type === 'xhr') value.type = 'AJAX';
119
- value.url = await response.url();
120
- value.domain = parseUrl(value.url)['host'];
121
- value.content = headers['content-type'];
122
- value.length = headers['content-length'];
123
- value.status = await response.status();
124
- value.ok = await response.ok();
125
- value.datetime = (new Date(headers['date'])).date('yyyy-mm-dd hh:ii:ss');
126
- // value.headers = headers;
127
- if (headers['server']) value.server = headers['server'];
128
- if (headers['set-cookie']) value.cookies = headers['set-cookie'];
129
- value.remote = await response.remoteAddress(); //目标服务器
130
- if (value.status === 301 || value.status === 302) return value;
131
- if (['image', 'font', 'other', 'script', 'stylesheet', 'document', 'ping', 'fetch'].has(value.type)) return value;
132
- if (value.content) {
133
- if (value.content.startsWith('application/vnd')) return value;
134
- if (value.content.startsWith('application/xml')) return value;
135
- if (value.content.startsWith('text/css')) return value;
136
- }
137
-
138
- value.post = await request.postData();
139
- if (value.post) value.post = value.post.toString();
140
-
141
- try {
142
- value.response = await response.buffer();
143
- value.response = value.response.toString();
144
- }
145
- catch (e) {
146
- value.response = e.parse();
147
- }
148
-
149
- return value;
150
- }
151
-
152
-
153
110
  async saveHtml(file) {
154
111
  try {
155
112
  await this.improveUrls(); //修正js/css的域名
@@ -175,7 +132,7 @@ module.exports = class {
175
132
 
176
133
  async watermark(conf) {
177
134
  await this.page.evaluate((conf) => {
178
- const wmDiv = document.createElement('div');
135
+ const wmDiv = document.createElement('div#watermark');
179
136
  wmDiv.style.position = 'fixed';
180
137
  wmDiv.style.top = `50%`;
181
138
  wmDiv.style.left = `50%`;
@@ -188,7 +145,7 @@ module.exports = class {
188
145
  wmDiv.innerText = conf.text;
189
146
  document.body.appendChild(wmDiv);
190
147
  }, conf);
191
- await this.page.waitForSelector('div');
148
+ await this.page.waitForSelector('div#watermark', { timeout: 1000 });
192
149
  }
193
150
 
194
151
 
@@ -206,7 +163,7 @@ module.exports = class {
206
163
  quality: quality,
207
164
  omitBackground: true, //显示背景
208
165
  });
209
- console.log('photograph=', file);
166
+ // console.log('photograph=', file);
210
167
  }
211
168
 
212
169
  /**
@@ -228,4 +185,56 @@ module.exports = class {
228
185
  }, domain);
229
186
  }
230
187
 
188
+
189
+ async parseResponse(response) {
190
+ // const response = await this.page.waitForResponse(res => res);
191
+
192
+ const value = {};
193
+ const request = await response.request();
194
+ const headers = await response.headers();
195
+ value.method = await request.method();
196
+ if (value.method === 'OPTIONS') return;
197
+
198
+ value.type = await request.resourceType();
199
+ // value.redirect = await response.redirectURL();
200
+ if (value.type === 'xhr') value.type = 'AJAX';
201
+ value.url = await response.url();
202
+ value.domain = parseUrl(value.url)['host'];
203
+ value.content = headers['content-type'];
204
+ value.length = headers['content-length'];
205
+ value.status = await response.status();
206
+ value.ok = await response.ok();
207
+ value.datetime = (new Date(headers['date'])).date('yyyy-mm-dd hh:ii:ss');
208
+ // value.headers = headers;
209
+ if (headers['server']) value.server = headers['server'];
210
+ if (headers['set-cookie']) value.cookies = headers['set-cookie'];
211
+ value.remote = await response.remoteAddress(); //目标服务器
212
+ if (value.status === 301 || value.status === 302) return value;
213
+ if (['image', 'font', 'other', 'script', 'stylesheet', 'document', 'ping', 'fetch'].has(value.type)) return value;
214
+ if (value.content) {
215
+ if (value.content.startsWith('application/vnd')) return value;
216
+ if (value.content.startsWith('application/xml')) return value;
217
+ if (value.content.startsWith('text/css')) return value;
218
+ }
219
+
220
+ value.post = await request.postData();
221
+ if (value.post) value.post = value.post.toString();
222
+
223
+ try {
224
+ value.response = await response.buffer();
225
+ value.response = value.response.toString();
226
+ }
227
+ catch (e) {
228
+ value.response = e.parse();
229
+ }
230
+
231
+ return value;
232
+ }
233
+
234
+
235
+
236
+
237
+
238
+
239
+
231
240
  }