Run JavaScript code on a page without showing browser window

I have a question regarding JavaScript and methods by which its code can be run. As we know, there is some JavaScript code embedded in tags and it runs natively within browser window - that's quite common and that's how the web pages currently work.

If we want interaction out of the box with the web page we are surfing, we must use tool such as GreaseMonkey and write our UserScripts that allow us to change page behavior to our desired one. This still needs us to open browser window and visit our target.

The question is: can we somehow run JavaScript code on a certain page without even opening browser window? I mean, in an ideal solution there would be a command-line tool that'd allow us to call:

./clibrowser http://google.com userscript.js

In which script, for example, we could run some AJAX code to fetch data coming from queries and pass it to some other PHP script that's save it to database.

I think that this question simplifies to: are there any tools like "command-line browser" with JavaScript engine? I know, that it may sound strange, but I am curious just because of the idea I have in my mind.

Answers:

Answer

Yes, there is. There is NodeJS, which runs on Chrome's V8 javascript engine: http://nodejs.org/.

There is also Rhino, which runs on Java: http://www.mozilla.org/rhino/.

The thing about these is that they don't include a "browser" as such... they are just javascript engines that can execute javascript, but don't provide the ability to parse/manipulate HTML, out of the box. However, there are javascript libraries built upon these engines that do simplify most of the work for you.

Answer

There is phantomjs: http://phantomjs.org which is a webkit-based headless browser with full support for javascript. It exposes an interface that lets you navigate to a page, interact with it and run whatever scripts you want in that page's context.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.