-
Notifications
You must be signed in to change notification settings - Fork 0
/
pro.js
279 lines (277 loc) · 9.87 KB
/
pro.js
1
2
3
4
5
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// ==UserScript==
// @name SimpICP
// @namespace http://your.namespace.com
// @version 0.1
// @description 一次图像验证码认证后搜索,格式化获取https://beian.miit.gov.cn/的备案信息,避免翻页获取数据
// @author Secur1ty0
// @match https://*/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
var storedRequests = [];
var requestLookup = [];
//------------------------------------------------------------------------
function customAjaxPost(url, requestBody, customHeaders) {
const requestOptions = {
method: 'POST',
headers: new Headers(customHeaders),
body: requestBody,
credentials: 'include',
};
return fetch(url, requestOptions)
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response.json();
})
.then(data => {
return data;
});
}
//------------------------------------------------------------------------
async function useStoredRequests(storedRequests, total, typeFlag) {
var allres = [];
var HeadersCopy = Object.assign({}, storedRequests[0].headers);
let allValues = storedRequests.flatMap(obj => Object.values(obj));
//
var url = "https://hlwicpfwc.miit.gov.cn/icpproject_query/api/icpAbbreviateInfo/queryByCondition";
delete HeadersCopy.method;
delete HeadersCopy.url;
var body0 = storedRequests[storedRequests.length - 1].body;
HeadersCopy.Cookie = storedRequests[storedRequests.length - 1].cookie;
var n100 = Math.floor(total / 100);
var remainder = total % 100;
if (n100 !== 0) {
alert("总数为" + total + "条,确认批量下载");
for (let i = 1; i <= n100; i++) {
let reqbody = JSON.parse(body0);
reqbody.pageSize = 100;
const modifiedBody = JSON.stringify(reqbody);
let res = await customAjaxPost(url, modifiedBody, HeadersCopy)
.then(response => {
if (response.code !== 200) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response.params.list;
})
allres.push(res);
};
if (remainder) {
let reqbody = JSON.parse(body0);
reqbody.pageSize = remainder;
const modifiedBody = JSON.stringify(reqbody);
let res = await customAjaxPost(url, modifiedBody, HeadersCopy)
.then(response => {
if (response.code !== 200) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response.params.list;
});
allres.push(res);
};
} else {
let reqbody = JSON.parse(body0);
reqbody.pageSize = remainder;
const modifiedBody = JSON.stringify(reqbody);
let res = await customAjaxPost(url, modifiedBody, HeadersCopy)
.then(response => {
if (response.code !== 200) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response.params.list;
});
allres.push(res);
};
let filename = JSON.parse(body0)
.unitName + ".csv";
if (remainder <= 10 && n100 == 0) {
let mergedArray = [];
for (let i = 0; i < allres.length; i++) {
mergedArray = mergedArray.concat(allres[i]);
};
var finalarray = [];
mergedArray.find(item => item.serviceType === 6);
if (typeFlag == 5) {
for (let i = 0; i < mergedArray.length; i++) {
delete mergedArray[i].domainId;
delete mergedArray[i].serviceId;
delete mergedArray[i].leaderName;
delete mergedArray[i].mainId;
delete mergedArray[i].contentTypeName;
delete mergedArray[i].limitAccess;
finalarray = finalarray.concat(mergedArray[i]);
};
}
if (typeFlag == 6) {
for (let i = 0; i < mergedArray.length; i++) {
delete mergedArray[i].cityId;
delete mergedArray[i].countyId;
delete mergedArray[i].dataId;
delete mergedArray[i].leaderName;
delete mergedArray[i].mainId;
delete mergedArray[i].mainUnitAddress;
delete mergedArray[i].mainUnitCertNo;
delete mergedArray[i].mainUnitCertType;
delete mergedArray[i].natureId;
delete mergedArray[i].provinceId;
delete mergedArray[i].serviceId;
delete mergedArray[i].serviceType;
delete mergedArray[i].version;
finalarray = finalarray.concat(mergedArray[i]);
};
}
//创建一个隐藏的 textarea 元素
const textarea = document.createElement('textarea');
textarea.style.position = 'fixed';
textarea.style.opacity = 0;
const tableText = finalarray.map(row => Object.values(row).join('\t')).join('\n');
textarea.value = tableText;
document.body.appendChild(textarea);
textarea.select();
document.execCommand('copy');
document.body.removeChild(textarea);
alert('表格数据已成功复制到剪贴板!');
} else {
let mergedArray = [];
for (let i = 0; i < allres.length; i++) {
mergedArray = mergedArray.concat(allres[i]);
};
var finalarray1 = [];
var csv = "";
if (typeFlag == 5) {
for (let i = 0; i < mergedArray.length; i++) {
delete mergedArray[i].domainId;
delete mergedArray[i].serviceId;
delete mergedArray[i].leaderName;
delete mergedArray[i].mainId;
delete mergedArray[i].contentTypeName;
delete mergedArray[i].limitAccess;
const headers = ['domain', 'mainLicence', 'natureName', 'serviceLicence', 'unitName', 'updateRecordTime'];
const values = headers.map(header => mergedArray[i][header]);
csv = csv + values.join(',') + '\n';
finalarray1 = finalarray1.concat(mergedArray[i]);
};
}
if (typeFlag == 6) {
for (let i = 0; i < mergedArray.length; i++) {
delete mergedArray[i].cityId;
delete mergedArray[i].countyId;
delete mergedArray[i].dataId;
delete mergedArray[i].leaderName;
delete mergedArray[i].mainId;
delete mergedArray[i].mainUnitAddress;
delete mergedArray[i].mainUnitCertNo;
delete mergedArray[i].mainUnitCertType;
delete mergedArray[i].natureId;
delete mergedArray[i].provinceId;
delete mergedArray[i].serviceId;
delete mergedArray[i].serviceType;
delete mergedArray[i].version;
const headers = ['natureName', 'serviceLicence', 'serviceName', 'unitName', 'updateRecordTime'];
const values = headers.map(header => mergedArray[i][header]);
csv = csv + values.join(',') + '\n';
finalarray1 = finalarray1.concat(mergedArray[i]);
};
}
const blob = new Blob(["\uFEFF" + csv], {
type: 'text/csv;charset=GB18030'
});
const link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = filename;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
alert("下载完成");
}
};
//------------------------------------------------------------------------
function logRequestAndResponse(requestBody, event, requestId) {
if (!requestLookup.includes(requestId) && event.type === 'load' && event.target.readyState === 4) {
requestLookup.push(requestId);
if (event.target.responseText.includes('total') && event.target.responseText.includes('hasNextPage')) {
var res = JSON.parse(event.target.responseText);
if (event.target.responseText.includes("domainId")) {
const listData = res.params.list
let total = res.params.total;
useStoredRequests(storedRequests, total, 5);
} else {
const listData = res.params.list
let total = res.params.total;
useStoredRequests(storedRequests, total, 6);
}
}
};
}
//------------------------------------------------------------------------
let requestBodies = new WeakMap();
var flag = 0;
if (window.XMLHttpRequest) {
const originalOpen = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function(method, url, async, user, password) {
if (url.includes("icpAbbreviateInfo/queryByCondition")) {
this._customRequestHeaders = {};
this._customRequestHeaders.method = method;
this._customRequestHeaders.url = url;
this._method = method;
this._url = url;
flag = 1;
var originalSetRequestHeader = XMLHttpRequest.prototype.setRequestHeader;
XMLHttpRequest.prototype._customRequestHeaders = {};
XMLHttpRequest.prototype.setRequestHeader = function(header, value) {
this._customRequestHeaders[header] = value;
return originalSetRequestHeader.apply(this, arguments);
};
}
requestBodies.set(this, null);
return originalOpen.apply(this, arguments);
};
const originalSend = XMLHttpRequest.prototype.send;
//------------------------------------------------------------------------
XMLHttpRequest.prototype.send = function(body) {
if (body.includes("pageSize")) {
var self = this;
var headerString = JSON.stringify(self._customRequestHeaders);
var requestId = `${this._method}:${this._url}:${headerString}:${body}`;
let requestBody = JSON.parse(body);
//请求头
for (var header in self._customRequestHeaders) {
}
//------------------------------------------------------------------------
if (!requestLookup.includes(requestId)) {
//requestLookup.push(requestId);
storedRequests.push({
//创建了一个新的对象,它与 _customRequestHeaders 的内容相同,但是是独立的
headers: Object.assign({}, self._customRequestHeaders),
body: body,
method: self._method,
url: self._url,
cookie: document.cookie
});
} else {
}
//------------------------------------------------------------------------
try {
if (requestBody && requestBody.hasOwnProperty('pageSize')) {
requestBody.pageSize = '10';
body = JSON.stringify(requestBody);
requestBodies.set(this, body);
this.addEventListener('load', function(event) {
logRequestAndResponse(requestBodies.get(this), event, requestId);
});
return originalSend.call(this, body);
} else {
requestBodies.set(this, arguments);
return originalSend.apply(this, arguments);
}
} catch (e) {
console.error('Failed to parse the request body as JSON.', e);
}
} else {
return originalSend.apply(this, arguments);
}
}
};
})();