nodejs_chromium 1.0.1 → 1.0.3

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 +77 -63
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodejs_chromium",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/src/chrome.js CHANGED
@@ -46,7 +46,7 @@ module.exports = class {
46
46
  }
47
47
 
48
48
  async open(url) {
49
- return await this.page.goto(url, { waitUntil: 'load' });
49
+ await this.page.goto(url, { waitUntil: 'load' });
50
50
  }
51
51
 
52
52
  /**
@@ -54,15 +54,14 @@ module.exports = class {
54
54
  *
55
55
  * @param url
56
56
  * @param data
57
- * @returns {Promise<*>}
58
57
  */
59
58
  async post(url, data) {
60
59
  //JSON.stringify(data)
61
- return await this.page.goto(url, { method: 'POST', body: data, waitUntil: 'load' });
60
+ await this.page.goto(url, { method: 'POST', body: data, waitUntil: 'load' });
62
61
  }
63
62
 
64
63
  async input(el, value) {
65
- return await this.page.type(el, value);
64
+ await this.page.type(el, value);
66
65
  }
67
66
 
68
67
  async click(el) {
@@ -71,9 +70,6 @@ module.exports = class {
71
70
 
72
71
  /**
73
72
  * page.on方法
74
- *
75
- * @param {Object} key
76
- * @param {Object} call
77
73
  */
78
74
  async on(key, call) {
79
75
  await this.page.on(key, call);
@@ -83,73 +79,34 @@ module.exports = class {
83
79
  await this.browser.disconnect();
84
80
  }
85
81
 
86
- async wait(ele, timeout = 0, tryCount = 0) {
87
- if (ele) {
88
- return await this.waitSelector(ele, timeout, tryCount);
89
- }
90
- return await this.page.waitForNavigation();
91
- }
92
-
93
- async waitSelector(ele, timeout = 0, tryCount = 0) {
82
+ async navigation(timeout = 0, tryCount = 0) {
94
83
  if (timeout < 200) timeout = timeout * 1000;
95
84
  try {
96
- await this.page.waitForSelector(ele, { timeout });
85
+ await this.page.waitForNavigation({ timeout });
97
86
  return true;
98
87
  }
99
88
  catch (e) {
100
89
  if (tryCount > 0) {
101
- return await this.waitSelector(ele, timeout, --tryCount)
90
+ return await this.navigation(ele, timeout, --tryCount)
102
91
  }
103
92
  return false;
104
93
  }
105
94
  }
106
95
 
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
-
96
+ async wait(ele, timeout = 0, tryCount = 0) {
97
+ if (timeout < 200) timeout = timeout * 1000;
141
98
  try {
142
- value.response = await response.buffer();
143
- value.response = value.response.toString();
99
+ await this.page.waitForSelector(ele, { timeout });
100
+ return true;
144
101
  }
145
102
  catch (e) {
146
- value.response = e.parse();
103
+ if (tryCount > 0) {
104
+ return await this.wait(ele, timeout, --tryCount)
105
+ }
106
+ return false;
147
107
  }
148
-
149
- return value;
150
108
  }
151
109
 
152
-
153
110
  async saveHtml(file) {
154
111
  try {
155
112
  await this.improveUrls(); //修正js/css的域名
@@ -163,6 +120,14 @@ module.exports = class {
163
120
  }
164
121
  }
165
122
 
123
+ async setCookie(cookies) {
124
+ await this.page.setCookie(...cookies);
125
+ }
126
+
127
+ async getCookie() {
128
+ return await this.page.cookies();
129
+ }
130
+
166
131
  async saveCookies(file) {
167
132
  try {
168
133
  const cookies = await this.page.cookies();
@@ -175,7 +140,7 @@ module.exports = class {
175
140
 
176
141
  async watermark(conf) {
177
142
  await this.page.evaluate((conf) => {
178
- const wmDiv = document.createElement('div');
143
+ const wmDiv = document.createElement('div#watermark');
179
144
  wmDiv.style.position = 'fixed';
180
145
  wmDiv.style.top = `50%`;
181
146
  wmDiv.style.left = `50%`;
@@ -188,7 +153,7 @@ module.exports = class {
188
153
  wmDiv.innerText = conf.text;
189
154
  document.body.appendChild(wmDiv);
190
155
  }, conf);
191
- await this.page.waitForSelector('div');
156
+ await this.page.waitForSelector('div#watermark', { timeout: 1000 });
192
157
  }
193
158
 
194
159
 
@@ -196,7 +161,6 @@ module.exports = class {
196
161
  * 网页拍照
197
162
  * @param file
198
163
  * @param quality
199
- * @returns {Promise<void>}
200
164
  */
201
165
  async photograph(file, quality = 50) {
202
166
  await this.page.screenshot({
@@ -206,13 +170,11 @@ module.exports = class {
206
170
  quality: quality,
207
171
  omitBackground: true, //显示背景
208
172
  });
209
- console.log('photograph=', file);
173
+ // console.log('photograph=', file);
210
174
  }
211
175
 
212
176
  /**
213
177
  * 补全所有本地js/css
214
- * @returns {Promise<void>}
215
- * @constructor
216
178
  */
217
179
  async improveUrls() {
218
180
  const url = parseUrl(this.page.url());
@@ -228,4 +190,56 @@ module.exports = class {
228
190
  }, domain);
229
191
  }
230
192
 
193
+
194
+ async parseResponse(response) {
195
+ // const response = await this.page.waitForResponse(res => res);
196
+
197
+ const value = {};
198
+ const request = await response.request();
199
+ const headers = await response.headers();
200
+ value.method = await request.method();
201
+ if (value.method === 'OPTIONS') return;
202
+
203
+ value.type = await request.resourceType();
204
+ // value.redirect = await response.redirectURL();
205
+ if (value.type === 'xhr') value.type = 'AJAX';
206
+ value.url = await response.url();
207
+ value.domain = parseUrl(value.url)['host'];
208
+ value.content = headers['content-type'];
209
+ value.length = headers['content-length'];
210
+ value.status = await response.status();
211
+ value.ok = await response.ok();
212
+ value.datetime = (new Date(headers['date'])).date('yyyy-mm-dd hh:ii:ss');
213
+ // value.headers = headers;
214
+ if (headers['server']) value.server = headers['server'];
215
+ if (headers['set-cookie']) value.cookies = headers['set-cookie'];
216
+ value.remote = await response.remoteAddress(); //目标服务器
217
+ if (value.status === 301 || value.status === 302) return value;
218
+ if (['image', 'font', 'other', 'script', 'stylesheet', 'document', 'ping', 'fetch'].has(value.type)) return value;
219
+ if (value.content) {
220
+ if (value.content.startsWith('application/vnd')) return value;
221
+ if (value.content.startsWith('application/xml')) return value;
222
+ if (value.content.startsWith('text/css')) return value;
223
+ }
224
+
225
+ value.post = await request.postData();
226
+ if (value.post) value.post = value.post.toString();
227
+
228
+ try {
229
+ value.response = await response.buffer();
230
+ value.response = value.response.toString();
231
+ }
232
+ catch (e) {
233
+ value.response = e.parse();
234
+ }
235
+
236
+ return value;
237
+ }
238
+
239
+
240
+
241
+
242
+
243
+
244
+
231
245
  }