Skip to content

Commit

Permalink
修正无法下载pdf的潜在bug
Browse files Browse the repository at this point in the history
  • Loading branch information
jiaojiaodubai committed Dec 28, 2023
1 parent 2f41eb1 commit 4b6ceac
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 166 deletions.
4 changes: 2 additions & 2 deletions ChinaXiv.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ function fixItem(item, doc, url) {
item.extra += addExtra('publicationTitle', labels.getWith('期刊') || '中国科学院科技论文预发布平台');
item.extra += addExtra('original-title', text(doc, 'div.hd > p'));
item.extra += addExtra('original-abstract', labels.getWith('Abstract'));
let pdfLink = attr(doc, '.side > .bd a[href*="filetype=pdf"]', 'href');
let pdfLink = doc.querySelector('.side > .bd a[href*="filetype=pdf"]');
item.attachments.push({
url: pdfLink,
url: pdfLink.href,
title: 'Full Text PDF',
mimeType: 'application/pdf'
});
Expand Down
322 changes: 161 additions & 161 deletions E-Tiller.js
Original file line number Diff line number Diff line change
@@ -1,162 +1,162 @@
{
"translatorID": "d611008a-850d-4860-b607-54e1ecbcc592",
"label": "E-Tiller",
"creator": "jiaojiaodubai23",
"target": "^https?://.*(/ch/)?.*(.aspx)?",
"minVersion": "5.0",
"maxVersion": "",
"priority": 100,
"inRepository": true,
"translatorType": 4,
"browserSupport": "gcsibv",
"lastUpdated": "2023-12-16 19:56:45"
}

/*
***** BEGIN LICENSE BLOCK *****
Copyright © 2022 jiaojiaodubai23@gmail.com
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 paths = [
'reader/view_abstract.aspx?file_no=',
'/article/abstract/'
];

function detectWeb(doc, url) {
Z.debug('---------- E-Tiller ----------');
let insite = Array.from(doc.querySelectorAll(`div[class*="foot"], div[id*="foot"]`))
.some(foot => (/北京勤云科技/.test(foot.textContent)));
Z.debug(`incite: ${insite}`);
if (!insite) return false;
for (let path of paths) {
if (url.includes(path) && doc.querySelector('meta[name="citation_title"]')) {
Z.debug(`match path: ${path}`);
return 'journalArticle';
}
else if (doc.querySelector('meta[name]') && getSearchResults(doc, true)) {
Z.debug(`match path: ${path}`);
return 'multiple';
}
}
return false;
}

function getSearchResults(doc, checkOnly) {
var items = {};
var found = false;
var rows = Array.from(doc.querySelectorAll(paths.map(path => `a[href*="${path}"]`).join(',')))
.filter(element => !(/^[[【〔]?\s*摘要/.test(ZU.trimInternal(element.textContent))));
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) {
if (detectWeb(doc, url) == 'multiple') {
let items = await Zotero.selectItems(getSearchResults(doc, false));
Z.debug('selected items:');
Z.debug(items);
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) {
let translator = Zotero.loadTranslator('web');
// Embedded Metadata
translator.setTranslator('951c027d-74ac-47d4-a107-9c3069ab7b48');
translator.setDocument(doc);
translator.setHandler('itemDone', (_obj, item) => {
item.extra = '';
if (item.title.includes('(网络首发、推荐阅读)')) {
item.extra += addExtra('available-date', item.date);
delete item.date;
item.extra += addExtra('Status', 'advance online publication');
item.title = item.title.replace(/(网络首发、推荐阅读)$/, '');
}
item.language = {
cn: 'zh-CN',
en: 'en-US'
}[item.language];
item.extra += addExtra('original-title', attr(doc, 'meta[name="citation_title"]', 'content', 1));
let creators = doc.querySelector('meta[name="citation_author"]')
? Array.from(doc.querySelectorAll('meta[name="citation_author"]')).map(element => element.content)
: attr(doc, 'meta[name="citation_authors"]', 'content', 0).split(/[,;,;]/);
if (creators.length) {
Z.debug(creators);
item.creators = creators
.map(creator => creator.replace(/(<.+>)?[\d,\s]+(<.+>)?$/, ''))
.filter(creator => creator)
.map(creator => ZU.cleanAuthor(creator, 'author'));
}
item.creators.forEach((creator) => {
if (/[\u4e00-\u9fa5]/.test(creator.lastName)) {
creator.lastName = creator.firstName + creator.lastName;
creator.firstName = '';
creator.fieldMode = 1;
}
});
let enCreators = attr(doc, 'meta[name="citation_authors"]', 'content', 1).split(/[,;,;]/);
let creatorsExt = JSON.parse(JSON.stringify(item.creators));
for (let i = 0; i < item.creators.length; i++) {
creatorsExt[i].original = enCreators[i];
}
if (item.tags.length == 1) {
item.tags = item.tags[0].split(/[;;]\s*/).map(tag => ({ tag: tag }));
}
item.extra += addExtra('creatorsExt', JSON.stringify(creatorsExt));
let pdfLink = attr(doc, 'a[href*="create_pdf"]', 'href');
if (pdfLink && !item.attachments.some(attachment => attachment.mimeType == 'application/pdf')) {
item.attachments.push({
url: pdfLink,
title: 'Full Text PDF',
mimeType: 'application/pdf'
});
}
item.complete();
});

let em = await translator.getTranslatorObject();
em.itemType = 'journalArticle';
await em.doWeb(doc, url);
}

function addExtra(key, value) {
return value
? `${key}: ${value}\n`
: '';
}

/** BEGIN TEST CASES **/
{
"translatorID": "d611008a-850d-4860-b607-54e1ecbcc592",
"label": "E-Tiller",
"creator": "jiaojiaodubai23",
"target": "^https?://.*(/ch/)?.*(.aspx)?",
"minVersion": "5.0",
"maxVersion": "",
"priority": 100,
"inRepository": true,
"translatorType": 4,
"browserSupport": "gcsibv",
"lastUpdated": "2023-12-16 19:56:45"
}

/*
***** BEGIN LICENSE BLOCK *****
Copyright © 2022 jiaojiaodubai23@gmail.com
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 paths = [
'reader/view_abstract.aspx?file_no=',
'/article/abstract/'
];

function detectWeb(doc, url) {
Z.debug('---------- E-Tiller ----------');
let insite = Array.from(doc.querySelectorAll(`div[class*="foot"], div[id*="foot"]`))
.some(foot => (/北京勤云科技/.test(foot.textContent)));
Z.debug(`incite: ${insite}`);
if (!insite) return false;
for (let path of paths) {
if (url.includes(path) && doc.querySelector('meta[name="citation_title"]')) {
Z.debug(`match path: ${path}`);
return 'journalArticle';
}
else if (doc.querySelector('meta[name]') && getSearchResults(doc, true)) {
Z.debug(`match path: ${path}`);
return 'multiple';
}
}
return false;
}

function getSearchResults(doc, checkOnly) {
var items = {};
var found = false;
var rows = Array.from(doc.querySelectorAll(paths.map(path => `a[href*="${path}"]`).join(',')))
.filter(element => !(/^[[【〔]?\s*摘要/.test(ZU.trimInternal(element.textContent))));
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) {
if (detectWeb(doc, url) == 'multiple') {
let items = await Zotero.selectItems(getSearchResults(doc, false));
Z.debug('selected items:');
Z.debug(items);
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) {
let translator = Zotero.loadTranslator('web');
// Embedded Metadata
translator.setTranslator('951c027d-74ac-47d4-a107-9c3069ab7b48');
translator.setDocument(doc);
translator.setHandler('itemDone', (_obj, item) => {
item.extra = '';
if (item.title.includes('(网络首发、推荐阅读)')) {
item.extra += addExtra('available-date', item.date);
delete item.date;
item.extra += addExtra('Status', 'advance online publication');
item.title = item.title.replace(/(网络首发、推荐阅读)$/, '');
}
item.language = {
cn: 'zh-CN',
en: 'en-US'
}[item.language];
item.extra += addExtra('original-title', attr(doc, 'meta[name="citation_title"]', 'content', 1));
let creators = doc.querySelector('meta[name="citation_author"]')
? Array.from(doc.querySelectorAll('meta[name="citation_author"]')).map(element => element.content)
: attr(doc, 'meta[name="citation_authors"]', 'content', 0).split(/[,;,;]/);
if (creators.length) {
Z.debug(creators);
item.creators = creators
.map(creator => creator.replace(/(<.+>)?[\d,\s]+(<.+>)?$/, ''))
.filter(creator => creator)
.map(creator => ZU.cleanAuthor(creator, 'author'));
}
item.creators.forEach((creator) => {
if (/[\u4e00-\u9fa5]/.test(creator.lastName)) {
creator.lastName = creator.firstName + creator.lastName;
creator.firstName = '';
creator.fieldMode = 1;
}
});
let enCreators = attr(doc, 'meta[name="citation_authors"]', 'content', 1).split(/[,;,;]/);
let creatorsExt = JSON.parse(JSON.stringify(item.creators));
for (let i = 0; i < item.creators.length; i++) {
creatorsExt[i].original = enCreators[i];
}
if (item.tags.length == 1) {
item.tags = item.tags[0].split(/[;;]\s*/).map(tag => ({ tag: tag }));
}
item.extra += addExtra('creatorsExt', JSON.stringify(creatorsExt));
let pdfLink = doc.querySelector('a[href*="create_pdf"]');
if (pdfLink && !item.attachments.some(attachment => attachment.mimeType == 'application/pdf')) {
item.attachments.push({
url: pdfLink.href,
title: 'Full Text PDF',
mimeType: 'application/pdf'
});
}
item.complete();
});

let em = await translator.getTranslatorObject();
em.itemType = 'journalArticle';
await em.doWeb(doc, url);
}

function addExtra(key, value) {
return value
? `${key}: ${value}\n`
: '';
}

/** BEGIN TEST CASES **/
var testCases = [
{
"type": "web",
Expand Down Expand Up @@ -349,5 +349,5 @@ var testCases = [
"url": "http://wltb.ijournal.cn/ch/index.aspx",
"items": "multiple"
}
]
/** END TEST CASES **/
]
/** END TEST CASES **/
6 changes: 3 additions & 3 deletions chaoxingqikan.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ async function scrape(doc, url = doc.location.href) {
});
newItem.extra = addExtra('titleTranslation', text(doc, '.title_translate'));
try {
let journalDoc = await requestDocument(attr(doc, 'div[class^="sTopImg"] > p:first-of-type > a', 'href'));
let journalDoc = await requestDocument(doc.querySelector('div[class^="sTopImg"] > p:first-of-type > a').href);
let labels = new Labels(journalDoc, '.FbPcon > p, .FbPcon > div > div');
newItem.language = {
中文: 'zh-CN',
Expand All @@ -116,11 +116,11 @@ async function scrape(doc, url = doc.location.href) {
catch (error) {
Z.debug('some error occured while geting journalDoc');
}
let pdfLink = attr(doc, 'a[class*="pdf-down"]', 'href');
let pdfLink = doc.querySelector('a[class*="pdf-down"]');
Z.debug(pdfLink);
if (pdfLink) {
newItem.attachments.push({
url: pdfLink,
url: pdfLink.href,
title: 'Full Text PDF',
mimeType: 'application/pdf'
});
Expand Down

0 comments on commit 4b6ceac

Please sign in to comment.