Is there a way to execute JavaScript in Perl? - javascript

I've worked some years now in Perl building Web scrapers, and given the problem that spam represents, and how scraping Web pages would turn out to be much more easier to those folks if, for example, Perl's LWP::UserAgent could handle its cup of JavaScript, I'm amazed no one has built a JS engine for it yet.
What am I missing here?
Thanks in advance. Regards.
PS: I'm not a spammer. Just curious.

Would you mean something like JavaScript::SpiderMonkey, a Perl interface to a JavaScript engine used by Mozilla?

WWW::Selenium?

TMTOWTDI.
Another option is WWW::Scripter, with the Javascript or AJAX plugin.

There is also Win32::IE::Mechanize, Mozilla::Mechanize. But the previously mentioned WWW::Selenium is the most DWIW and well-supported if you have access to browsers and can run the Selenium server. Selenium is a Java critter that runs the browser interactions for you. It has IDEs for several browsers and can write code for you—by recording browser actions—in several languages including Perl or you can hand write it. It’s test-centric, where it excels, but there is no reason not to be use it for general automation/scraping.

You can try to install SpiderMonkey and -in your perl program- execute javascript in backticks, and capture the result, just like from any other unix command line tool. Spidermonkey has a command line option for that, similar to perl's -e command line option. The spidermonkey binary is called "js", thus:
/path/to/spidermonkey/bin/js -e "print(10);"
> 10
I think you can also install v8-shell as an alternative engine, but then you must install 'scons' first, which is available on unix-only.
./v8-shell -e 'print("10*10 = " + 10*10)'

Related

How to compile Javascript console application locally?

I am a beginner in Javascript, I decided to practice Javascript by problem solving using it, I found an online judge that accepts Javascript V8 4.8.0 code.
So, I searched online to get that version of Javascript V8 on my machine, but I couldn't find any easy way, All the pages were explaining how to build it, and it seems to be a process that I don't need to go through.
Is there an easy way to compile and run command line apps written in Javascript on my machine?
Note: I don't want to use node.js because I tried using it's I/O and
as a beginner I think it is complex in some way.
Update: I found that package manager pbox.me which provides a version of V8 JavaScript Engine and I managed to install it.
Yet another problem appeared: whenever I try to run a js file writing d8 myfile.js in command line nothing happens as if it is an empty program, knowing that I tryied to d8.exe file and it is working, and I made sure the PATH is inserted in the environment variables.
What am I doing wrong?
The easiest way to get started with JavaScript is probably to use it in a browser. You can type simple things directly into the browser's JavaScript console (check the menu); or you can embed your code in a simple HTML document.
If you want, you can even pretty easily implement the readline()/print() functions, so you can pretend to be doing stdin/stdout based I/O: just read from an array of strings, and send output to console.log (or create DOM nodes if you want to be fancy and/or learn how to generate dynamic website content by hand).
Side note: V8 4.8 is severely outdated, don't use it to execute code you haven't written yourself.

How to run JavaScript/AppleScript from cocoa app?

I am trying to execute some AppleScript from Objective-C using NSAppleScript... however, the code I am trying is the new JavaScript for automation in Yosemite. It does not appear to do anything when it is run, however, normal AppleScript works fine.
[NSApp activateIgnoringOtherApps:YES];
NSAppleScript *scriptObject = [[NSAppleScript alloc] initWithSource:
#"\n\
iCal = Application(\"Calendar\");\n\
iCal.includeStandardAdditions = true;\n\
iCal.activate();\n\
iCal.displayAlert(\"testing\");\n\
"];
[scriptObject executeAndReturnError: nil];
How can I get this to run?
Thanks
NSAppleScript is hardwired to compile source code as AppleScript. You'd need to use OSAKit instead, which is shoddy and undocumented, but at least allows you to specify which language to use when compiling from source. Alternatively, you could kludge a workaround by piping your source to osacompile -l JavaScript, then loading the resulting compiled .scpt file into NSAppleScript.
However, it's not clear from your example why you're using NSAppleScript. If you want to execute user-supplied scripts you should probably look at NSUserAppleScriptTask: it's even crappier than NSAppleScript, but it's designed to run the script in a subprocess outside your app's sandbox. (NSAppleScript runs scripts in-process, which in a sandboxed app prevents user scripts talking to arbitrary applications.)
OTOH, if you're only using NSAppleScript to run your own code, you'd be much better using Scripting Bridge (which is crappy and broken, but may be sufficient if your needs are modest), or use AppleScript via the AppleScript-ObjC bridge, which allows your ObjC code to use AppleScript-based 'classes' much as if they were native Cocoa classes. Given that AppleScript is the only supported solution that knows how to speak Apple event correctly (JXA's riddled with flaws too), I'd recommend the latter.

How to interpret JavaScript with Python

It is possible to run JavaScript with Python? There are any library that makes this possible?
I need to execute some JavaScript, I know that this is possible with some Java libraries, but I prefer Python.
Can someone give me a clue on this?
Best Regards,
You can check spidermonkey
If you already use PyQt and QWebView in it, displaying custom html, the function evaluateJavaScript of QWebFrame may be useful for you:
# Python
def runJavaScriptText(self, jsText):
jsText = 'hello()' # just to fit javascript example
self.webView.page().currentFrame().evaluateJavaScript(jsText)
// Javascript
function hello() {
alert('hello');
};
Using spidermonkey would give you a tightier integration of your code, but as a workaround, you could make the javascript get run in a browser using Selenium remote-control:
http://seleniumhq.org/projects/remote-control/
(there are ways of doing that without needing a "physical" display for the browser, using VNC servers, for example)
Does it need to be CPython ?
And does the Javascript need a browser client environment ?
If not, you can probably call Rhino from Jython.
( Note also that Jython release is only at 2.5.2 )
Yes you can execute JavaScript from Python. I find the easiest way is through Python's bindings to the webkit library - here is an example. In my experience selenium and spidermonkey are harder to get working.

Javascript as command line callable scripting language

I'm looking for a way to write some simple scripts in javascript, like I would in ruby. For example, I might write a script like:
var str = "Hello World";
console.log(str);
And i want to able to call it from my command line like this:
js hello_world.js
Is there some sort of Javascript runtime, that includes a standard library that would allow me to do this style of script development? I know there is node.js, but that is specific to a web server, right?
Node.js has emerged as the clear winner here, at least for me.
http://nodejs.org/
Well, there's JScript.NET:
http://www.devsource.com/c/a/Techniques/JScriptNET-Applications-at-the-Command-Line/1/
As mentioned in this previous question, you might want to check out Rhino, an implementation of Javascript written in Java. Specifically, the page on the JavaScript shell might be of interest.
Hope this helps.
what about mozilla's spidermonkey ?
apt-cache search javascript interpreter
spidermonkey-bin - standalone JavaScript/ECMAScript (ECMA-262) interpreter
libjenkins-htmlunit-core-js-java - Jenkins branch of the HtmlUnit Core JS Interpreter
gnome-js-common - Common modules for GNOME JavaScript interpreters
With Phantom JS you can easily run a javascript from command line and it naively supports DOM, CSS Selector and JSON.
Why do you want to do this in javascript? Why not use a language that is suitable for this type of command line environment such as Perl? Javascript's main purpose is to faciltate client-side browser control.

Javascript for command line utilities

Given a need to write command line utilities to do common tasks like uploading files to a remote FTP site, downloading data from a remote MySQL database etc.
Is it practical to use JavaScript for this sort of thing? I know there are JavaScript interpreters that can be run from the command line, but are there libraries for things like FTP and database access the way there are for e.g. Java? If so, what's the best place to look for them? (Google searches with JavaScript in the keywords always seem to return many pages of browser specific things.)
And is there a way to package a JavaScript program up as a standalone executable on Windows?
Update: I've decided Python is a better tool for this kind of job, but the answers to the original question are still good ones.
Standalone executable?
By the way you ask the question, I'm not sure if you are aware, but the Windows Script Host - included in Windows - allows you to run .js files from the command-line. Your javascript will not be an executable, it will remain a script, a text file. The script runs within cscript.exe, which is provided by WSH. There's no compilation required. Maybe you knew all that.
I use Javascript this way for various utilities on Windows.
I think your instinct is right on the availability of libraries. You are sort of on your own to find all those things. Although, once you find them, it's not hard to package Javascript libraries as COM components and allow re-use from anywhere. See here for an example of packaging the Google Diff/Patch/Match Javascript library in COM.
Addendum: Once a bit of code is available within COM, it can be consumed by any Javascript running on the machine. Some examples of COM objects available to Javascript scripts running in WSH:
MSXML2.XMLHTTP object - used in AJAX, but can be used for any HTTP communication. There also an object for the XSLT engine so you can do transforms from script.
Excel.Application - allows you to open up Excel spreadsheets and automate them from Javascript.
Communicator.UIAutomation - automate MS Communicator (send IM's via script)
COM objects for Google Earth.
SlowAES - an all-Javascript implementation of AES encryption.
You can use Rhino to compile Javascript into Java byte code, and get access to all Java libraries.
Or you could use JScript.net, and get access to the .net libraries. .net includes a jsc.exe that produces exe-files.
Both of these requires the respective framework to be installed to be able to run.
Node.js is by far the best environment for running non-browser JS. I've used Rhino and SpiderMonkey, and there's a pretty huge difference in everything from the basics like how errors are handled to the size of the community using the tool. Node is pitched for "server-side" JS - building server apps in JS. It's great for this. But it works equally well for building command line tools.
The NPM package manager (bundled with Node) provides a nice global directory for finding and installing packages. It works much better than other language equivalents like PECL / Pear / CPAN / etc. Several high quality tools like JSHint, The Jade templating language, and the CoffeeScript compiler are all already available through NPM/Node:
npm install -g jshint, coffee-script, jade
jshint my_code.js
jade < my.jade > my.html
For args parsing, there are packages like commander.js. I currently use a heavily extended version of Commander in my underscore-cli command-line tool.
For messing with JSON or for doing command-line JS work (similar to "perl -pe"), check out underscore-cli - It's a really powerful tool for processing JSON data, processing underscore templates, and running JS expressions from the command-line. I use it for 1001 different things that would otherwise be really annoying to accomplish.
Rhino is bundled with JDK 1.6, jrunscript.exe in the bin directory will allow you to run any Javascript you want. Since it runs under Java you get access to any Java libraries that you may have.
We use it from the command line extensively. It's very good at that.
One way is to write these utilities as AIR applications - They can be written in JavaScript and need not have a UI. They have access to the command line, and there are existing ActionScript 3 libraries that can handle FTP etc. These ActionScript APIs can be called from JS, in AIR applications. AIR applications also have access to a sqlite database.
jslibs is a good standalone JavaScript runtime that support many 3rd party open source libraries like zlib, SQLite, NSPR, libiconv, libTomCrypt, OpenGL, ...

Categories

Resources