Skip to content

Commit

Permalink
fix:
Browse files Browse the repository at this point in the history
修复抓取知网网页条目时,新条目不能放到原文件夹(collection)中;过滤卷期字段前面的0
l0o0 committed Jan 19, 2022
1 parent c73c3b6 commit a176fea
Showing 2 changed files with 32 additions and 6 deletions.
21 changes: 15 additions & 6 deletions chrome/content/scripts/jasminum.js
Original file line number Diff line number Diff line change
@@ -148,6 +148,7 @@ Zotero.Jasminum = new function () {
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") {
@@ -157,21 +158,29 @@ Zotero.Jasminum = new function () {
let postData = this.Scrape.createRefPostData([articleId]);
let data = await this.Scrape.getRefText(postData);
// Zotero.debug("** Jasminum webpage data");
// Zotero.debug(data);

// Some item will be updated after published
if (data.length === 0 && articleId.dbname.includes("TEMP")) {
articleId.dbname = articleId
.dbname
.replace(/TEMP.*$/, 'LAST' + item.getField("dateAdded").slice(0, 4));
postData = this.Scrape.createRefPostData([articleId]);
data = await this.Scrape.getRefText(postData);
articleId = await this.Scrape.getIDFromPage(item.getField("url"));
Zotero.debug([articleId]);
}
postData = this.Scrape.createRefPostData([articleId]);
data = await this.Scrape.getRefText(postData);
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) {
17 changes: 17 additions & 0 deletions chrome/content/scripts/scrape.js
Original file line number Diff line number Diff line change
@@ -279,6 +279,21 @@ Zotero.Jasminum.Scrape = new function () {
return { dbname: dbname[1], filename: filename[1], dbcode: dbcode[1] };
}.bind(Zotero.Jasminum);

/**
* Sometimes CNKI URL contains a temporary dbname,
* you need to find a valid dbname from page.
* @param {String} CNKI url string
* @return {Object} {dbname: ..., filename: ..., dbcode: ...}
*/
this.getIDFromPage = async function (url) {
let pageResp = await Zotero.HTTP.request("GET", url);
let page = this.Utils.string2HTML(pageResp.responseText);
let dbcode = page.querySelector("input#paramdbcode").value;
let filename = page.querySelector("input#paramfilename").value;
let dbname = page.querySelector("input#paramdbname").value;
return { dbname: dbname, filename: filename, dbcode: dbcode };
}.bind(Zotero.Jasminum);


this.search = async function (fileData) {
Zotero.debug("**Jasminum start search");
@@ -406,6 +421,8 @@ Zotero.Jasminum.Scrape = new function () {
.replace("</li><li>", "") // divide results
.replace(/<br>|\r/g, "\n")
.replace(/vo (\d+)\n/, "VO $1\n") // Divide VO and IS to different line
.replace(/IS 0(\d+)\n/g, "IS $1\n") // Remove leading 0
.replace(/VO 0(\d+)\n/g, "VO $1\n")
.replace(/\n+/g, "\n")
.replace(/\n([A-Z][A-Z1-9]\s)/g, "<br>$1")
.replace(/\n/g, "")

0 comments on commit a176fea

Please sign in to comment.