Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
4 tasks done
JHB-L opened this issue May 9, 2024 · 3 comments
Closed
4 tasks done

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

JHB-L opened this issue May 9, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@JHB-L
Copy link

JHB-L commented May 9, 2024

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

  • 无法保存条目

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

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

image

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

浏览器

  • Chrome(谷歌浏览器)

自查清单

  • 我已经按照教程将翻译器更新到最新版
  • 我尝试过重启浏览器、Zotero

附件
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 JHB-L added the bug Something isn't working label May 9, 2024
@JHB-L
Copy link
Author

JHB-L commented May 9, 2024

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

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

@jiaojiaodubai
Copy link
Collaborator

jiaojiaodubai commented May 10, 2024

@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
Copy link
Author

JHB-L commented May 10, 2024

@jiaojiaodubai 非常感谢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants