简体   繁体   English

如何在Firefox插件中使用pdf.js库?

[英]How to use pdf.js library in a firefox addon?

I'm making a Firefox add-on extension to insert a pdf document in the translated page of Google and I want to use pdf.js . 我正在制作Firefox附加扩展程序,以便在Google的翻译页面中insert pdf文档,并且我想使用pdf.js Don't want to use embed or iframe tags to do it, I want to do my own pdf viewer. 不想使用embed或iframe标签来做,我想做自己的pdf查看器。 I'm trying to use the pdj.js library in a Firefox add-on, but it not work. 我正在尝试在Firefox加载项中使用pdj.js库,但是它不起作用。 I have already tried to do it adding this in manifest.json . 我已经尝试过将其添加到manifest.json

    "content_scripts": [ 
    {
      "matches":["*://translate.google.com/*"],
      "js": ["script.js","pdf.js"]
    }
   [enter image description here][1] ]

but when I do: 但是当我这样做时:

pdfjsLib.getDocument(pdf_url); 

it not working, the script stop working. 它不起作用,脚本停止工作。

I do try to add the script in the head of the page doing 我确实尝试在页面的顶部添加脚本

var pdf_js = document.createElement('script');
pdf_js.src = 'https://mozilla.github.io/pdf.js/build/pdf.js';
document.getElementsByTagName('head')[0].appendChild(pdf_js);

and it does not work either. 而且也不起作用。 But when I use pdfjsLib from debug console in Firefox it works, but from the script that I use for the addon, pdfjsLib.getDocument function it not working. 但是,当我从Firefox的调试控制台中使用pdfjsLib时 ,它可以工作,但是从我用于插件pdfjsLib.getDocument函数的脚本中,它却无法工作。

I had a similar problem when I used jQuery, the library does not work in the script. 使用jQuery时,我遇到了类似的问题,该库在脚本中不起作用。

I tried watching the navigation console as suggested by Jaromanda X and I got this 我试着按照Jaromanda X的建议观看导航控制台,我明白了

ReferenceError: ReferenceError:

pdfjsLib is not defined pdfjsLib未定义

I solved the problem. 我解决了问题。 The problem was that the manifest.json in the "content_scripts" , in "js" , the order of scripts was is bad.. 问题在于"content_scripts"中的"content_scripts" manifest.json "content_scripts""js"的脚本顺序是错误的。

I had first and this not work because script.js (the addon script) is before that pdf.js 我首先遇到了这个问题,因为script.js (附加脚本)在该pdf.js之前

   "content_scripts": [ 
    {
      "matches":["*://translate.google.com/*"],
      "js": ["script.js","pdf.js"]
    }

the correct way is. 正确的方法是。

   "content_scripts": [ 
    {
      "matches":["*://translate.google.com/*"],
      "js": ["pdf.js","script.js"]
    }

in case of use Jquery, should are before of script.js too 如果使用Jquery,也应该在script.js之前

 "js": ["jquery.min.js","pdf.js","script.js"]

我通过添加以下行来解决它:

pdfjsLib.GlobalWorkerOptions.workerSrc = '//mozilla.github.io/pdf.js/build/pdf.worker.js';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM