-
Notifications
You must be signed in to change notification settings - Fork 524
/
National Standards Open System - China.js
296 lines (278 loc) · 8.21 KB
/
National Standards Open System - China.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
{
"translatorID": "cc6b64f5-5352-40ea-9196-d154cbb00d9a",
"label": "National Standards Open System - China",
"creator": "Zeping Lee, rnicrosoft",
"target": "https?://openstd\\.samr\\.gov\\.cn/",
"minVersion": "3.0",
"maxVersion": "",
"priority": 100,
"inRepository": true,
"translatorType": 4,
"browserSupport": "gcsibv",
"lastUpdated": "2023-12-29 11:33:52"
}
/*
***** BEGIN LICENSE BLOCK *****
Copyright © 2022 Zeping Lee
This file is part of Zotero.
Zotero is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Zotero is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with Zotero. If not, see <http://www.gnu.org/licenses/>.
***** END LICENSE BLOCK *****
*/
function detectWeb(doc, url) {
if (url.includes('hcno=')) {
return 'standard';
}
else if (getSearchResults(doc, true)) {
return 'multiple';
}
return false;
}
function getSearchResults(doc, checkOnly) {
var items = {};
var found = false;
var rows = doc.querySelectorAll('.mytxt > a');
for (let row of rows) {
let hcno = tryMatch(row.getAttribute('onclick'), /'(\w+)'/, 1);
let title = ZU.trimInternal(row.textContent);
if (!hcno || !title) continue;
if (checkOnly) return true;
found = true;
items[`https://openstd.samr.gov.cn/bzgk/gb/newGbInfo?hcno=${hcno}`] = title;
}
return found ? items : false;
}
async function doWeb(doc, url) {
if (detectWeb(doc, url) == 'multiple') {
let items = await Zotero.selectItems(getSearchResults(doc, false));
if (!items) return;
for (let url of Object.keys(items)) {
await scrape(await requestDocument(url));
}
}
else {
await scrape(doc, url);
}
}
async function scrape(doc, url = doc.location.href) {
var newItem = new Zotero.Item('standard');
newItem.extra = '';
let labels = new Cells(doc, '.row div.col-xs-12');
Z.debug(labels.data.map(arr => [arr[0], ZU.trimInternal(arr[1].innerText)]));
let textLabels = new TextLabels(doc, '.container table:nth-child(2)');
Z.debug(textLabels.data);
newItem.title = textLabels.get('中文标准名称');
newItem.number = tryMatch(text(doc, 'td > h1'), /:([\w /-]+)/, 1).replace('-', '—');
newItem.status = textLabels.get('标准状态').split(' ')[0];
newItem.date = labels.get(['发布日期', '实施日期']);
newItem.url = url;
newItem.language = 'zh-CN';
newItem.libraryCatalog = '国家标准全文公开系统';
newItem.extra += addExtra('original-title', textLabels.get('英文标准名称'));
newItem.extra += addExtra('CCS', labels.get('CCS'));
newItem.extra += addExtra('ICS', labels.get('ICS'));
newItem.extra += addExtra('applyDate', labels.get('实施日期'));
newItem.creators.push({
firstName: '',
lastName: labels.get(['归口部门', '主管部门']),
creatorType: 'author',
fieldMode: 1
});
newItem.attachments.push({
title: 'Snapshot',
document: doc
});
newItem.complete();
}
class Cells {
constructor(doc, selector) {
this.emptyElm = doc.createElement('div');
this.data = [];
const cells = Array.from(doc.querySelectorAll(selector)).filter(element => !element.querySelector(selector));
let i = 0;
while (cells[i + 1]) {
this.data.push([cells[i].textContent.replace(/\s*/g, ''), cells[i + 1]]);
i += 2;
}
}
get(label, element = false) {
if (Array.isArray(label)) {
let result = label
.map(aLabel => this.get(aLabel, element));
result = element
? result.find(element => element.childNodes.length)
: result.find(element => element);
return result
? result
: element
? this.emptyElm
: '';
}
const pattern = new RegExp(label);
const keyValPair = this.data.find(element => pattern.test(element[0]));
if (element) return keyValPair ? keyValPair[1] : this.emptyElm;
return keyValPair
? ZU.trimInternal(keyValPair[1].innerText)
: '';
}
}
class TextLabels {
constructor(doc, selector) {
// innerText在详情页表现良好,但在多条目表现欠佳,故统一使用经过处理的text
this.data = text(doc, selector)
.replace(/^[\s\n]*/gm, '')
.replace(/:\n/g, ': ')
.replace(/\n([^】\]::]+?\n)/g, ' $1')
.split('\n')
.map(keyVal => [
tryMatch(keyVal, /^[[【]?([\s\S]+?)[】\]::]\s*[\s\S]+$/, 1).replace(/\s/g, ''),
tryMatch(keyVal, /^[[【]?[\s\S]+?[】\]::]\s*([\s\S]+)$/, 1)
]);
}
get(label) {
if (Array.isArray(label)) {
let result = label
.map(aLabel => this.get(aLabel))
.find(value => value);
return result
? result
: '';
}
let pattern = new RegExp(label);
let keyVal = this.data.find(element => pattern.test(element[0]));
return keyVal
? ZU.trimInternal(keyVal[1])
: '';
}
}
function addExtra(key, value) {
return value
? `${key}: ${value}\n`
: '';
}
function tryMatch(string, pattern, index = 0) {
let match = string.match(pattern);
if (match && match[index]) {
return match[index];
}
return '';
}
/** BEGIN TEST CASES **/
var testCases = [
{
"type": "web",
"url": "https://openstd.samr.gov.cn/bzgk/gb/newGbInfo?hcno=ADAA308A0BE559EC29E773B71591F463",
"items": [
{
"itemType": "standard",
"title": "国际单位制及其应用",
"creators": [
{
"firstName": "",
"lastName": "国家市场监督管理总局",
"creatorType": "author",
"fieldMode": 1
}
],
"date": "1993-07-01",
"extra": "original-title: SI units and recommendations for the use of theirmultiples and of certain other units\nCCS: A51\napplyDate: 1994-07-01",
"language": "zh-CN",
"libraryCatalog": "国家标准全文公开系统",
"number": "GB 3100—1993",
"status": "现行",
"url": "https://openstd.samr.gov.cn/bzgk/gb/newGbInfo?hcno=ADAA308A0BE559EC29E773B71591F463",
"attachments": [
{
"title": "Snapshot",
"mimeType": "text/html"
}
],
"tags": [],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "https://openstd.samr.gov.cn/bzgk/gb/newGbInfo?hcno=7FA63E9BBA56E60471AEDAEBDE44B14C",
"items": [
{
"itemType": "standard",
"title": "信息与文献 参考文献著录规则",
"creators": [
{
"firstName": "",
"lastName": "国家标准化管理委员会",
"creatorType": "author",
"fieldMode": 1
}
],
"date": "2015-05-15",
"extra": "original-title: Information and documentation—Rules for bibliographic references and citations to information resources\nCCS: A14\nICS: 01.140.20\napplyDate: 2015-12-01",
"language": "zh-CN",
"libraryCatalog": "国家标准全文公开系统",
"number": "GB/T 7714—2015",
"status": "现行",
"url": "https://openstd.samr.gov.cn/bzgk/gb/newGbInfo?hcno=7FA63E9BBA56E60471AEDAEBDE44B14C",
"attachments": [
{
"title": "Snapshot",
"mimeType": "text/html"
}
],
"tags": [],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "https://openstd.samr.gov.cn/bzgk/gb/newGbInfo?hcno=38E0F18A7D25ECAA7257073408F63277",
"items": [
{
"itemType": "standard",
"title": "月球与行星原位探测相机通用规范",
"creators": [
{
"firstName": "",
"lastName": "中国科学院",
"creatorType": "author",
"fieldMode": 1
}
],
"date": "2023-11-27",
"extra": "original-title: General specification of lunar and planetary in-situ exploration camera\nCCS: N38\nICS: 17.180.01\napplyDate: 2023-11-27",
"language": "zh-CN",
"libraryCatalog": "国家标准全文公开系统",
"number": "GB/Z 43082—2023",
"status": "现行",
"url": "https://openstd.samr.gov.cn/bzgk/gb/newGbInfo?hcno=38E0F18A7D25ECAA7257073408F63277",
"attachments": [
{
"title": "Snapshot",
"mimeType": "text/html"
}
],
"tags": [],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "https://openstd.samr.gov.cn/bzgk/gb/std_list_type?p.p1=1&p.p90=circulation_date&p.p91=desc",
"items": "multiple"
}
]
/** END TEST CASES **/