Tutorial :Run a javascript file on a set of html documents



Question:

I have a set of html documents on local disk to process. I need to load each document file as document object and run a piece of javascript code over it and store the result in a separate file. I would prefer to this from python though I'm open to any other suggestions too. I tried to do this through extensions in chrome and firefox but the browsers do not allow javascript to access files on disk. Is there a solution for this?

I tried python-spidermonkey but it allows only javascript execution. I doesn't seem to be able to load the html file as document object.


Solution:1

What I would do is run a local web-server. Then you can send data to the server (with JavaScript, Ajax) to save to disk whatever you need.

To load the HTML file you can also usa Ajax. You could simply use .innerHTML to load the HTML string into the Document.


Solution:2

If you are on a Windows pc you also have the option of doing it in an .hta file. You can use vbscript to access the local files system. They syntax is very simple and you can use DevGuru to get all your reference. I use vbscript to create Photoshop scripts and it works fine. I am sure it can do what you need as well.


Solution:3

I got it working with pyqt and webkit


Solution:4

You can't read or write files using JavaScript in a browser environment. You should try using server-side javascript.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »