l0o0 / translators_CN

Zotero translator中文网页抓取翻译器🎉This is Zotero translators for Chinese Sites(beta), not the official Zotero repo
GNU Affero General Public License v3.0
4.07k stars 524 forks source link

使用Pss-System.js(昨天添加的,感谢)保存报错 #340

Closed JHB-L closed 5 months ago

JHB-L commented 5 months ago

你遇到了什么问题? [必填]

发生问题的链接 [必填] 国家知识产权局专利检索及分析系统

问题描述 [必填] 浏览器可识别条目,保存时报错

image

你的预期结果 可以保存相关条目及pdf附件

浏览器

自查清单

附件 Connector报错记录:

[JavaScript Error: "Error: No title specified for item
    at Object._itemDone (chrome-extension://ekhagklcjbdpajgpjgmbionohlpdbjgc/translate/translation/translate.js:615:32)
    at Object._itemDone (chrome-extension://ekhagklcjbdpajgpjgmbionohlpdbjgc/inject/sandboxManager.js:89:17)
    at Object.complete (chrome-extension://ekhagklcjbdpajgpjgmbionohlpdbjgc/translate/translation/translate.js:1965:26)
    at doWeb (eval at <anonymous> (chrome-extension://ekhagklcjbdpajgpjgmbionohlpdbjgc/inject/sandboxManager.js:63:4), <anonymous>:74:10)
    at Zotero.Translate.Web.rest (chrome-extension://ekhagklcjbdpajgpjgmbionohlpdbjgc/translate/translation/translate.js:1346:49)
    at chrome-extension://ekhagklcjbdpajgpjgmbionohlpdbjgc/translate/translation/translate.js:1331:39" {file: "[object Object]"}]
JHB-L commented 5 months ago

测试了一下,好像就国内专利保存不了,不确定是否和网络环境有关。

还有就是可否加入这个pdf下载功能,保存pdf附件。测试的外国专利保存正常,但全是屏幕快照的形式 image

jiaojiaodubai commented 4 months ago

@JHB-L 网站的 PDF 下载使用 POST 请求来获取文件,相关参数由网页自身维护(基本上,当你搜索出来的时候就在内部确定了),这些参数难以从外部获取。目前无法在 translator 中实现连同 PDF附件抓取,你只能手动下载PDF。

不过我注意到下载的PDF文件名有规律,基于此为你编写了实用的代码来导入这些手动下载的 PDF,使用步骤如下:

  1. 从浏览器抓取条目到 Zotero 中;
  2. 手动下载同一页面上的 PDF 文件;
  3. 在 Zotero 中选中那些要导入附件的专利;
  4. 在 “工具—开发者—Run JavaScript”中输入下文贴出的代码,将代码中的 downloadDir 的值替换为你的下载路径,勾选“Run as async function”,然后点击窗口左上角的“Run”按钮来运行代码。
const debug = [];
// 设置下载目录
const downloadDir = "C:\\test\\";
debug.push(`use ${downloadDir} as root dir`);
const patents = ZoteroPane.getSelectedItems().filter(item => item.itemType == "patent");
let pdfs = [];
await Zotero.File.iterateDirectory(
    downloadDir,
    async (child) => {
      if (!child.isDir && /\.pdf$/i.test(child.name)) {
        pdfs.push(child);
      }
    }
);
for (const item of patents) {
    const msg = [];
    msg.push(`process item: ${item.getDisplayTitle()}`);
    let patentNumber = item.getField("patentNumber");
    if (!patentNumber || Zotero.Items.get(item.getAttachments()).some(attachment => attachment.isPDFAttachment())) {
        msg.push(`skip.`);
        continue;
    };
    for (pdf of pdfs) {
        const basename = pdf.name.slice(0, -4);
        const theSame = basename == patentNumber;
        // 不要使用.includes(),否则文件名很短时可能导致意外的匹配
        const include = new RegExp(`^${basename};\s?|;\s?${basename}$|;\s?${basename};\s?`).test(patentNumber);
        msg.push(`patent number: ${patentNumber} = base name: ${basename}? ${theSame}`);
        msg.push(`patent number: ${patentNumber} in base name: ${basename}? ${include}`);
        if (theSame || include) {
            await Zotero.Attachments.importFromFile({
                file: pdf.path,
                libraryID: item.libraryID,
                parentItemID: item.id
            });
            pdfs = pdfs.filter(file => file != pdf);
            msg.push( `add attachment: ${pdf.name}. `);
            // 在下一行的前面输入两个“/”,即可避免匹配附件的同时删除本地的 PDF 文件
            Zotero.File.pathToFile(pdf.path).remove(true);
            msg.push( `remove file: ${pdf.path}. `);
            break;
        }
    }
    if (!Zotero.Items.get(item.getAttachments()).some(attachment => attachment.isPDFAttachment())) {
        msg.push(`no attachment matched.`);
    };
    debug.push(msg.join("\n"));
};
return debug.join("\n\n");
JHB-L commented 4 months ago

@jiaojiaodubai 非常感谢