0

Below are my codes in extension.js. If you look at the codes, I tried different ways to load the file to my extension. No matter what, I always getting

VM3051:15 Uncaught ReferenceError: PDFJS is not defined

Tried with putting the file in different locations.

appAPI.ready(function($) {
  console.log("pdf min js loading");
  appAPI.resources.includeJS('jspdf.js');
 // appAPI.resources.includeJS('js/jspdf.js');
// appAPI.resources.includeRemoteJS('//cdnjs.cloudflare.com/ajax/libs/jspdf/1.2.61/jspdf.min.js');
 //$.globalEval(appAPI.resources.get('//cdnjs.cloudflare.com/ajax/libs/jspdf/1.2.61/jspdf.min.js'));
 console.log("done");
    setTimeout(function(){
        alert(window.location.href);
    if(window.location.href.indexOf(".pdf") > -1) {
        console.log("its a pdf");

        alert("pdf");
        var doc = new jsPDF();

    }else{
    alert($.trim($('div').find('h1,h2,h3,h4,h5,p,span').text()));
    }

    },6000);
});

Here is the file structure

enter image description here

I cannot modify manifest.json because the extension should be unique for all the browsers not just for chrome.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • Note: `window.location.href.indexOf(".pdf") > -1` isn't reliable. Use something like `window.location.href.endsWith(".pdf")`. – gcampbell Jul 06 '16 at 13:29
  • @gcampbell Yes .. sure. I'll update it soon. Just testing now. – Suresh Atta Jul 06 '16 at 13:31
  • @sᴜʀᴇsʜᴀᴛᴛᴀ, I guess you mean `jspdf.min.js`, could you please also provide `manifest.json`? – Haibara Ai Jul 07 '16 at 00:06
  • @HaibaraAi Yes. Just added my manifest. And also tried putting it in lib folder of extension and given the path in manifest. Still the same error. – Suresh Atta Jul 07 '16 at 04:18
  • File structure screenshot is hardly useful as it's incomplete and contains extra paths like `Resources` that aren't used in the posted code. There's also a capital letter in `Settings.json` so make sure the file name is exactly the same or simply *always* use lowercase. – wOxxOm Jul 09 '16 at 11:23
  • @wOxxOm Settings json is fine and if you readed full code, `// appAPI.resources.includeJS('js/pdf.js');` is access the files from js folder of resources. – Suresh Atta Jul 09 '16 at 12:40
  • Crossrider [documentation](http://docs.crossrider.com/#!/api/appAPI.resources) says each resource is limited to 100kB, but pdf.js is larger. Maybe that's why it's not loaded. Actually, I'm not sure why you don't load it as a normal content script. – wOxxOm Jul 09 '16 at 13:01
  • @wOxxOm The pdf js is already minified and I cannot (don't know) how to reduce its size further. Yaa I tried the content script way, no luck. Added the path in manifest as content script (tried full path (http://... & js/jspdf.min.js)) with no luck. Do you see any problem in using the codes ? – Suresh Atta Jul 09 '16 at 13:58

1 Answers1

2

I'm confused, the two CloudFlare URLs in your code reference the project jsPDF. I would assume the local pdf.js does the same too.

In your code, you're using

PDFJS.getDocument();

This syntax comes from PDF.js which is a totally different project from Mozilla.

If you're sticking with jsPDF, your code should be something like:

var doc = new jsPDF();
doc.text(20, 20, 'Hello world.');
doc.save('Test.pdf');

Or you'll need to include the correct library for PDF.js.

After the edits you've made and your comments, it seems you've switch completely over to jsPDF but you're still getting the same error which clearly mentions PDF.js.

Are you sure you're debugging the correct and last version of your app which is only using jsPDF?

I've setup a small reproduction example on Crossrider using only jsPDF.

Project structure

The extension.js code is the following:

appAPI.ready(function($) {
  console.log("pdf min js loading");

  appAPI.resources.includeJS('jspdf.js');

  console.log("done");

  var doc = new jsPDF();

  console.log(doc);
});

When debugging the extension, I'm getting this result:

Output

doc is an object containing an instance of jsPDF which I can later use.

There should be no mention of PDF.js whatsoever. My only guess could be that you're running / debugging a version of your extension still containing references to this project.

HiDeoo
  • 10,353
  • 8
  • 47
  • 47
  • Edited a bit. Hope that clears. I am just using jsPdf and tried using different ways of loading it. When I use in content scripts it is working. But it questions the support of mozilla and intenet explorer. – Suresh Atta Jul 12 '16 at 09:50
  • So what's the error now, I'm assuming you're not getting `Uncaught ReferenceError: PDFJS is not defined` anymore since you switched to jsPDF ? – HiDeoo Jul 12 '16 at 10:06
  • I am getting the same error. The problem is it is not being loaded through jquery getScript or eval or inline etc. It is working only through content scripts – Suresh Atta Jul 12 '16 at 10:18
  • I've edited my answer to add a working example with no error mentioning PDF.js and a wild guess on why you would get reference to this project without using it. – HiDeoo Jul 12 '16 at 10:56
  • Thanks Man. You made it possible. I tried many other ways and failed. If I remember correctly, I tried this way too :) But a fresh start might cleared off the things. Thanks again. – Suresh Atta Jul 12 '16 at 11:16