-
Notifications
You must be signed in to change notification settings - Fork 524
/
MagTech.js
667 lines (644 loc) · 16.1 KB
/
MagTech.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
{
"translatorID": "69614630-f9e8-440d-9a7c-1f4fd7edf191",
"label": "MagTech",
"creator": "jiaojiaodubai23",
"target": "^https?://.*(/CN/)?.*(\\.shtml)?",
"minVersion": "5.0",
"maxVersion": "",
"priority": 200,
"inRepository": true,
"translatorType": 4,
"browserSupport": "gcsibv",
"lastUpdated": "2024-06-20 16:34:51"
}
/*
***** BEGIN LICENSE BLOCK *****
Copyright © 2022 [email protected]
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 *****
*/
const webType = [
// http://cea.ceaj.org/CN/10.3778/j.issn.1002-8331.2302-0361
{
name: 'flat',
issingle: true,
pagekey: 'body[id="goTop"]',
},
// http://www.zgxdyyzz.com.cn/CN/10.3969/j.issn.1672-9463.2022.03.019
{
name: 'flat_float',
issingle: true,
pagekey: 'body[id="metaVue"]',
},
// http://www.zgyj.ac.cn/CN/abstract/abstract9973.shtml
{
name: 'notebook_classic',
issingle: true,
pagekey: 'div[id="tabsDiv"]',
},
// http://jst.tsinghuajournals.com/CN/Y2015/V55/I1/50
{
name: 'notebook_morden',
issingle: true,
pagekey: 'ul[class="tabs-nav"]'
},
// http://jst.tsinghuajournals.com/CN/1000-0054/home.shtml
{
name: 'notebook_flat',
issingle: true,
pagekey: 'div[class="center row"] ul[class="nav nav-tabs"]',
},
// https://www.ams.org.cn/CN/0412-1961/home.shtml
{
name: 'biaoti',
issingle: false,
pagekey: 'li[class="biaoti"] a:first-of-type'
},
// http://www.syxb-cps.com.cn/CN/0253-2697/home.shtml
{
name: 'wenzhang',
issingle: false,
pagekey: 'div[class="wenzhang"] dd:first-of-type a:first-of-type'
},
// http://www.sykjlt.com/CN/1002-302X/home.shtml
{
name: 'txt_biaoti',
issingle: false,
pagekey: 'form[id="AbstractList"] td > a[class="txt_biaoti"]:first-child'
},
// http://www.zgswfz.com.cn/CN/2095-039X/home.shtml
{
name: 'form+title',
issingle: false,
pagekey: 'form[name="AbstractList"] span[class="title"]'
},
// http://www.zgyxcx.com/CN/volumn/home.shtml
{
name: 'title',
issingle: false,
pagekey: 'div[class="article-list"] div[class="j-title"] a:first-of-type'
},
// http://www.suidaojs.com/CN/2096-4498/home.shtml
{
name: "title-1-hight",
issingle: false,
pagekey: 'ul[class="article-list article-list-height"] div[class="j-title-1"] > a:first-child'
},
// http://www.cjn.org.cn/CN/home
{
name: "title-1",
issingle: false,
pagekey: 'ul[class="article-list"] div[class="j-title-1"] > a:first-child'
},
// http://www.dwjs.com.cn
{
name: "manytable",
issingle: false,
pagekey: 'form[name="AbstractList"] > table td > table td > table td > a:nth-child(1) > u'
}
];
/* 外来的Metadata.js水土不服,决定自己造轮子 */
class Metas {
constructor(doc) {
this.doc = doc;
}
getMeta(recipes, lang) {
var result;
try {
var recipe = recipes.find(element => (this.doc.querySelectorAll(`head > meta[name="${element.name}"]`).length));
// if (recipe) {
result = this.doc.querySelectorAll(`head > meta[name="${recipe.name}"]`);
result = Array.from(result);
Z.debug(`In recipe ${JSON.stringify(recipe)},I got:\n${result.map(element => (element.content))}`);
result = result.filter(element => (!element.getAttribute('xml:lang') || (element.getAttribute('xml:lang') == lang)));
result = result.map(element => (element.content));
// Z.debug(`after filtrating, result is ${result}`);
if (recipe.callback) {
result = recipe.callback(result);
// Z.debug(`after callbacking, result is ${result}`);
}
else {
result = (result.length == 1) ? result[0] : result;
}
return result;
// }
}
catch (error) {
return '';
}
}
}
const metaMap = {
title: [
{ name: "citation_title", },
{ name: "DC.Title", },
{ name: "dc.title" }
],
abstractNote: [
{ name: "dc.description" },
{ name: "Description" },
{ name: "description" }
],
publicationTitle: [
{ name: "prism.publicationName" },
{ name: "citation_journal_title" },
{ name: "dc.source" }
],
volume: [
{ name: "citation_volume" }
],
issue: [
{ name: "citation_issue" }
],
firstpage: [
{ name: "prism.startingPage" },
{ name: "citation_firstpage" }
],
lastpage: [
{ name: "prism.endingPage" },
{ name: "citation_lastpage" }
],
date: [
{ name: "prism.publicationDate" },
{ name: "citation_publication_date" },
{ name: "citation_online_date" },
{ name: "citation_date" },
{ name: "dc.date" }
],
journalAbbreviation: [
{ name: "citation_journal_abbrev" },
],
DOI: [
{ name: "citation_doi" },
{ name: "DOI" },
{ name: "prism.doi" }
],
ISSN: [
{ name: "prism.issn" },
{ name: "citation_issn" }
],
rights: [
{ name: "prism.copyright" },
{ name: "dc.copyright" }
],
// extra: "其他",
creators: [
{
name: "citation_author",
callback: cleanAutorArr
},
{
name: "citation_authors",
callback: cleanAutorStr
},
{
name: "dc.creator",
callback: cleanAutorArr
},
{
name: "DC.Contributor",
callback: cleanAutorArr
},
{
name: "authors",
callback: cleanAutorStr
}
],
tags: [
{
name: "dc.subject",
callback: cleanKeywordsArr
},
{
name: "citation_keywords",
callback: cleanKeywordsArr
},
{
name: "DC.Keywords",
callback: cleanKeywordsStr
},
{
name: "keywords",
callback: cleanKeywordsStr
}
],
};
const tagMap = {
title: "T",
date: "D",
DOI: "R",
publicationTitle: "J",
pages: "P",
volume: "V",
issue: "N",
abstractNote: "X",
url: "U"
};
function isItem(doc) {
let insite = doc.querySelector('a[href*="www.magtech.com"]');
let havetitle = (new Metas(doc)).getMeta(metaMap.title);
let like = doc.querySelector('[class*="magtech"]');
if (havetitle && (insite || like)) {
return {
name: 'Unknown-type',
issingle: true
};
}
return false;
}
function detectWeb(doc, _url) {
var type = webType.find(element => (doc.querySelector(element.pagekey)));
if (!type) type = isItem(doc);
if (!type) return false;
Z.debug(type);
if (type.issingle) {
return 'journalArticle';
}
else if (getSearchResults(doc, type, true)) {
return 'multiple';
}
return false;
}
function getSearchResults(doc, type, checkOnly) {
var items = {};
var found = false;
if (type.name == 'manytable') {
Z.debug('type is manytable');
let elements = Array.from(doc.querySelectorAll('form[name="AbstractList"] > table:only-child td:only-child > table td > table'));
// Z.debug(elements.map((element) => (element.innerText)));
for (let element of elements) {
let innerText = element.querySelector('td > a:nth-child(1) > u');
if (innerText && innerText.textContent == '摘要') {
let href = innerText.parentElement.href;
let title = element.querySelector('td > b').innerText;
Z.debug(`${href}\n${title}`);
if (!href || !title) continue;
if (checkOnly) return true;
found = true;
items[href] = title;
}
}
return found ? items : false;
}
else {
var rows = doc.querySelectorAll(type.pagekey);
for (let row of rows) {
let href = row.href;
let title = ZU.trimInternal(row.textContent);
if (!href || !title) continue;
if (checkOnly) return true;
found = true;
items[href] = title;
}
return found ? items : false;
}
}
async function doWeb(doc, url) {
var type = webType.find(element => (doc.querySelector(element.pagekey)));
if (!type) type = isItem(doc);
if (detectWeb(doc, url) == 'multiple') {
let items = await Zotero.selectItems(getSearchResults(doc, type, false));
if (!items) return;
Z.debug(items);
for (let url of Object.keys(items)) {
await doWeb(await requestDocument(url), url);
}
}
else {
await scrape(doc, type, url);
}
}
function cleanAutorArr(creators) {
creators = creators.map(creator => (matchCreator(creator)));
return creators;
}
function cleanAutorStr(creators) {
creators = str2arr(creators[0]);
return cleanAutorArr(creators);
}
function matchCreator(creator) {
if (/[A-Za-z]/.test(creator)) {
creator = ZU.cleanAuthor(creator, 'author');
}
else {
creator = creator.replace(/\s/g, '');
creator = {
lastName: creator,
creatorType: 'author',
fieldMode: 1
};
}
return creator;
}
function cleanKeywordsArr(keywords) {
keywords = keywords.map(keyword => ({ tag: keyword }));
return keywords;
}
function cleanKeywordsStr(keywords) {
keywords = str2arr(keywords[0]);
keywords = cleanKeywordsArr(keywords);
return keywords;
}
function str2arr(string) {
return string.split(/[,;,;]\s?/);
}
async function scrape(doc, type, url = doc.location.href) {
// Z.debug(type);
var metas = new Metas(doc);
var newItem = new Z.Item('journalArticle');
var language = [
{ name: "citation_language" },
{ name: "DC.Language" },
{ name: "dc.language" }
];
language = metas.getMeta(language).toLowerCase();
// Z.debug(`document language is ${language}`);
var fields = Object.keys(metaMap);
for (let i = 0; i < fields.length; i++) {
let key = fields[i];
newItem[key] = metas.getMeta(metaMap[key], language);
}
newItem.pages = (function () {
let firstpage = newItem.firstpage;
let lastpage = newItem.lastpage;
delete newItem.firstpage;
delete newItem.lastpage;
return (firstpage == lastpage || !lastpage) ? firstpage : `${firstpage}-${lastpage}`;
})();
newItem.language = (function () {
return (['zh', 'cn', 'zh-cn', 'chi'].includes(language)) ? 'zh-CN' : 'en-US';
})();
newItem.url = url;
newItem.attachments.push({
title: 'Snapshot',
document: doc
});
newItem.attachments.push({
url: metas.getMeta([{ name: "citation_pdf_url" }]),
title: 'Full Text PDF',
mimeType: 'application/pdf'
});
if (type.name == 'flat' || type.name == 'notebook_classic') {
let risURL = doc.querySelector('#ris_export').href;
// Z.debug(risURL)
let risText = await requestText(risURL);
risText = risText.split('\r\n');
// Z.debug(risText);
for (const field in tagMap) {
const tag = tagMap[field];
var value;
try {
value = (risText.find(line => (line.charAt(1) == tag))).substring(3);
}
catch (erro) {
value = '';
}
if (!newItem.field || field == 'abstractNote') {
newItem.field = value;
}
}
}
// Z.debug(newItem);
newItem.complete();
}
/** BEGIN TEST CASES **/
var testCases = [
{
"type": "web",
"url": "https://www.xdfyjs.cn/CN/1009-086X/home.shtml",
"items": "multiple"
},
{
"type": "web",
"url": "http://www.zgswfz.com.cn/CN/10.16409/j.cnki.2095-039x.2018.01.002",
"items": [
{
"itemType": "journalArticle",
"title": "植食性害虫食诱剂的研究与应用",
"creators": [
{
"lastName": "蔡晓明",
"creatorType": "author",
"fieldMode": 1
},
{
"lastName": "李兆群",
"creatorType": "author",
"fieldMode": 1
},
{
"lastName": "潘洪生",
"creatorType": "author",
"fieldMode": 1
},
{
"lastName": "陆宴辉",
"creatorType": "author",
"fieldMode": 1
}
],
"date": "2018-02-08",
"DOI": "10.16409/j.cnki.2095-039x.2018.01.002",
"ISSN": "2095-039X",
"abstractNote": "植食性害虫主要取食植物的茎叶、果实、花蜜等,并且常对某些食物...",
"issue": "1",
"language": "zh-CN",
"libraryCatalog": "MagTech",
"pages": "8",
"publicationTitle": "中国生物防治学报",
"url": "http://www.zgswfz.com.cn/CN/10.16409/j.cnki.2095-039x.2018.01.002",
"volume": "34",
"attachments": [
{
"title": "Snapshot",
"mimeType": "text/html"
},
{
"title": "Full Text PDF",
"mimeType": "application/pdf"
}
],
"tags": [
{
"tag": "偏好选择"
},
{
"tag": "寄主植物"
},
{
"tag": "挥发物"
},
{
"tag": "绿色防控"
},
{
"tag": "行为调控"
}
],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "http://jst.tsinghuajournals.com/CN/Y2015/V55/I1/50",
"items": [
{
"itemType": "journalArticle",
"title": "施工粉尘健康损害量化评价",
"creators": [
{
"lastName": "李小冬",
"creatorType": "author",
"fieldMode": 1
},
{
"lastName": "苏舒",
"creatorType": "author",
"fieldMode": 1
},
{
"lastName": "黄天健",
"creatorType": "author",
"fieldMode": 1
}
],
"date": "2015-01-20",
"ISSN": "1000-0054",
"issue": "1",
"journalAbbreviation": "清华大学学报(自然科学版)",
"language": "zh-CN",
"libraryCatalog": "MagTech",
"pages": "50-55",
"publicationTitle": "清华大学学报(自然科学版)",
"url": "http://jst.tsinghuajournals.com/CN/Y2015/V55/I1/50",
"volume": "55",
"attachments": [
{
"title": "Snapshot",
"mimeType": "text/html"
},
{
"title": "Full Text PDF",
"mimeType": "application/pdf"
}
],
"tags": [
{
"tag": "健康损害"
},
{
"tag": "施工粉尘"
},
{
"tag": "环境影响评价"
},
{
"tag": "量化评价"
}
],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "http://www.dwjs.com.cn/T2JUSGUU2P9AedAuY61LaRnnjULDzh%2Bv_w8bZtqBahG9wUCJsceKzRtv6B_%2B%2B9%2BO?encrypt=1",
"items": [
{
"itemType": "journalArticle",
"title": "新型电力系统的“碳视角”:科学问题与研究框架",
"creators": [
{
"lastName": "康重庆",
"creatorType": "author",
"fieldMode": 1
},
{
"lastName": "杜尔顺",
"creatorType": "author",
"fieldMode": 1
},
{
"lastName": "李姚旺",
"creatorType": "author",
"fieldMode": 1
},
{
"lastName": "张宁",
"creatorType": "author",
"fieldMode": 1
},
{
"lastName": "陈启鑫",
"creatorType": "author",
"fieldMode": 1
},
{
"lastName": "郭鸿业",
"creatorType": "author",
"fieldMode": 1
},
{
"lastName": "王鹏",
"creatorType": "author",
"fieldMode": 1
}
],
"date": "2022-03-05",
"DOI": "10.13335/j.1000-3673.pst.2021.2550",
"ISSN": "1000-3673",
"abstractNote": "电力在我国能源消费与碳排放中占据重要地位。电力系统低碳转型,构建以新能源为主体的新型电力系统将对我国碳达峰、碳中和战略目标的实现起到关键作用。该文首先分析了从“电视角”到“碳视角”下电力学科研究体系的转变趋势,并对当前“碳视角”下的电力系统研究概况进行了综述。基于“碳视角”下的电力系统研究路径,从电力系统全环节碳排放计量和“战略-技术-市场”协同低碳化解决方案2个方面,分析了电力低碳转型过程中的关键科学问题。在此基础上,从碳计量与碳追踪、碳规划与碳轨迹、碳减排与碳优化、碳市场与碳交易4个方面提出了新型电力系统“碳视角”的研究框架,并对关键研究内容进行了分析和阐述。",
"issue": "3",
"language": "zh-CN",
"libraryCatalog": "MagTech",
"pages": "821-833",
"publicationTitle": "电网技术",
"url": "http://www.dwjs.com.cn/T2JUSGUU2P9AedAuY61LaRnnjULDzh%2Bv_w8bZtqBahG9wUCJsceKzRtv6B_%2B%2B9%2BO?encrypt=1",
"volume": "46",
"attachments": [
{
"title": "Snapshot",
"mimeType": "text/html"
},
{
"title": "Full Text PDF",
"mimeType": "application/pdf"
}
],
"tags": [
{
"tag": "新型电力系统"
},
{
"tag": "碳减排与碳优化"
},
{
"tag": "碳市场与碳交易"
},
{
"tag": "碳规划与碳轨迹"
},
{
"tag": "碳计量和碳追踪"
}
],
"notes": [],
"seeAlso": []
}
]
}
]
/** END TEST CASES **/