view contents of large array? - javascript

I have a very large array I need to check for debugging purposes, problem is it crashes firebug and the like if I try to view the data.
Is there a way I can dump the array to a text file or something?

Why not just dump it on the document itself? If you are using Firefox, try the following:
document.write(myBigArray.toSource());
Then copy paste like your usually do on normal website.
p/s: toSource() requires browser that supports Javascript 1.3 and above

Opera has scrollable alerts, it's very useful for developing.
EDIT: Tested with success for messages with 500000 lines. You can also copy from it.

Post the array to the server (json/hidden field normal form post), and use your server-side language to save that array dump to a file.

If you are using IE you could try copying a string representation of the array to the clipboard.

There are some libraries that can help in writing to files. You can use ActiveX, but that binds you to internet explorer for your debugging and that's kind of outside the javascript world.

Related

Use Chrome Dev Tools copy from Script

I am creating some helpers scripts in my web app, which I run from the console, to output some information about the current state (e.g. JSON.stringify of objects). For example, I define the temporal window.Foo method, I open the Chrome Console, and run Foo(). It would be great if I could use the Console method copy, which copies some value to the clipboard.
I've checked that if I define a function that uses copy inside the Chrome console, it works, but as expected, if I define it in a regular JavaScript file, it doesn't. Do you have any clue how can I access this method through an API or something (if it is even possible)? Any possible workaround to copy info from a script to the clipboard without having to type copy?
The fastest solution is to wrap the script with copy (e.g. copy(Foo())), but that means extra typing... I was wondering if it could be done faster.
Thanks
I'm not sure, if you mean exactly this, but maybe it could helps you- look in the sources tab :) There you can see all loaded scripts (and also make breakpoints, etc.)

Are there any javascript libraries that can render terminal output in a browser?

I've got a text file that contains terminal output which includes all kinds of character codes such as moving the cursor around, etc. How can I render this properly in a browser?
There are sevral options that I've found based on terminal emulation using Javascript:
jQuery Terminal plugin
GateOne
http://cb.vu/
shellinabox
The first option seems to be the closest solution to what you need.
https://github.com/drudru/ansi_up probably is what you need, it will render any termial output to html
I used term.js, bone.io, expres.io for terminal emulation. Its working pretty well.
https://github.com/PrimeEuler/ShellServer.js
https://github.com/chjj/tty.js
https://github.com/thlorenz/hypernal Renders terminal output as html to simplify reusing server side modules in the browser.

Send Javascript code to browser

Is there a way to send javascript commands to an open web running in a browser from the shell?
Let's say I have stackoverflow.com open with Chrome. Well, I'd like to send something like
alert('hi!');
from the shell, with something similar to the following:
$ send -t Chrome -w "stackoverflow.com" -c "alert('hi!')"
I was wondering this, because if I can write alert('hi!') on the javascript console of Chrome, I should be able to do the same with a call somewhere, right?
I've seen node.js but I think is not possible with that, please, let me know if I'm wrong.
I know the question could seem weird but I'm curious, thanks in advance :)
You can send JavaScript to Firefox through the jssh extension.
http://www.croczilla.com/bits_and_pieces/jssh/
This is what the Watir testing framework uses to automate Firefox.
I don't know of an equivalent for Chrome.
For IE seems like you can use good old VBScript: http://www.autohotkey.com/forum/topic7642.html
Worked for me just fine now with IE8. :)
Edit: to open this very question and alert JS value have this code as .vbs file and run it:
Dim oIE
Set oIE = CreateObject("InternetExplorer.Application")
oIE.Visible = 1
oIE.Navigate "http://stackoverflow.com/questions/4992552/send-javascript-code-to-browser/4992812"
Do While (oIE.Busy)
Wscript.Sleep 10
Loop
oIE.Navigate "javascript:alert(fkey);"
I think it is possible if you write and install some browser addon that would receive your signal, and do the job. Interesting question, ill be tracking it.
It is entirely up to the browser to provide this sort of functionality. In fact, the form of your problem isn't specific even to browsers:
I was wondering this, because if I can [provide some sort of input] to [a program] to make it [do something], I should be able to do the same with a call somewhere, right?
Some programs do indeed provide hooks for scripting, but if they don't, you're out of luck. There is certainly no guarantee that if you can do something via the GUI, then you can trigger the same action from a command line call.
Now the fact that most browsers provide some sort of plugin architecture, makes it much more likely that such a plugin exists that will listen for external input in this way, if this functionality is missing in the base product.
However, this is still going to be very specific to the particular model and even version of the browser you want to control - so if it's something you wanted to release into the wild, you'll need to be very specific with your requirements.
You can send arbitrary code to browser through the address bar! For example in AutoHotKey style,
send F6 //focus the address bar
send ctrl+v //given that your code is in clipboard
send enter
Now there is another question. Can we get the return value of the inject code? The answer is YES!
Assign your return value to document.title, and retrieve the window title from your application.
If the return value is too long (eg. a JSON format string), do the trick that
document.title='calculating...';
document.title=returnValue.subString(0,20);
sleep(10);
document.title=returnValue.subString(20,40);
sleep(10);
document.title=returnValue.subString(40,60);
...
document.title='finished';
Hope it works.

a simple problem about chrome extension. : )

I am a beginner with chrome extension.
In my content_script.js, how can a get an array in Javascript from background.html?
I did it like this: (assuming you have an array foo in background.html)
chrome.extension.getBackgroundPage().foo
But apparently, that only works for things like popup.html, etc. To get data from your background page in a content_script.js file, you need to use:
chrome.extension.connect()
The API docs are here.
you have to use message passing which I am finding a pain to use, but anyway details here
http://code.google.com/chrome/extensions/messaging.html#connect

How can I execute javascript in Bash?

I try to get to a page straight from Bash at http://www.ocwconsortium.org/. The page appears when you write mathematics to the field at the top right corner. I tested
open http://www.ocwconsortium.org/#mathematics
but it leads to the main page. It is clearly some javascript thing. How can I get the results straight from Bash on the first page?
[Clarification]
Let's take an example. I have the following lines for a Math search engine in .bashrc:
alias mathundergradsearch='/Users/user/bin/mathundergraduate'
Things in a separate file:
#!/bin/sh
q=$1
w=$2
e=$3
r=$4
t=$5
open "http://www.google.com/cse?cx=007883453237583604479%3A1qd7hky6khe&ie=UTF-8&q=$q+$w+$e+$r+$t&hl=en"
Now, I want something similar to the example. The difference is that the other site contains javascript or something that does not allow me to see the parameters. How could I know where to put the search parameters as I cannot see the details?
open "http://www.ocwconsortium.org/index.php?q=mathematics&option=com_coursefinder&uss=1&l=&s=&Itemid=166&b.x=0&b.y=0&b=search"
You need quotes because the URL contains characters the shell considers to be special.
The Links web browser more or less runs from the commandline (like lynx) and supports basic javascript.
Even though the title of the post sounds general, your question is very specific. It's unclear to me what you're trying to achieve in the end. Clearly you can access sites that rely heavily on javascript (else you wouldn't be able to post your question here), so I'm sure that you can open the mentioned site in a normal browser.
If you just want to execute javascript from the commandline (as the title suggests), it's easy if you're running bash via cygwin. You just call cscript.exe and provide a .js scriptname of what you wish to execute.
I didn't get anything handled by JavaScript - it just took me to
http://www.ocwconsortium.org/index.php?q=mathematics&option=com_coursefinder&uss=1&l=&s=&Itemid=166&b.x=0&b.y=0&b=search
Replacing mathematics (right after q=) should work. You may be able to strip out some of that query string, but I tried a couple of things and and it didn't play nice.
Don't forget to encode your query for URLs.
You will need to parse the response, find the URL that is being opened via JavaScript and then open that URL.
Check this out: http://www.phantomjs.org/.
PhantomJS it's a CLI tool that runs a real, fully-fledged Browser without the Chrome.

Categories

Resources