forked from l0o0/jasminum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
614 lines (579 loc) · 23.1 KB
/
test.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
Zotero.Jasminum = {
init: function () {
// // Register the callback in Zotero as an item observer
// var notifierID = Zotero.Notifier.registerObserver(
// Zotero.Jasminum.notifierCallback,
// ["item"]
// );
// // Unregister callback when the window closes (important to avoid a memory leak)
// window.addEventListener(
// "unload",
// function (e) {
// Zotero.Notifier.unregisterObserver(notifierID);
// },
// false
// );
Components.utils.import("resource://gre/modules/osfile.jsm");
Zotero.debug("Init Jasminum ...");
},
notifierCallback: {
// Check new added item, and adds meta data.
// TODO Add a check function here
notify: function (event, type, ids, extraData) {
// var automatic_pdf_download_bool = Zotero.Prefs.get('zoteroscihub.automatic_pdf_download');
if (event == "add") {
suppress_warnings = true;
Zotero.Jasminum.updateItems(
Zotero.Items.get(ids),
suppress_warnings
);
}
},
},
displayMenuitem: function () {
var pane = Services.wm.getMostRecentWindow("navigator:browser")
.ZoteroPane;
var items = pane.getSelectedItems();
Zotero.debug("**Jasminum selected item length: " + items.length);
var showMenu = items.some((item) => Zotero.Jasminum.checkItem(item));
pane.document.getElementById(
"zotero-itemmenu-jasminum"
).hidden = !showMenu;
var showMenuPDF = false;
if (items.length === 1) {
showMenuPDF = Zotero.Jasminum.checkItemPDF(items[0]);
pane.document.getElementById(
"zotero-itemmenu-jasminum-bookmark"
).hidden = !showMenuPDF;
}
pane.document.getElementById("id-jasminum-separator").hidden = !(
showMenu || showMenuPDF
);
Zotero.debug("**Jasminum show menu: " + (showMenu || showMenuPDF));
},
updateSelectedEntity: function (libraryId) {
Zotero.debug("**Jasminum Updating items in entity");
if (!ZoteroPane.canEdit()) {
ZoteroPane.displayCannotEditLibraryMessage();
return;
}
var collection = ZoteroPane.getSelectedCollection(false);
if (collection) {
Zotero.debug(
"**Jasminum Updating items in entity: Is a collection == true"
);
var items = [];
collection.getChildItems(false, false).forEach(function (item) {
items.push(item);
});
suppress_warnings = true;
Zotero.Jasminum.updateItems(items, suppress_warnings);
}
},
updateSelectedItems: function () {
Zotero.debug("**Jasminum Updating Selected items");
Zotero.Jasminum.updateItems(ZoteroPane.getSelectedItems());
},
checkItem: function (item) {
// Return true, when item is OK for update cnki data.
if (
!item.isAttachment() ||
item.isRegularItem() ||
!item.isTopLevelItem()
) {
return false;
}
var filename = item.getFilename();
// Find Chinese characters in string
if (escape(filename).indexOf("%u") < 0) return false;
// Extension should be CAJ or PDF
var ext = filename.substr(filename.length - 3, 3);
if (ext != "pdf" && ext != "caj") return false;
return true;
},
splitFilename: function (filename) {
// Make query parameters from filename
var prefix = filename.substr(0, filename.length - 4);
var prefixArr = prefix.split("_");
return {
author: prefixArr[prefixArr.length - 1],
keyword: prefixArr.slice(0, prefixArr.length - 1).join(" "),
};
},
createPost: function (fileData) {
// Create a search string.
static_post_data = {
action: "",
NaviCode: "*",
ua: "1.21",
isinEn: "1",
PageName: "ASP.brief_result_aspx",
DbPrefix: "SCDB",
DbCatalog: "中国学术期刊网络出版总库",
ConfigFile: "SCDB.xml",
db_opt: "CJFQ,CDFD,CMFD,CPFD,IPFD,CCND,CCJD",
year_type: "echar",
CKB_extension: "ZYW",
txt_1_sel: "SU$%=|",
txt_1_value1: fileData.keyword,
txt_1_relation: "#CNKI_AND",
txt_1_special1: "=",
au_1_sel: "AU",
au_1_sel2: "AF",
au_1_value1: fileData.author,
au_1_special1: "=",
au_1_special2: "%",
his: "0",
__: Date() + " (中国标准时间)",
};
var urlEncodedDataPairs = [];
for (name in static_post_data) {
urlEncodedDataPairs.push(
encodeURIComponent(name) +
"=" +
encodeURIComponent(static_post_data[name])
);
}
return urlEncodedDataPairs.join("&").replace(/%20/g, "+");
},
selectRow: function (rowSelectors) {
Zotero.debug("**Jasminum select window start");
var io = { dataIn: rowSelectors, dataOut: null };
var newDialog = window.openDialog(
"chrome://zotero/content/ingester/selectitems.xul",
"_blank",
"chrome,modal,centerscreen,resizable=yes",
io
);
return io.dataOut;
},
getIDFromUrl: function (url) {
if (!url) return false;
// add regex for navi.cnki.net
var dbname = url.match(/[?&](?:db|table)[nN]ame=([^&#]*)/i);
var filename = url.match(/[?&]filename=([^&#]*)/i);
var dbcode = url.match(/[?&]dbcode=([^&#]*)/i);
if (
!dbname ||
!dbname[1] ||
!filename ||
!filename[1] ||
!dbcode ||
!dbcode[1] ||
dbname[1].match("TEMP$")
)
return false;
return { dbname: dbname[1], filename: filename[1], dbcode: dbcode[1] };
},
parseRef: function (targetID) {
var postData =
"formfilenames=" +
encodeURIComponent(
targetID.dbname + "!" + targetID.filename + "!1!0,"
) +
"&hid_kLogin_headerUrl=/KLogin/Request/GetKHeader.ashx%3Fcallback%3D%3F" +
"&hid_KLogin_FooterUrl=/KLogin/Request/GetKHeader.ashx%3Fcallback%3D%3F" +
"&CookieName=FileNameS";
},
promiseGet: function (url) {
Zotero.debug("** Jasminum create http get.");
return new Promise(function (resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.open("GET", url);
xhr.onload = function () {
if (this.status === 200) {
resolve(xhr.response);
} else {
reject({
status: this.status,
statusText: xhr.statusText,
});
}
};
xhr.onerror = function () {
reject({
status: this.status,
statusText: xhr.statusText,
});
};
xhr.send();
});
},
searchPrepare: async function (fileData) {
var searchData = Zotero.Jasminum.createPost(fileData);
var SEARCH_HANDLE_URL =
"https://kns.cnki.net/kns/request/SearchHandler.ashx";
var url = SEARCH_HANDLE_URL + "?" + searchData;
Zotero.debug("**Jasminum start prepare");
var searchPrepareOut = await Zotero.Jasminum.promiseGet(url);
return searchPrepareOut;
},
search: async function (searchPrepareOut, fileData) {
Zotero.debug("**Jasminum start search");
var keyword = encodeURI(fileData.keyword);
Zotero.debug("**Jasminum keyword: " + keyword);
var resultUrl =
"https://kns.cnki.net/kns/brief/brief.aspx?pagename=" +
searchPrepareOut +
`&t=${Date.parse(new Date())}&keyValue=${keyword}&S=1&sorttype=`;
Zotero.debug(resultUrl);
var searchResult = await Zotero.Jasminum.promiseGet(resultUrl);
var targetRow = Zotero.Jasminum.getSearchItems(searchResult);
return targetRow;
},
getSearchItems: function (resptext) {
Zotero.debug("**Jasminum get item from search");
var parser = new DOMParser();
var html = parser.parseFromString(resptext, "text/html");
var rows = html.querySelectorAll("table.GridTableContent > tbody > tr");
Zotero.debug("**Jasminum 搜索结果:" + (rows.length - 1));
var targetRow;
if (rows.length <= 1) {
Zotero.debug("**Jasminum No items found.");
return null;
} else if (rows.length == 2) {
targetRow = rows[1];
} else {
// Get the right item from search result.
var rowIndicators = {};
for (let idx = 1; idx < rows.length; idx++) {
var rowText = rows[idx].textContent.split(/\s+/).join(" ");
rowIndicators[idx] = rowText;
Zotero.debug(rowText);
}
var targetIndicator = Zotero.Jasminum.selectRow(rowIndicators);
// Zotero.debug(targetIndicator);
// No item selected, return null
if (!targetIndicator) return null;
targetRow = rows[Object.keys(targetIndicator)[0]];
}
// Zotero.debug(targetRow.textContent);
return targetRow;
},
getRefworks: async function (targetRow) {
Zotero.debug("**Jasminum start get ref");
if (targetRow == null) {
return new Error("No items returned from the CNKI");
}
var targetUrl = targetRow.getElementsByClassName("fz14")[0].href;
var targetID = Zotero.Jasminum.getIDFromUrl(targetUrl);
Zotero.debug(targetID);
// Get reference data from CNKI by ID.
var postData =
"formfilenames=" +
encodeURIComponent(
targetID.dbname + "!" + targetID.filename + "!1!0,"
) +
"&hid_kLogin_headerUrl=/KLogin/Request/GetKHeader.ashx%3Fcallback%3D%3F" +
"&hid_KLogin_FooterUrl=/KLogin/Request/GetKHeader.ashx%3Fcallback%3D%3F" +
"&CookieName=FileNameS";
var url =
"https://kns.cnki.net/kns/ViewPage/viewsave.aspx?displayMode=Refworks&" +
postData;
var resp = await Zotero.Jasminum.promiseGet(url);
// Zotero.debug(resp);
var parser = new DOMParser();
var html = parser.parseFromString(resp, "text/html");
var data = Zotero.Utilities.xpath(
html,
"//table[@class='mainTable']//td"
)[0]
.innerHTML.replace(/<br>/g, "\n")
.replace(
/^RT\s+Conference Proceeding/gim,
"RT Conference Proceedings"
)
.replace(/^RT\s+Dissertation\/Thesis/gim, "RT Dissertation")
.replace(/^(A[1-4]|U2)\s*([^\r\n]+)/gm, function (m, tag, authors) {
authors = authors.split(/\s*[;,,]\s*/); // that's a special comma
if (!authors[authors.length - 1].trim()) authors.pop();
return tag + " " + authors.join("\n" + tag + " ");
});
Zotero.debug(data);
return data;
},
promiseTranslate: async function (translate, libraryID) {
Zotero.debug("** Jasminum translate begin ...");
translate.setHandler("select", function (translate, items, callback) {
for (let i in items) {
let obj = {};
obj[i] = items[i];
callback(obj);
return;
}
});
let newItems = await translate.translate({
libraryID: libraryID,
saveAttachments: false,
});
if (newItems.length) {
Zotero.debug(newItems[0]);
Zotero.debug("** Jasminum translate end.");
return newItems[0];
}
throw new Error("No items found");
},
fixItem: function (newItem) {
var creators = newItem.getCreators();
for (var i = 0; i < creators.length; i++) {
var creator = creators[i];
if (creator.firstName) continue;
var lastSpace = creator.lastName.lastIndexOf(" ");
if (
creator.lastName.search(/[A-Za-z]/) !== -1 &&
lastSpace !== -1
) {
// western name. split on last space
creator.firstName = creator.lastName.substr(0, lastSpace);
creator.lastName = creator.lastName.substr(lastSpace + 1);
} else {
// Chinese name. first character is last name, the rest are first name
creator.firstName = creator.lastName.substr(1);
creator.lastName = creator.lastName.charAt(0);
}
creators[i] = creator;
}
newItem.setCreators(creators);
// Clean up abstract
if (newItem.getField("abstractNote")) {
newItem.setField(
"abstractNote",
newItem
.getField("abstractNote")
.replace(/\s*[\r\n]\s*/g, "\n")
.replace(/<.*?>/g, "")
);
}
// Remove wront CN field.
newItem.setField("callNumber", "");
newItem.setField("libraryCatalog", "CNKI");
if (newItem.getNotes()) {
Zotero.Items.erase(newItem.getNotes());
}
return newItem;
},
updateItems: async function (items) {
var zp = Zotero.getActiveZoteroPane();
if (items.length == 0) return;
var selectParent = true ? items.length === 1 : false;
var item = items.shift();
var itemCollections = item.getCollections();
var libraryID = item.libraryID;
if (!Zotero.Jasminum.checkItem(item)) return;
var fileData = Zotero.Jasminum.splitFilename(item.getFilename());
var searchPrepareOut = await Zotero.Jasminum.searchPrepare(fileData);
Zotero.debug("searchPrepareOut");
Zotero.debug(searchPrepareOut);
var targetRow = await Zotero.Jasminum.search(
searchPrepareOut,
fileData
);
Zotero.debug("targetRow");
Zotero.debug(targetRow.textContent);
var data = await Zotero.Jasminum.getRefworks(targetRow);
var translate = new Zotero.Translate.Import();
translate.setTranslator("1a3506da-a303-4b0a-a1cd-f216e6138d86");
translate.setString(data);
var newItem = await Zotero.Jasminum.promiseTranslate(
translate,
libraryID
);
Zotero.debug(newItem);
newItem = Zotero.Jasminum.fixItem(newItem);
Zotero.debug("** Jasminum DB trans ...");
if (itemCollections.length) {
for (let collectionID of itemCollections) {
newItem.addToCollection(collectionID);
}
}
// Put old item as a child of the new item
item.parentID = newItem.id;
item.saveTx();
newItem.saveTx();
if (items.length) {
Zotero.Jasminum.updateItems(items);
}
Zotero.debug("** Jasminum finished.");
},
checkItemPDF: function (item) {
return (
item.isAttachment() &&
item.attachmentContentType &&
item.attachmentContentType === "application/pdf" &&
escape(item.getFilename()).includes("%u")
); // Contain Chinese
},
getChapterUrl: async function (itemUrl) {
Zotero.debug("** Jasminum get chapter url.");
var respText = await Zotero.Jasminum.promiseGet(itemUrl);
var parser = new DOMParser();
var respHTML = parser.parseFromString(respText, "text/html");
var chapterDown = Zotero.Utilities.xpath(
respHTML,
"//a[contains(text(), '分章下载')]"
);
if (chapterDown.length === 0) {
Zotero.debug("No chapter found.");
return null;
}
var readerUrl = Zotero.Utilities.xpath(
respHTML,
"//a[contains(text(), '在线阅读')]"
)[0].href;
Zotero.debug("** Jasminum reader url: " + readerUrl);
var respText = await Zotero.Jasminum.promiseGet(readerUrl);
var parser = new DOMParser();
var respHTML = parser.parseFromString(respText, "text/html");
var chapterUrl = Zotero.Utilities.xpath(
respHTML,
"//iframe[@id='treeView']"
)[0].getAttribute("src");
Zotero.debug("** Jasminum chapter url: " + chapterUrl);
return "https://kreader.cnki.net/Kreader/" + chapterUrl;
},
getBookmark: async function (item) {
// demo url https://kreader.cnki.net/Kreader/buildTree.aspx?dbCode=cdmd&FileName=1020622678.nh&TableName=CMFDTEMP&sourceCode=GHSFU&date=&year=2020&period=&fileNameList=&compose=&subscribe=&titleName=&columnCode=&previousType=_&uid=
var parentItem = item.parentItem;
var parentItemType = parentItem.itemTypeID; // theis = 7
var itemUrl = "";
var itemChapterUrl = "";
if (
parentItemType === 7 &&
parentItem.getField("extra") &&
parentItem.getField("extra").includes("cnki")
) {
Zotero.debug("1");
itemChapterUrl = parentItem.getField("extra");
} else if (
parentItemType === 7 &&
parentItem.getField("url") &&
parentItem.getField("url").includes("cnki")
) {
Zotero.debug("2");
itemUrl = parentItem.getField("url");
Zotero.debug("** Jasminum item url: " + itemUrl);
itemChapterUrl = await Zotero.Jasminum.getChapterUrl(itemUrl);
} else {
Zotero.debug("3");
var fileData = {
keyword: parentItem.getField("title"),
author:
parentItem.getCreator(0).lastName +
parentItem.getCreator(0).firstName,
};
var searchPrepareOut = await Zotero.Jasminum.searchPrepare(
fileData
);
var targetRow = await Zotero.Jasminum.search(
searchPrepareOut,
fileData
);
itemUrl = targetRow.querySelector("a.fz14").href;
itemChapterUrl = await Zotero.Jasminum.getChapterUrl(itemUrl);
// 获取文献链接URL -> 获取章节目录URL
}
Zotero.debug("** Jasminum item url: " + itemUrl);
Zotero.debug("** Jasminum item chapter url: " + itemChapterUrl);
var chapterText = await Zotero.Jasminum.promiseGet(itemChapterUrl);
var parser = new DOMParser();
var chapterHTML = parser.parseFromString(chapterText, "text/html");
var tree = chapterHTML.getElementById("treeDiv");
var rows = tree.querySelectorAll("tr");
var rows_array = [];
for (let row of rows) {
Zotero.debug(row.textContent.trim());
var cols = row.querySelectorAll("td");
var level = cols.length - 1;
var title = row.textContent.trim();
var onclickText = cols[cols.length - 1]
.querySelector("a")
.getAttribute("onclick");
var pageRex = onclickText.match(/CDMDNodeClick\('(\d+)'/);
var page = pageRex[1];
var bookmark = `BookmarkBegin\nBookmarkTitle: ${title}\nBookmarkLevel: ${level}\nBookmarkPageNumber: ${page}`;
rows_array.push(bookmark);
}
var bookmark = rows_array.join("\n");
return bookmark;
},
addBookmark: async function (item, bookmark) {
Zotero.debug("** Jasminum add bookmark begin");
Zotero.debug(item);
let cacheFile = Zotero.getTempDirectory();
cacheFile.append("bookmark.txt");
let tmpDir = OS.Path.dirname(cacheFile.path);
if (cacheFile.exists()) {
cacheFile.remove(false);
}
let cachePDF = Zotero.getTempDirectory();
cachePDF.append("output.pdf");
if (cachePDF.exists()) {
cachePDF.remove(false);
}
let encoder = new TextEncoder();
let array = encoder.encode(bookmark);
let promise = OS.File.writeAtomic(cacheFile.path, array, {
tmpPath: cacheFile.path + ".tmp",
});
var pdftk = "C:\\Program Files (x86)\\PDFtk Server\\bin\\pdftk.exe";
if (Zotero.isLinux) {
pdftk = "/usr/bin/pdftk";
} else if (Zotero.isMac) {
pdftk = "Path in mac"; // TODO
}
var args = [
item.getFilePath(),
"update_info_utf8",
cacheFile.path,
"output",
cachePDF.path,
];
Zotero.debug(
"PDFtk: Running " +
pdftk +
" " +
args.map((arg) => "'" + arg + "'").join(" ")
);
try {
await Zotero.Utilities.Internal.exec(pdftk, args);
Zotero.debug("PDFtk: Add bookmark:");
await Zotero.Jasminum.updateBookmarkAttachment(item, cachePDF.path);
cacheFile.remove(false);
cachePDF.remove(false);
Zotero.debug("** Jasminum add bookmark complete!");
} catch (e) {
Zotero.logError(e);
try {
cacheFile.remove(false);
cachePDF.remove(false);
} catch (e) {
Zotero.logError(e);
}
throw new Zotero.Exception.Alert("PDFtk add bookmark failed.");
}
},
updateBookmarkAttachment: async function (item, markedpdf) {
var parentItem = item.parentItem;
var parentItemID = parentItem.id;
var libraryID = parentItem.libraryID;
var fileBaseName = item.getFilename().replace(/\.pdf/g, "");
Zotero.debug(parentItemID + fileBaseName + markedpdf + libraryID);
var file = markedpdf;
var newItem = await Zotero.Attachments.importFromFile({
file,
libraryID,
fileBaseName,
parentItemID,
});
await newItem.saveTx();
// delete old attachment
Zotero.Items.erase(item.id);
},
addBookmarkItem: async function (item) {
var bookmark = await Zotero.Jasminum.getBookmark(item);
await Zotero.Jasminum.addBookmark(items[0], bookmark);
},
};
var items = ZoteroPane.getSelectedItems();
await Zotero.Jasminum.updateSelectedItems(items);
await Zotero.Jasminum.addBookmarkItem(items[0]);