Developing a add-on/extension to view PDFs

I realize that Firefox already has built-in PDF support, but I’m trying to develop an add-on to replace the default viewer. When the user clicks on a PDF link, I want it to open in my add-on.

I’ve scoured https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/What_are_WebExtensions, but I don’t see how to do this.

In Chrome, I can use the file_browser_handlers option in the manifest.json. This doesn’t seem to exist for Firefox.

How can I accomplish this?

I can see at least two ways (with them being achievable in multiple variations as far as I’m aware):

  • Redirect PDFs to an URL within your extension that then handles loading and displaying the PDF. Breaks “Save page as” for saving the PDF
  • Replace the content of the PDF response with your PDF viewer. Means it gets displayed for the URL the PDF is at, but also breaks “Save page as”.

Thank you Martin.

I suppose I might be able to achieve Save As by generating a blob for download. But this might just be a proof of concept anyway.

Redirect PDFs to an URL within your extension that then handles loading and displaying the PDF. Breaks “Save page as” for saving the PDF

This sounds easiest, but this concerns me a bit. I guess the way this would work would be that the user clicks on a link to say, “http://www.pdf995.com/samples/pdf.pdf” and then the extension redirects to www.mycustompdfviewer.com?pdf=http://www.pdf995.com/samples/pdf.pdf which opens the pdf. Is that what you’re thinking? Would there be issues with popup blockers?

The downside would be that the final link isn’t so clean.

Replace the content of the PDF response with your PDF viewer. Means it gets displayed for the URL the PDF is at, but also breaks “Save page as”.

I’m not sure exactly what you mean by “the content of the PDF response”. Would I replace the GET results of the HTTP call to the PDF, and with what? Can you say more?

It would redirect to moz-extension://{local-guid}/viewer.html?url=https://example.com. As such the page is local and not remote.

The code of your viewer, so likely the HTML and the required bootstrap data. Since replacing the content of a request means you also get to read the original data, you can easily send the PDF data to the viewer afterward.

An example to study would be https://github.com/nt1m/livemarks/ which I think does one of the two methods for RSS and ATOM feeds (so special xml files).