OpenWrt Forum Archive

Topic: How to execute javascript through terminal

The content of this topic has been archived on 29 Apr 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

Hi to all i need a script that can help me to
-open a website
-submit password
-when it log in do other.

Actually I can do that through computer's browser (as chrome/firefox) with this example command

javascript:document.getElementById("password").value = "password";auth();

Is there a way to do it through openwrt?
Thanks

(Last edited by Squalo on 1 Nov 2017, 00:21)

well that depends on what you all want to do, but curl and wget can log into pages from within shell scripts.  perl can do most anything if you can code in it.

I only want to log in and, when it is logged in, launch other function (in browser is as to click on an icon)

javascript: function();

Anyone have a good idea? I searched more and more over internet but i don't find anything that can help me!

(Last edited by Squalo on 1 Nov 2017, 16:52)

You cannot run Javascript unless you have some sort of a JS runtime. Most browsers come with a built-in engine, but there are also headless JS engines, such as PhantomJS. They are used for unit-testing web pages.

One course of action would be to compile PhantomJS from sources, execute it, and using the runtime, download the login page, manipulate the elements to insert username, password and then call the "auth" function. After the login request has succeeded, you proceed to do whatever it is you need to do.

EDIT: Note that this is by no means a simple ordeal. PhantomJS has a lot of build requirements, and uses python as a build environment. Expect a lot of work.

(Last edited by Antek on 1 Nov 2017, 18:11)

Yes you are right! But how i can do that? Your solution is very difficult to do! There aren't any way to do that? Bash script or other things?
WWTK says that it possible to write a similar solution and execute it on openwrt with perl, it's right? How? thanks

(Last edited by Squalo on 1 Nov 2017, 20:22)

In most cases, you do not need to execute any JS to emulate a login page. Do not focus on the JS, focus on the content that gets sent to the server. Most probably, all what that JS does is to prepare some variables and launch a POST against the server. Use the debugging capabilities of your browser, intercept what is sent to the server, and you will have most of the work done.

Antek wrote:

Maybe the WWW library for Perl (LWP) will help you? http://search.cpan.org/dist/libwww-perl/lib/LWP.pm

I think this is the best solution ... now i'm trying to create the script!

<form method="post" action="index.lp" name="authform" id="authform">
<input type="password" name="password" id="password" onkeypress="return enter_submit(event);" />
<input name="enter" class="btn btn-primary" type="button" value="log in" onclick='auth()' />

Now i'm testing it on computer and have some problem:
1) Perl need a c compiler...after i can install it on openwrt?
2) I can try 2 different ways,
The first and more rapid is to use WWW::Scripter::Plugin::JavaScript but i cannot install the module because i cannot install mingw (i did, in ppm-shell, install mingw and it return "ppm install failed: Can't find any package that provides mingw"). Furthermore i don't see WWW::Scipter in https://lede-project.org/packages/index … ges---perl
I wrote that script (can it work?):

  use WWW::Scripter;
  $w = new WWW::Scripter;
  
  $w->use_plugin('JavaScript');
  $w->get('http://url');
  $w->get('javascript:document.getElementById("password").value = "password";auth();');
  sleep (1);
  $w->get('http://url');
  $w->get('javascript: function();');

The second is to use WWW:Mechanize. How i can see output page? And how i can send command to the second page? Now with that script i get this error: "Missing base argument at D:/Program Files/Perl/lib/HTTP/Response.pm line 92."
(can it work?)

    use WWW::Mechanize;
    my $mech = WWW::Mechanize->new();

    $mech->get( $url );

    $mech->follow_link( url => 'http://url' );

    $mech->submit_form(
        form_name => 'authform',
        fields    => { password  => 'password', },
        button    => 'log in'
    );

eduperez wrote:

In most cases, you do not need to execute any JS to emulate a login page. Do not focus on the JS, focus on the content that gets sent to the server. Most probably, all what that JS does is to prepare some variables and launch a POST against the server. Use the debugging capabilities of your browser, intercept what is sent to the server, and you will have most of the work done.

I've already read the http header but it use some md5 (i think) and i don't know how to solve! I think also that with script i can solve quickly...

(Last edited by Squalo on 3 Nov 2017, 12:47)

The discussion might have continued from here.