-
Notifications
You must be signed in to change notification settings - Fork 280
/
jasminum.js
executable file
·673 lines (635 loc) · 27.1 KB
/
jasminum.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
668
669
670
671
672
673
Zotero.Jasminum = new function () {
// Default values
this.userAgent =
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36";
this.CNDB = ['CNKI', 'WeiPu', 'Wanfang Data', 'SuperLib', '中国知网CNKI', '维普', '万方', '全国图书馆联盟'];
this.CookieSandbox = null;
this.RefCookieSandbox = null;
/**
* Initiate addon
*/
this.init = async function () {
// Register the callback in Zotero as an item observer
var notifierID = Zotero.Notifier.registerObserver(
this.notifierCallback,
["item"]
);
// Unregister callback when the window closes (important to avoid a memory leak)
window.addEventListener(
"unload",
function (e) {
Zotero.Notifier.unregisterObserver(notifierID);
},
false
);
// 等待数据维护更新完毕
// await Zotero.Schema.schemaUpdatePromise;
this.initPref();
Components.utils.import("resource://gre/modules/osfile.jsm");
Zotero.debug("Init Jasminum ...");
};
/**
* Initiate Jasminum preferences
*/
this.initPref = function () {
if (Zotero.Prefs.get("jasminum.pdftkpath") === undefined) {
var pdftkpath = "C:\\Program Files (x86)\\PDFtk Server\\bin";
if (Zotero.isLinux) {
pdftkpath = "/usr/bin";
} else if (Zotero.isMac) {
pdftkpath = "/opt/pdflabs/pdftk/bin";
}
Zotero.Prefs.set("jasminum.pdftkpath", pdftkpath);
}
const initPrefs = {
"jasminum.autoupdate": false,
"jasminum.namepatent": "{%t}_{%g}",
"jasminum.zhnamesplit": false,
"jasminum.rename": true,
"jasminum.autobookmark": true,
"jasminum.autolanguage": false,
"jasminum.autolanguagefunc": 'Auto',
"jasminum.language": 'zh_CN',
"jasminum.languagelist": 'zh,en',
"jasminum.ennamesplit": true,
"jasminum.attachment": 'pdf',
"jasminum.citefield": 'extra',
"jasminum.dateformatter": 'ISO',
"jasminum.dateformatterfill": false,
"jasminum.translatorurl": "https://oss.wwang.de/translators_CN"
};
for (let pref in initPrefs) {
if (Zotero.Prefs.get(pref) === undefined) Zotero.Prefs.set(pref, initPrefs[pref]);
}
};
this.notifierCallback = {
// Check new added item, and adds meta data.
notify: async function (event, type, ids, extraData) {
// var automatic_pdf_download_bool = Zotero.Prefs.get('zoteroscihub.automatic_pdf_download');
if (event == "add") {
// Auto update meta data
var addedItems = Zotero.Items.get(ids);
if (Zotero.Prefs.get("jasminum.autoupdate")) {
Zotero.debug("** Jasminum new items added.");
var items = [];
for (let item of addedItems) {
if (Zotero.Jasminum.UI.isCNKIFile(item)) {
items.push(item);
}
}
Zotero.debug(`** Jasminum add ${items.length} items`);
Zotero.Jasminum.searchItems(items);
}
// Split or merge name
if (!Zotero.Prefs.get("jasminum.zhnamesplit")) {
Zotero.debug("** Jasminum merge CN name");
var items = [];
for (let item of addedItems) {
if (
Zotero.Jasminum.CNDB.includes(
item.getField("libraryCatalog")
)
) {
items.push(item);
}
}
Zotero.Jasminum.mergeName(items);
}
// Add bookmark after new PDF is attached.
if (Zotero.Prefs.get("jasminum.autobookmark")) {
for (let item of addedItems) {
if (
item.parentID &&
Zotero.ItemTypes.getName(
item.parentItem.itemTypeID
) == "thesis" &&
item.parentItem.getField("libraryCatalog") ==
"CNKI" &&
item.attachmentContentType == "application/pdf"
) {
Zotero.debug("***** New PDF item is added");
await Zotero.Jasminum.addBookmarkItem(item);
}
}
}
// Set default language field
if (Zotero.Prefs.get("jasminum.autolanguage")) {
for (let item of addedItems) {
switch (Zotero.Prefs.get("jasminum.autolanguagefunc")) {
case "Auto":
await Zotero.Jasminum.bacthSetLanguage(item);
break;
case "Manual":
await Zotero.Jasminum.setLanguage(item);
break;
}
}
}
}
},
};
/**
* For selected CNKI attachments. Retrive keywords from file name.
* And Search CNKI meta-data by these keywords
* @return {void}
*/
this.searchSelectedItems = function () {
Zotero.debug("**Jasminum Updating Selected items");
this.searchItems(ZoteroPane.getSelectedItems());
};
this.searchItems = async function (items) {
if (items.length == 0) return;
var item = items.shift();
var itemCollections = item.getCollections();
Zotero.debug(itemCollections);
var libraryID = item.libraryID;
// Retrive meta data for webpage item
if (Zotero.ItemTypes.getName(item.itemTypeID) === "webpage") {
Zotero.debug("** Jasminum add webpage.");
let articleId = this.Scrape.getIDFromURL(item.getField("url"));
Zotero.debug([articleId]);
let data = await this.Scrape.getRefText([articleId]);
// Zotero.debug("** Jasminum webpage data");
// Some item will be updated after published
if (data.length === 0 && articleId.dbname.includes("TEMP")) {
articleId = await this.Scrape.getIDFromPage(item.getField("url"));
Zotero.debug([articleId]);
}
data = await this.Scrape.getRefText([articleId]);
var newItems = await this.Utils.trans2Items(data, libraryID);
let targetData = {
targetUrls: [item.getField("url")],
citations: [null]
};
newItems = await this.Utils.fixItem(newItems, targetData);
// Keep the same collection in newItem.
if (itemCollections.length) {
for (let collectionID of itemCollections) {
for (let i of newItems) {
i.addToCollection(collectionID);
await i.saveTx();
};
}
}
// Move notes and attachments to newItems
let childIDs = item.getNotes().concat(item.getAttachments());
if (childIDs.length > 0) {
for (let childID of childIDs) {
var childItem = Zotero.Items.get(childID);
childItem.parentID = newItems[0].id;
await childItem.saveTx();
}
}
// Move item to Trash
item.deleted = true;
await item.saveTx();
} else {
var fileData = this.Scrape.splitFilename(item.attachmentFilename);
Zotero.debug(fileData);
var targetRows = await this.Scrape.search(fileData);
// 有查询结果返回
if (targetRows && targetRows.length > 0) {
var [data, targetData] = await this.Scrape.getRefworks(
targetRows
);
var newItems = await this.Utils.trans2Items(data, libraryID);
Zotero.debug(newItems);
newItems = await this.Utils.fixItem(newItems, targetData);
Zotero.debug("** Jasminum DB trans ...");
if (itemCollections.length) {
for (let collectionID of itemCollections) {
newItems.forEach(function (item) {
item.addToCollection(collectionID);
});
}
}
// 只有单个返回结果
if (newItems.length == 1) {
var newItem = newItems[0];
// Put old item as a child of the new item
item.parentID = newItem.id;
// Use Zotfile to rename file
if (
Zotero.Prefs.get("jasminum.rename") &&
typeof Zotero.ZotFile != "undefined"
) {
Zotero.ZotFile.renameSelectedAttachments();
}
await item.saveTx();
await newItem.saveTx();
// Add bookmark after PDF attaching to new item
if (
Zotero.Prefs.get("jasminum.autobookmark") &&
this.UI.isCNKIPDF(item)
) {
await this.addBookmarkItem(item);
}
} else {
// 有多个返回结果,将文件与新条目关联,用于用户后续手动选择
newItems.forEach(function (newItem) {
item.addRelatedItem(newItem);
});
await item.saveTx();
}
Zotero.debug("** Jasminum finished.");
} else {
// 没有查询结果
this.Utils.showPopup(
"No results found!",
`作者:${fileData.author},\n 篇名:${fileData.keyword},\n 未查询到结果`,
1)
}
}
if (items.length) {
this.searchItems(items);
}
};
this.addBookmarkItem = async function (item) {
if (item === undefined) {
item = ZoteroPane.getSelectedItems()[0];
}
if (!(await this.Scrape.checkPath())) {
this.Utils.showPopup(
"PDFtk Server is not installed",
"未找到 PDFtk Server 的可执行文件。参考插件设置首选项中的下载地址下载并安装,在首选项中设置对应的可执行文件路径(路径以bin结尾)",
1
);
return;
}
// Show alert when file is missing
var attachmentExists = await OS.File.exists(item.getFilePath());
if (!attachmentExists) {
this.Utils.showPopup(
"Attachment is missing",
"该条目下未找到对应的 PDF 文件",
1
)
return;
}
var bookmark, note;
[bookmark, note] = await this.Scrape.getBookmark(item);
if (!bookmark) {
this.Utils.showPopup(
"No Bookmarks found!",
"未找到书签信息,请打开该条目知网链接,确认网页左侧是否出现书签章节信息",
1
)
return;
} else {
// Add TOC note
var noteHTML = item.getNote();
noteHTML += note;
item.setNote(noteHTML);
await item.saveTx();
await this.Scrape.addBookmark(item, bookmark);
}
};
this.splitNameM = function (type) {
var items = this.getItems(type, true);
this.splitName(items);
};
this.mergeNameM = function (type) {
var items = this.getItems(type, true);
this.mergeName(items);
};
this.splitName = async function (items) {
var isSplitEnName = Zotero.Prefs.get("jasminum.ennamesplit")
for (let item of items) {
var creators = item.getCreators();
for (var i = 0; i < creators.length; i++) {
var creator = creators[i];
creator.fieldMode = 0;
if ( // English Name
(creator.lastName.search(/[A-Za-z]/) >= 0 ||
creator.firstName.search(/[A-Za-z]/) >= 0) &&
creator.firstName === "" // 名为空
) {
// 如果不拆分/合并英文名,则跳过
if (!isSplitEnName) continue
var EnglishName = creator.lastName;
var temp = EnglishName.split(/[\n\s+,]/g)
.filter(Boolean); // 过滤空字段
creator.lastName = temp.pop();
creator.firstName = temp.join(" ");
} else if (creator.firstName === "") { // For Chinese Name,名为空
var chineseName = creator.lastName || creator.firstName;
creator.lastName = chineseName.charAt(0);
creator.firstName = chineseName.substr(1);
}
creator.fieldMode = 0;// 0: two-field, 1: one-field (with empty first name)
creators[i] = creator;
}
if (creators != item.getCreators()) {
item.setCreators(creators);
item.saveTx();
}
}
};
this.mergeName = async function (items) {
var isSplitEnName = Zotero.Prefs.get("jasminum.ennamesplit")
for (let item of items) {
var creators = item.getCreators();
for (var i = 0; i < creators.length; i++) {
var creator = creators[i];
creator.fieldMode = 1;
if ( // English Name
creator.lastName.search(/[A-Za-z]/) !== -1 ||
creator.lastName.search(/[A-Za-z]/) !== -1
) {
// 如果不拆分/合并英文名,则跳过
if (!isSplitEnName) continue
creator.lastName = creator.firstName + " " + creator.lastName;
} else { // For Chinese Name
creator.lastName = creator.lastName + creator.firstName;
}
creator.firstName = "";
creator.fieldMode = 1;// 0: two-field, 1: one-field (with empty first name)
creators[i] = creator;
}
if (creators != item.getCreators()) {
item.setCreators(creators);
item.saveTx();
}
}
};
this.splitSemicolonNamesN = async function (type) {
var items = this.getItems(type, true)
this.splitSemicolonNames(items);
}
/**
* 在知网搜索结果列表添加文献时,可能导致该文献的作者名变成类似于 姓名;姓名;姓名 的形式,
* 使用此函数将分号分隔的姓名分隔到不同的条目中。
*/
this.splitSemicolonNames = async function (items) {
for (let item of items) {
const creators = item.getCreators();
var newlist = [];
for (let creator of creators) {
if (
creator.lastName.search(";") &&
creator.firstName === ""
) {
const names = creator.lastName.split(";").filter(s => s !== "");
for (let name of names) {
newlist.push(
{
"firstName": "",
"lastName": name,
"creatorType": "author"
}
);
}
} else {
newlist.push(creator);
}
}
if (newlist !== creators) {
item.setCreators(newlist);
item.saveTx();
}
}
}
this.removeDotM = function (type) {
var items = this.getItems(type, true);
this.removeDot(items);
};
this.removeDot = async function (items) {
for (let item of items) {
var attachmentIDs = item.getAttachments();
for (let id of attachmentIDs) {
var atta = Zotero.Items.get(id);
var newName = atta.attachmentFilename.replace(
/([_\u4e00-\u9fa5]), ([_\u4e00-\u9fa5])/g,
"$1$2"
);
await atta.renameAttachmentFile(newName);
atta.setField("title", newName);
atta.saveTx();
}
}
};
/**
* Update citation in Zotero item field
* 110 citations(CNKI)[2021-08-22]<北大核心, CSCI>
* @param {[Zotero.item]}
* @return {void}
*/
this.updateCiteCSSCI = async function (items) {
var html;
for (let item of items) {
if (["patent", "webpage"].includes(Zotero.ItemTypes.getName(item.itemTypeID))) {
this.Utils.showPopup(
"条目类型不支持",
`${Zotero.ItemTypes.getName(item.itemTypeID)}类型条目不需要抓取`,
1
)
continue;
} else if (item.getField("title").search(/[_\u4e00-\u9fa5]/) === -1) {
this.Utils.showPopup(
"条目类型不支持",
`非中文条目`,
1
)
continue;
} else if (item.getField("url")) {
let url = item.getField("url");
let resp = await Zotero.HTTP.request("GET", url);
html = this.Utils.string2HTML(resp.responseText);
// 检测是否出现知网验证页面,一般网页以nxgp开头的页面,会出现知网验证页面
if (html.querySelector("div.verify_wrap")) {
this.Utils.showPopup(
"期刊、引用抓取异常",
"抓取信息时出现知网验证页面",
1);
continue;
}
// 特异性网址,
if (Zotero.Utilities.xpath(html, "//h2[@id='erro_span']")) {
Zotero.debug("** Jasminum 条目网址有点特殊");
let fileData = {
keyword: item.getField("title"),
author: item.getCreators()[0].lastName + item.getCreators()[0].firstName
};
let targetRows = await this.Scrape.search(fileData);
if (targetRows && targetRows.length > 0) {
let urls = await this.Scrape.getRefworks(
targetRows, onlyUrl = true
);
Zotero.debug("** Jasminum " + urls[0]);
item.setField('url', urls[0]);
item.saveTx();
url = item.getField("url");
resp = await Zotero.HTTP.request("GET", url);
html = this.Utils.string2HTML(resp.responseText);
// 检测是否出现知网验证页面,一般网页以nxgp开头的页面,会出现知网验证页面
if (html.querySelector("div.verify_wrap")) {
this.Utils.showPopup(
"期刊、引用抓取异常",
"抓取信息时出现知网验证页面",
1);
continue;
}
}
}
let dateString = new Date().toLocaleDateString().replace(/\//g, '-');
let cite = this.Scrape.getCitationFromPage(html);
// let citeString = `CNKI citations: ${cite}[${dateString}]`;
let citeString = `${cite}[${dateString}]`;
let cssci = this.Scrape.getCSSCI(html);
// let cssciString = "Chinese Core Journals: <" + cssci + ">";
let cssciString = "<" + cssci + ">";
let field = Zotero.Prefs.get("jasminum.citefield");
var extraData = item.getField(field);
// Remove old cite and CSSCI string
extraData = extraData.replace(/\d+ citations?\(CNKI\)\[[\d-]{8,10}\].*\s?/, '');
extraData = extraData.replace(/^<.*?>\s?/, "");
extraData = extraData.replace(/Chinese Core Journals: <.*?>/, "")
extraData = extraData.replace(/CNKI citations:\s?\d+\[[\d-]{8,10}\]/, '');
let extraAdd = "";
if (cite != null && cite > 0) {
// if (extraData.match(/CNKI citations:\s?/)) {
// extraData = extraData.replace(/CNKI citations:\s?\d+\[[\d-]{10}\]/,
// citeString);
// } else {
// extraData = extraData.trim() + '\n' + citeString;
// } // 暂时注释,等后期使用新的展示方式
if (extraData.match(/👍/)) { // 先用这简单的展示,便于展示排序
extraData = extraData.replace(/👍\s?\d+\[[\d-]{8,10}\]/, "");
}
extraAdd = "👍" + citeString;
}
if (cssci) { // 或者可以参考其他核心期刊数据来源
// if (extraData.match(/Chinese Core Journals: /)) {
// extraData = extraData.replace(/Chinese Core Journals: <.*?>/, cssciString);
// } else {
// extraData = extraData.trim() + '\n' + cssciString;
// }
if (extraData.match(/📗/)) {
extraData = extraData.replace(/📗<.*?>/, "");
}
extraAdd += '📗' + cssciString;
}
this.Utils.showPopup(
"期刊、引用抓取完毕",
`${item.getField('title')}, ${cite}, ${cssci ? cssci : '非核心期刊'}`,
0
)
Zotero.debug("** Jasminum cite number: " + cite);
Zotero.debug("** Jasminum cssci: " + cssci);
item.setField(field, extraAdd + "\n" + extraData.trim());
await item.saveTx();
} else {
this.Utils.showPopup(
"条目抓取失败",
"缺失条目 URL 信息",
1
);
}
}
};
this.updateCiteCSSCIItems = function () {
var items = ZoteroPane.getSelectedItems();
this.updateCiteCSSCI(items);
};
/**
* Set default language value in item field
* @param {[Zotero.item]}
* @return {void}
*/
this.setLanguage = async function (item) {
let defaultLanguage = Zotero.Prefs.get("jasminum.language");
if (item.getField("language") != defaultLanguage) {
item.setField("language", defaultLanguage);
await item.saveTx();
}
};
this.setLanguageItems = async function (type) {
var items = this.getItems(type, true);
for (let item of items) { await this.setLanguage(item) }
};
/**
* Batch Set language using nlp.js
* @param {[Zotero.item]}
* @return {void}
*/
this.bacthSetLanguage = async function (item) {
// 获取常用语言列表
let languageStr = Zotero.Prefs.get("jasminum.languagelist").replace(/\s*/g, "");
let languageList = languageStr.split(/,|,/g);
// 使用 nlp.js 进行识别
let langGuess = this.NLP.guess(item.getField("title"), languageList)[0]["alpha2"];
if (langGuess && item.getField("language") != langGuess) {
item.setField("language", langGuess);
await item.saveTx();
}
};
this.bacthSetLanguageItems = async function (type) {
let items = this.getItems(type, true);
for (let item of items) { await this.bacthSetLanguage(item) }
};
/**
* Uniform date format
* Inspired by https://forums.zotero.org/discussion/84444/date-formats
* date format https://www.w3schools.com/js/js_date_formats.asp
* @param {[Zotero.item]}
* @return {void}
*/
this.dateFormatter = async function (type) {
let items = this.getItems(type, true)
let dateFormat = Zotero.Prefs.get("jasminum.dateformatter")
let isFill = Zotero.Prefs.get("jasminum.dateformatterfill")
let separator = (dateFormat == "ISO") ? "-" : "/"
for (let item of items) {
let oldDate = item.getField('date')
let dateJSON = Zotero.Date.strToDate(oldDate);
let newDate = ""
if (dateFormat == "yearOnly") {
newDate = dateJSON.year
} else {
// month 以 0 开始
let newMonth = dateJSON.month + 1
let newDay = dateJSON.day
if (isFill) {
// 当 month,day 小于 10 时,在前补 0
newMonth = ('0' + newMonth).slice(-2)
newDay = ('0' + dateJSON.day).slice(-2)
}
let dateList = [dateJSON.year, newMonth, newDay]
if (dateFormat == "short") dateList.reverse()
// 去除日期数组里 undefined, NaN
newDate = dateList.filter(x => Number(x)).join(separator)
}
if (newDate && newDate != oldDate) {
item.setField("date", newDate)
await item.saveTx();
}
}
}
/**
* get items from different type
* @param {string}
* @return {[Zotero.item]}
*/
this.getItems = function (type = "items", regular = false) {
let items = []
if (type === "items") {
items = ZoteroPane.getSelectedItems()
} else if (type === "collection") {
let collection = ZoteroPane.getSelectedCollection();
if (collection) items = collection.getChildItems();
}
// 只保留元数据条目
// 用于解决多选项目时选中附件类条目导致小组件修改错误,使得批量修改中断。
if (regular) items = items.filter(item => item.isRegularItem())
return items
}
/**
* Download pdf/caj attachments from CNKI for selected items
* @param {[Zotero.item]}
* @return {void}
*/
this.getAttachments = async function () {
var items = ZoteroPane.getSelectedItems();
for (var item of items) { await this.Scrape.importAttachment(item) }
};
}