nodejs_chromium 1.0.0 → 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 +5 -5
  2. package/src/chrome.js +74 -61
package/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "nodejs_chromium",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
7
  "test": "echo \"Error: no test specified\" && exit 1"
8
- },
9
- "dependencies": {
10
- "nodejs_patch": ">1.0.5",
11
- "puppeteer": "^21.10.0"
8
+ },
9
+ "dependencies": {
10
+ "nodejs_patch": ">1.0.5",
11
+ "puppeteer": "^21.10.0"
12
12
  },
13
13
  "author": "fazo@qq.com",
14
14
  "license": "ISC"
package/src/chrome.js CHANGED
@@ -1,10 +1,10 @@
1
- const fs = require("fs");
1
+ const { writeFileSync: saveFile } = require("fs");
2
2
  const { parse: parseUrl } = require("url");
3
3
 
4
4
  module.exports = class {
5
5
  browser = null;
6
6
  page = null;
7
- response = [];
7
+ responseCall = null;
8
8
 
9
9
  constructor(browser, page, params) {
10
10
  this.browser = browser;
@@ -35,14 +35,22 @@ module.exports = class {
35
35
  });
36
36
 
37
37
  page.on('response', async res => {
38
+ if (!this.responseCall) return;
38
39
  let json = await this.parseResponse(res);
39
- if (json.method === 'POST') console.log(json);
40
- if (json.body) this.response.push(json);
40
+ this.responseCall(json);
41
41
  });
42
42
  }
43
43
 
44
+ response(call) {
45
+ this.responseCall = call;
46
+ }
47
+
48
+ async setCookie(cookies) {
49
+ await this.page.setCookie(...cookies);
50
+ }
51
+
44
52
  async open(url) {
45
- return await this.page.goto(url, { waitUntil: 'load' });
53
+ await this.page.goto(url, { waitUntil: 'load' });
46
54
  }
47
55
 
48
56
  /**
@@ -50,15 +58,14 @@ module.exports = class {
50
58
  *
51
59
  * @param url
52
60
  * @param data
53
- * @returns {Promise<*>}
54
61
  */
55
62
  async post(url, data) {
56
63
  //JSON.stringify(data)
57
- return await this.page.goto(url, { method: 'POST', body: data, waitUntil: 'load' });
64
+ await this.page.goto(url, { method: 'POST', body: data, waitUntil: 'load' });
58
65
  }
59
66
 
60
67
  async input(el, value) {
61
- return await this.page.type(el, value);
68
+ await this.page.type(el, value);
62
69
  }
63
70
 
64
71
  async click(el) {
@@ -67,7 +74,7 @@ module.exports = class {
67
74
 
68
75
  /**
69
76
  * page.on方法
70
- *
77
+ *
71
78
  * @param {Object} key
72
79
  * @param {Object} call
73
80
  */
@@ -83,7 +90,7 @@ module.exports = class {
83
90
  if (ele) {
84
91
  return await this.waitSelector(ele, timeout, tryCount);
85
92
  }
86
- return await this.page.waitForNavigation();
93
+ await this.page.waitForNavigation();
87
94
  }
88
95
 
89
96
  async waitSelector(ele, timeout = 0, tryCount = 0) {
@@ -100,59 +107,13 @@ module.exports = class {
100
107
  }
101
108
  }
102
109
 
103
- async parseResponse(response) {
104
- // const response = await this.page.waitForResponse(res => res);
105
-
106
- const value = {};
107
- const request = await response.request();
108
- const headers = await response.headers();
109
- value.method = await request.method();
110
- if (value.method === 'OPTIONS') return;
111
-
112
- value.type = await request.resourceType();
113
- // value.redirect = await response.redirectURL();
114
- if (value.type === 'xhr') value.type = 'AJAX';
115
- value.url = await response.url();
116
- value.domain = parseUrl(value.url)['host'];
117
- value.content = headers['content-type'];
118
- value.length = headers['content-length'];
119
- value.status = await response.status();
120
- value.ok = await response.ok();
121
- value.datetime = (new Date(headers['date'])).date('yyyy-mm-dd hh:ii:ss');
122
- // value.headers = headers;
123
- if (headers['server']) value.server = headers['server'];
124
- if (headers['set-cookie']) value.cookies = headers['set-cookie'];
125
- value.remote = await response.remoteAddress(); //目标服务器
126
- if (value.status === 301 || value.status === 302) return value;
127
- if (['image', 'font', 'other', 'script', 'stylesheet', 'document', 'ping', 'fetch'].has(value.type)) return value;
128
- if (value.content) {
129
- if (value.content.startsWith('application/vnd')) return value;
130
- if (value.content.startsWith('application/xml')) return value;
131
- if (value.content.startsWith('text/css')) return value;
132
- }
133
-
134
- value.post = await request.postData();
135
- if (value.post) value.post = value.post.toString();
136
-
137
- try {
138
- value.response = await response.buffer();
139
- value.response = value.response.toString();
140
- }
141
- catch (e) {
142
- value.response = e.parse();
143
- }
144
-
145
- return value;
146
- }
147
-
148
-
149
110
  async saveHtml(file) {
150
111
  try {
151
112
  await this.improveUrls(); //修正js/css的域名
152
113
  const body = await this.page.evaluate(() => {
153
114
  return document.documentElement.innerHTML;
154
115
  });
155
- await fs.writeFileSync(file, body);
116
+ await saveFile(file, body);
156
117
  }
157
118
  catch (e) {
158
119
  console.log('saveHtml Error:', e.parse());
@@ -162,7 +123,7 @@ module.exports = class {
162
123
  async saveCookies(file) {
163
124
  try {
164
125
  const cookies = await this.page.cookies();
165
- await fs.writeFileSync(file, JSON.stringify(cookies, null, 2));
126
+ await saveFile(file, JSON.stringify(cookies, null, 2));
166
127
  }
167
128
  catch (e) {
168
129
  console.log('saveHtml Error:', e.parse());
@@ -171,7 +132,7 @@ module.exports = class {
171
132
 
172
133
  async watermark(conf) {
173
134
  await this.page.evaluate((conf) => {
174
- const wmDiv = document.createElement('div');
135
+ const wmDiv = document.createElement('div#watermark');
175
136
  wmDiv.style.position = 'fixed';
176
137
  wmDiv.style.top = `50%`;
177
138
  wmDiv.style.left = `50%`;
@@ -184,7 +145,7 @@ module.exports = class {
184
145
  wmDiv.innerText = conf.text;
185
146
  document.body.appendChild(wmDiv);
186
147
  }, conf);
187
- await this.page.waitForSelector('div');
148
+ await this.page.waitForSelector('div#watermark', { timeout: 1000 });
188
149
  }
189
150
 
190
151
 
@@ -202,7 +163,7 @@ module.exports = class {
202
163
  quality: quality,
203
164
  omitBackground: true, //显示背景
204
165
  });
205
- console.log('photograph=', file);
166
+ // console.log('photograph=', file);
206
167
  }
207
168
 
208
169
  /**
@@ -224,4 +185,56 @@ module.exports = class {
224
185
  }, domain);
225
186
  }
226
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
+
227
240
  }