I have written some javascript which reads data in a textarea (inside a browser)
and outputs the result in a string (as innerHTML) in a div.
It's brilliant and it does its job.
I would like to port it out of the browser.
I would like to reuse the same javascript (which effectively is just a function that given a string returns another string) out of the browser.
Ideally I would like to run it a commandline tool and say read file c:\input.txt and writes the response on c:\output.txt
Can somebody suggest how best I can achieve this ?
I looked around and came across to commonjs.org (which is very interesting) and node.js
(which is interesting too) as well as other obscure projects.
Yet, I still don't know how to do this. If somebody has done something similar I would like to hear.
Many Thanks,
p.s.
I tagged this question 'server-side javascript' mainly to say that it's not a browser question.
Rhino from Mozilla is javascript interpreter written in java which might do what you need.
If your're on a windows system, you could the native c-/wscript interpreter using the FileSystemObject to read and write files. Otherwise jsdb may be an option, where you can use the Stream-object.
http://RingoJs.org
It's a thin wrapper on top of Rhino. Makes dealing with files, modules, etc a lot easier, see for example: http://ringojs.org/api/master/fs/
Related
I have a FreeSwtich solution running on Linux with quite a lot of configuration scripts written in javascript.
The problem is that we need to write and read files; which javascript normally doesn't support.
I tried the SpiderMonkey File Object but it doesn't work and has been marked as obsolete..
The setup is a bit special; there is really only one dial plan with one javascript handling the call initially. Depending on a number of parameters the call is then 'handed over' to one of 20 or so Javascripts dynamically included. It is actually a data base lookup that returns the name of the script to run.
So looking for other options if there are any? Rewriting the entire thing in LUA is an option of course but to keep the current structure that would mean rewriting a lot of javascripts. Unless someone can think of a magic way to call LUA script from javascript?
There's a bunch of various programming languages supported by FreeSWITCH: Perl, Python, Lua, Ruby. Take whatever suits you better :)
You can provide those files via an HTTP interface and fetch them from your Javascript scripts.
You can also fetch them as BLOB objects from your SQL database.
But this whole setup seems like a lot of CPU work for every call, so I wonder if performance is not already an issue.
This question is mainly for security purposes. I need to know if it is possible to view by any means (plugins, programmatically or whatever) a list of all variables and their values in a gwt application compiled to javascript.
Let's say I have a variable x created by gwt in its normal deployment mode.... let's just ignore how did the value get there... Can the user somehow get to know that there is a var called x and its value...
Please note that I am not looking for software engineering best practices, the question is over simplified so that we get to the point. I know that I should not have anything sensitive on the client on the first place... but please let's just skip that since the case is a much bigger story...
Thanks a lot..
Short awnser... yes..
GWT compiles to javascript and obfuscates everything, that said, all information is available from the compiled source if one knows what to look for. If someone succeeds in injecting a simple script tag into your application, they can simple retreive all scripts through XMLHttpRequest and parse them as text. No matter how obfuscated, it's theoretically possible to get what you want from any javascript source. If you can see it in the raw script file, it's attainable, doesn't really matter if it's locked away in anonymous closures or whatnot, any JS security mechanism can be circumvented.
Main condition is to get control of the page (script injection).
To quote yourself: " I know that I should not have anything sensitive on the client on the first place..."
If it's worth hacking, people will try it.
GWT code is compiled to javascript. So ultimately user can use javascript introspection to discover all objects and their properties.
Short answer - No, not unless you know what you are looking for.
GWT compiler does something called as cross-compiling, it transforms java code into java script/ECMA script. The mapping between a variable in java to that in generated script is not straight forward. The language semantics are not the same; the compiler tries to optimize and generates obfuscated JS (to reduce the size). You can tweak this to certain extent by passing arguments at compile time (by setting PRETTY). This still does not guarantee a one on one mapping.
On different quote, even decompiled java code does not look like the original source. ( thats' the complexity of the problem)
There are numerous log files that I have to review daily for my job. Several good parsers already exist for these log files but I have yet to find exactly what I want. Well, who could make something more tailored to you than you, right?
The reason I am using JavaScript (other than the fact that I already know it) is because it's portable (no need to install anything) but at the same time cross-platform accessible. Before I invest too much time in this, is this a terrible method of accomplishing my goal?
The input will be entered into a text file, delimited by [x] and the values will be put into an array to make accessing these values faster than pulling the static content.
Any special formatting (numbers, dates, etc) will be dealt with before putting the value in the array to prevent a function from repeating this step every time it is used.
These logs may contain 100k+ lines which will be a lot for the browser to handle. However, each line doesn't contain a ton of information.
I have written some of it already, but with even 10,000 lines it's starting to run slow and I don't know if it's because I wasn't efficient enough or if this just cannot be effectively done. I'm thinking this is because all the data is in one giant table. I'd probably be better off paginating it, but that is less than desirable.
Question 1: Is there anything I failed to mention that I should consider?
Question 2: Would you recommend a better alternative?
Question 3: (A bit off topic, so feel free to ignore). Instead of copy/pasting the input, I would like to 'open' the log file but as far as I know JavaScript cannot do this (for security reasons). Can this be accomplished with a input="file" without actually having a server to upload to? I don't know how SSJS works, but it appears that I underestimated the limitations of JavaScript.
I understand this is a bit vague, but I'm trying to keep you all from having to read a book to answer my question. Let me know if I should include additional details. Thanks!
I think JavaScript is an "ok" choice for this. Using a scripting language to parse log files for personal use is a perfectly sane decision.
However, I would NOT use a browser for this. Web browsers place limitations on how long a bit of javascript can run, or on how many instructions it is allowed to run, or both. If you exceed these limits, you'll get something like this:
Since you'll be working with a large amount of data, I suspect you're going to hit this sooner or later. This can be avoided by clever use of setTimeout, or potentially with web workers, but that will add complexity to your project. This is probably not what you want.
Be aware that JavaScript can run outside of browsers as well. For instance, Windows comes with the Windows Script Host. This will let you run JavaScript from the command prompt, without needing a browser. You won't get the "Script too long" error. As an added bonus, you will have full access to the file system, and the ability to pass command-line arguments to your code.
Good luck and happy coding!
To answer your top question in bold: No, it is not a terrible idea.
If JS is the only language you know, you want to avoid setting up any dependencies, and you want to stay platform-independent... JavaScript seems like a good fit for your particular case.
As a more general rule, I would never use JS as a language to write a desktop app. Especially not for doing a task like log parsing. There are many other languages which are much better suited to this type of problem, like Python, Scala, VB, etc. I mention Python and Scala because of their script-like behaviour and minimal setup requirements. Python also has very similar syntax to JS so it might be easier to pick up then other languages. VB (or any .NET language) would work too if you have a Visual Studio license because of it's easy to use GUI builder if that suits your needs better.
My suggested approach: use an existing framework. There are hundreds, if not thousands of log parsers out there which handle all sorts of use-cases and different formats of logs that you should be able to find something close to what you need. It may just take a little more effort than Google'ing "Log Parsers" to find one that works. If you can't find one that suits your exact needs and you are willing to spend time making your own, you should use that time instead to contribute to one of the existing ones which are open source. Extending an existing code base should always be considered before trying to re-invent the wheel for the 10th gillion time.
Given your invariants "javascript, cross-platform, browser ui, as fast as possible" I would consider this approach:
Use command line scripts (windows: JScript; linux: ?) to parse log files and store 'clean'/relevant data in a SQLite Database (fall back: any decent scripting language can do this, the ready made/specialized tools may be used too)
Use the SQLite Manager addon to do your data mining with SQL
If (2) gets clumsy - use the SQLite Manager code base to 'make something more tailored'
Considering your comment:
For Windows-only work you can use the VS Express edition to write an app in C#, VB.NET, C++/CLI, F#, or even (kind of) Javascript (Silverlight). If you want to stick to 'classic' Javascript and a browser, write a .HTA application (full access to the local machine) and use ADO data(base) access and try to get the (old) DataGrid/Flexgrid controls (they may be installed already; search the registry).
I'm currently working on a network security project that checks for XSS vulnerabilities on a website, which hopefully can be used for pen-testers out there (in case you don't believe me and think I'm some kinda script kiddy, here's the class website: http://netsec.cs.northwestern.edu/projects/).
So, I'm having trouble detecting JavaScript on a given HTML page. I spent many hours installing PyV8 and V8 and it seems that they can evaluate simple JavaScript statements. However, for more 'complex' JavaScript problems, for example, an alert box, PyV8 does not seem to support it. So, I doubt if I can feed PyV8 some arbitrary JavaScript code and expect it to give me the corresponding JavaScript output.
I did find this JS server/client in DrEval but it doesn't seem to work in the latest revision of V8/PyV8.
Please help! My project is due in about a week from today and no one in the class seems to be able to help me because this is a rather strange problem..
Uhmm.. And I apologize in advance if this question has been answered somewhere else before. I did search for this topic for at least 2 hours..
Thanks in advance for the responses!
A incredibly hacky way would be to look for "text/javascript" in the source of the webpage.
import urllib2
if urllib2.urlopen('http://www.google.co.uk').read().find('text/javascript') == 0:
print "It has js."
This is NOT the best answer I would be happy to hear from someone who does know the correct way to do it.
I'm not really sure what you want to do but here are some thoughts:
If you want to run JavaScript code in the context of a web page, you need a browser or an emulation of one. Try envjs. It needs Java, though, because it needs a JavaScript interpreter.
I'm not aware of a library which offers the same functionality for Python. Maybe you can fix that. envjs is written mostly in JavaScript but it needs some support functions from the interpreter (printing to the console, downloading data, opening files).
XSS vulnerabilities happen if you can inject JavaScript code into a page, no matter what code already exists. So you need to check all fields of a form, post the form to the server and then check that it properly escapes all values on the next page.
PyV8 is Python binding of V8 javascript engine. It can evaluate even the most complex javascript code. It is also being used by Google Chrome.
That being said, there are a few things you need to do to make it work properly. First of all, PyV8 is a javascript engine only. It does not construct the DOM. Thus, you need to create a DOM yourself and run the PyV8 context with it.
Also, you have said that the alert function does not work. The alert(); function is part of the DOM, it is shorthand of window.alert();. So, you have to specify each function and property in DOM.
PyV8 source comes with a simple DOM. You can download it from http://code.google.com/p/pyv8/source/browse/
A lot of questions have been asked and answered about running server-side javascript on Google App Engine, but all of the answers deal with Java instances in order to make use of Java-based JS interpreters like Rhino, Rhino for Webapps, etc.
Is there any way to execute server-side javascript code on a Python GAE instance? I'm thinking something exactly along the lines of pyv8, but with support for App Engine (which I guess would mean a pure python implementation of the interpreter).
The only solution I can come up with at the moment is to use some sort of gross hack to run a Java and Python GAE instance side-by-side (via different versions) so they can both talk to the same datastore, let the Java instance host the JS code, and use an API to talk back'n'forth. Not very appealing.
No need to get into all the "this is unnecessary, you shouldn't be doing this" discussion -- I know this isn't ideal and I'm simply curious if it can be done.
As far as I can find: No
I've done a bit of searching, but it seems that nobody has tried to implement a pure Python Javascript engine, and I can't blame them: it would be a huge amount of work for very few use cases (unfortunately, yours is one of those). A couple of projects—Grailbrowser and Pybrowser—have Python code to render HTML, so might one day aim to run javascript, but it's not even started, and neither of them look in active development.
The most likely way it would ever happen is if Google were to offer the Parrot VM (which can run various dynamic languages) on Appengine. That's a cool idea, but I'm not holding my breath.
What might work is to run Jython (and Rhino) in a Java instance. Of course, then you'd have to get to any App services through the Java API, not the Python one, which would be ugly.
Actually, it can indeed be done, using either AppEngineJs or ESXX:
http://www.appenginejs.org/
http://esxx.blogspot.com/2009/06/esxx-on-google-app-engine.html
I am currently trying to solvevthe same problem with PyJON
http://code.google.com/p/pyjon/
Seems to be a pure Python JavaScrit parser an interpreter.