简体   繁体   中英

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 :)

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);"

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.

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 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM