Does pdf.js execute javascript contained in a pdf file? - javascript

It appears that PDF has, for lack of a better term the "feature" to contain javascript, but luckily it seems there is a change that only some software implements support for javascript
Since web browsers deal already with the issue to handle tons of untrusted input, and hence do some sort of sandboxing, I rather appreciate to open pdf documents inside the browser sandbox via pdf.js. However I wonder if pdf.js will implement the "javascript contained in pdf file misfeature"?

pdf.js still does not support embedded javascript
There are some other implementations (e.g. https://www.pdftron.com/webviewer/demo/pdf-forms) but most of them have some limitations unless you are looking for simple field/form calculations

Update 2022: Since Summer 2021, pdf.js (and by extension, my library ngx-extended-pdf-viewer) uses Quick.js to run JavaScript in a sandbox. You've already mentioned that the browser runs JavaScript in a sandbox, so this is a sandbox running in a sandbox. The Mozilla team didn't want to give the JavaScript code embedded in the PDF file access to browser resources.
If you're using Firefox as a PDF viewer, you're using a slightly different version of pdf.js. It runs the JavaScript code in the same sandbox that runs Firefox WebExtensions.
Here's an article written by the authors of the JavaScript implementation: https://hacks.mozilla.org/2021/10/implementing-form-filling-and-accessibility-in-the-firefox-pdf-viewer/

Related

Ability to load init script in Firefox?

I have a long time stick with Conkeror as my default web browser and get used to configuring/adding new features to my browser using js code with all the XUL Api through the .conkerorrc file. I'm migrating to Firefox since it has better support and is actively maintained by Mozilla. However, one of the feature that I've been missing so much is the dot file, which I can easily configure anything that I like, back up all of them through git and eval the code directly (using Mozrepl) while I'm coding to see the result.
Is there any way that I can inject/execute a sciprt on Firefox startup, for example ~/.firefox/index.js?
There is no functionality in stock Firefox to execute JavaScript code supplied by the user at startup. Functionality like this has been something that has been requested of Firefox since 2006-04-02.
It is trivial to write an add-on in any of the different Firefox add-on types (XUL/Overlay, Restartless/Bootstrap, Add-on SDK, or WebExtensions) which would run whatever JavaScript you desire upon Firefox startup. This could be done to either run code that was included in the add-on (simple), or that runs the JavaScript contained in a file that is loaded from a location external to the add-on (more complex). Which add-on type you used to implemented this would impact which interfaces you had available within the code you write. One drawback of writing your own extension which runs code included in the add-on is that in order to use it with a release, or beta version of Firefox is that you would need to have it signed by Mozilla. While this is a quick and easy process, it does add some additional overhead to the development/test cycle.
You have not specified any of the firefox-addon tags in your question. In addition, you have not described the functionality you desire, except as generalities. It also does not appear to be the intent of your question to ask how you would implement such an add-on. Given those and the fact that there are already multiple add-ons that implement the functionality of running arbitrary JavaScript (including XUL) code supplied by the user, I am not going to supply code here which performs this function.
However, if you are interested in using an already existing add-on, here are a few options:
userChromeJS: This extension was derived from the code originally provided as an example of how to implement the functionality requested in bug 332529. Its first feature listed is: "Complete chrome customization is possible by running custom javascript code or overlaying chrome with .xul overlays." This is an Overlay based add-on with which you can use XUL. This sounds like the functionality what you are interested in obtaining.
uc: "A userChromeJS clone with built-in subscript/overlay loader capability."
Greasemonkey: "Customize the way a web page displays or behaves, by using small bits of JavaScript." This is a commonly used add-on which permits writing more complex JavaScript code. The code is executed in a sandbox, not in the scope of an extension. This is done for security reasons.
Custom Style Script (Inject desired CSS or JS): "Add Custom JavaScript Codes or Styles (CSS) to an specific page or all pages."

browser plug-in or extension, which to choose?

I intend to modify the display of existing websites.
For example, when Google returns search results, can I customize the web page displaying (such as adding some bubble visualizations onto that page's blank places) ? Could it be done using browser plug-in or extension?
Based on my knowledge, a browser extension, such as firefox or chrome extension, is usually for adding tiny icons to the address bar.
Could it be done using browser plug-in or extension?
Yes. But you don't want to write a browser plug-in. They're fairly complicated to write and users are reticent to install them (with good reason) Further, you'd have to write two, as Mozilla and Google can't agree on a format (Chrome is removing the venerable NPAPI, but Mozilla won't implement Google's PPAPI, claiming that it's a moving target with inadequate documentation).
Based on my knowledge, a browser extension, such as firefox or chrome extension, is usually for adding tiny icons to the address bar.
No. Firefox add-ins and Chrome extensions can do a lot more than that, including modifying the page once it's rendered.
So you'll probably want to write a Firefox add-in and a Chrome extension. (Sadly, yes, that's two codebases to maintain, although they use similar technologies — HTML, CSS, JavaScript — so you can share a bit between them.)

JavaScript to read local file

Is there any way to enable native JavaScript to read local file on the disk?
no Ajax, no JQuery or other JavaScript library, only use the native JavaScript?
I have searched, but it seems there is no way to do this.
My requirements are that when the Internet can not be accessed, I will show a warning page in the Chrome browser.
HTML5, have a File Api, if you are interested in reading file, just for the reading - MSDN, HTML5rocks.
If you are more interested in the case when the user is offline, and your application needs a storage, you can take a look at localstorage - MDN, diveintohtml5. There is an example how to check if it is available, although CanIUse says it is currently available everywhere. Here is a live example.
Hope this will help, in your case which seems to be the synchronization after being offline.
If you are using JavaScript in a browser, there is no way to access an arbitrary file from the local filesystem like this. XMLHTTPRequest can be used to access files natively without a library like jQuery, but it prevents access to local files as a security measure.
However, you can use a cache.manifest file to set up an offline version of an HTML 5 site/app.
You can use a combination of <input type="file"/> and Javascript's window.FileReader to read file. It has many methods of receiving the data, easiest being FileReader.readAsBinaryString(file); You can only read files that a user has selected from a file browser.
Here's Mozilla Developer Network's documentation on it.
For your uses, however, it seems like developing a Chrome extension would be a better solution to manipulating the way Google Chrome works.

Can we create browser extension for firefox using javascript

i tried to write a script which can be added as an extension to the browser. The problem with this script is how to access the dom elements of a webpage. i used XMLHttpRequest object for accessing the web page but it has same origin policy issues. I worked on it for nearly 3 weeks finally i used Anyorigin.com code but i am not able to get all the functionality so i thought of using userscript where we can easily access the web page's DOM elements. Is ther any other way to create an extension
While enloz's answers are good, Firefox has an Add-on Builder that you can you use.
Add-ons are built using HTML, CSS and JavaScript. There is a web based version with the following features (quoting from their website):
Edit code in a feature-rich environment
Test your add-ons instantly
Use built-in version control & sharing
You can find the web based version here: https://builder.addons.mozilla.org/
Try this:
Greasemonkey:
Customize the way a web page displays or behaves, by
using small bits of JavaScript.
https://addons.mozilla.org/en-US/firefox/addon/greasemonkey/
Update:
This looks to be a better solution, but I haven't tested it.
http://crossrider.com/

Understanding browser plugins: creation, deployment, and use

Let's say I wanted to create a browser plug-in that would open someone's floppy drive for them whenever they click a button on my web page. (You remember that old gag? - Don't worry, I don't really want to do that.)
What are the steps necessary to create a browser plug-in that will work on most browsers and most operating systems?
How do I deploy the plug-in? For instance, how do I make it so that the plug-in can be easily downloaded and installed? Do I have to worry about digital certificates?
How do I check for and use the pluggin once it's installed? Is it available to javascript in the form of an API?
Take a look at a project called FireBreath: http://www.firebreath.org/display/documentation/FireBreath+Home
Firebreath abstracts many of the differences between the browser plugin frameworks and platform issues so you can easily write the code for a plugin once and just re-compile it for different platforms.
I have found it good for exposing existing C/C++ library API's to Javascript so I can write dynamic pages that use functionality from the C++ library which were otherwsie not available.
Most modern browsers have their own plugin frameworks for developers to utilize. So, for example, a Firefox plugin will not work in chrome or IE. Firefox and chrome do both utilize javascript and css but they are fundamentally different in their structure.
As far as deployment, Mozilla and Google provide hosting for their respective plugins. I do know that firefox extensions are basically zip files with the extension changed.
EDIT...
I had assumed you meant browser extensions so disregard my answer if that is not the case.
The easiest cross-browser solution is use Java Applets or maybe Flash (I am not sure how you do it in Flash).
With applets, you would need to sign the applet and also create a security policy file for accessing the disk. When the applet loads in the browser it would ask permission from the client user, he/she needs to click Allow to give permission for the applet to access the disk. This would allow you to access any disk in the system.

Categories

Resources