4

我有一个 pdf 文件,里面有很多外部链接。这些链接指向 youtube 视频中的特定时间位置,但这对于问题并不重要。我还有一个带有 pdf 查看器的网页,它基于examples/components/simpleviewer.htmlpdf.js 存储库中的示例。我想不以原始形式显示这些链接,而是让它们调用一些 javascript 代码,以便我能够自己对单击链接做出反应,而实际上并不遵循原始链接目的地。

首先,我试图从 npm 包中扩展类PDFViewer,但我没有设法找出在那里覆盖什么以及如何覆盖。所以我通过修补 AnnotationLayer 类做了一个肮脏的调整。现在一切正常,但这种解决方案可能会导致将来出现问题。我想找到实现相同目标的假定方法。web/pdf_viewer.jspdfjs-dist

这是我在打字稿中的解决方案:

let AnnotationLayer = (<any>pdfjsLib).AnnotationLayer;
let old_annotations_renderer = AnnotationLayer.render;
AnnotationLayer.render = (parameters:any) => {
    for (let annotation of parameters.annotations) {
        if (is_video_url(annotation.url))
            annotation.url = `javascript:console.log('click on ${annotation.url}')`;
    }
    old_annotations_renderer(parameters);
};
4

0 回答 0