Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
刚才我花了点时间找到了让划词翻译支持 PDF 文件的方法,在这里记录一下。
首先要说明的是,官方提供的结合 Webpack 使用的例子只能将 PDF 转换为 canvas,划词翻译还是没法使用,所以我需要的其实是 pdf.js 里的 viewer。
详细步骤
pdfjs-1.3.91-dist.zip
并解压到./src
的pdfjs-1.3.91-dist
目录下./src/pdfjs-1.3.91-dist/web/viewer.js
,注释掉其中的第 7157-7159 行。这是为了避免 view.js 在检测到不同源的 PDF 文档时报错——在 Chrome 扩展程序里是可以进行跨域请求的。./src/pdfjs-1.3.91-dist/web/viewer.html
,并在</body>
前加上下面几行代码:现在,在浏览器内打开
chrome-extension://bocneeaaedgmgcehokhbgffchgbbhllk/pdfjs-1.3.91-dist/web/viewer.html?file=http%3A%2F%2Fmozilla.github.io%2Fpdf.js%2Fweb%2Fcompressed.tracemonkey-pldi-09.pdf
就能预览 http://mozilla.github.io/pdf.js/web/compressed.tracemonkey-pldi-09.pdf 的 PDF 文件,并且划词翻译能正常工作。工作原理
chrome-extension://*
类的网页里,所以要手动在 viewer.html 底部加上内容脚本的代码。?file=pdf文件的URL
(pdf 文件的 URL 需要使用encodeURIComponent()
编码),viewer.html 就会加载 PDF 文件了。需要解决的问题
pdfjs-1.3.91-dist.zip
里有近 400 个文件,但需要改动的只有两个文件,所以我需要使用编程的方式将它下载到项目中及改动文件(使用postinstall
)。.pdf
结尾,或者使用 chrome.webRequest 检测(后者好些)此 PR 就解决了上面的两个问题。相关 issue:#69