2

I have a JavaScript script in procedure.js

When I'm on my navigator (Google Chrome) I can open the developer console then copy paste my script to execute it on the web page I'm looking at.

But now I'm trying to do it automatically with Java (or Linux shell ?).

I want that my Java program open the webpage, then execute the procedure.js on this webpage.

How can I do that? Thanks for your help

Tony Hinkle
  • 4,706
  • 7
  • 23
  • 35
M. Ozn
  • 1,018
  • 1
  • 19
  • 42
  • Don't you have the script included with a – Cobus Kruger Jun 14 '16 at 12:56
  • My script isn't internal to the website, i do not own this website, basically I just want to use javascript to automatically click on multiple buttons which appear on this website – M. Ozn Jun 14 '16 at 13:03
  • Sounds like you're trying to automate the process of getting SO cred! Seriously, this should be doable if you're using a WebView component in your Java program. Is that an option? – Cobus Kruger Jun 14 '16 at 13:13
  • No I'm trying to automate birthdays messages on Facebook lol, I connect to my today_birthday page manually then run my script to automatically wish a happy birthday ! My script just write the message on available textbox then it click on the post button. With a webview I'll be able to connect and run JavaScript ? It seems to be the solution I'm looking for :) – M. Ozn Jun 14 '16 at 13:18

3 Answers3

2

Use a headless browser like phantom JS as opposed to java.

You then then include your script using include.js:

Example below:

var page;
page = require('webpage').create();

page.onLoadFinished = function(status) {
    if (status === 'success') {
        page.includeJs('http://some/js/file.js', function() {
            doSomething();
            phantom.exit();
        });
    } else {
        console.log('Connection failed.');
        phantom.exit();
    }
};

page.open("http://url/of/website");

To run, save as a .js file and then execute phantom name-of-js-file.js

William Greenly
  • 3,914
  • 20
  • 18
  • Thanks for your answer, your solution seems to be a better way, I think i'll do like that ! There is anyway to store cookies ? – M. Ozn Jun 15 '16 at 07:13
1

Write a Java application that loads the page in a WebView and use WebEngine.executeScript() to execute your JavaScript.

There is a very nice description of how to do this on this question: Execute a Javascript function for a WebView from a JavaFX program

Community
  • 1
  • 1
Cobus Kruger
  • 8,338
  • 3
  • 61
  • 106
  • Thanks for your answer it's what i was looking for ! Last question, actually when I go to the website, It automatically connect me with cookies saved on my web navigator, but I think that if i use a Webview, it will works like I connect from a new web navigator, so without my cookies no ? So i'll have to implement a js script to connect to the site, then go to the webpage I want, and finally run my initial script no ? Or there is a way to store cookies and pass it to my webview ? – M. Ozn Jun 14 '16 at 13:36
  • Nope, the site will see the WebView as a new connection and will identify it using new cookies. That's similar to signing into Facebook from two different computers and you should be able to remain signed in on both.. – Cobus Kruger Jun 14 '16 at 13:42
1

If the goal is to perform actions in your Facebook account, the correct way is to use the Facebook API instead.

The official Facebook Developers site is where you'll:

  1. Register your app.
  2. Find all the documentation you need.

The major benefits of this approach are:

  1. The API is officially supported.
  2. The API is much more likely to keep working over the long run. Facebook does change their site every now and again.
  3. You can put it on your resumé.
Cobus Kruger
  • 8,338
  • 3
  • 61
  • 106
  • My real problem is the duration of my script, because Facebook often change is way of working. But with Java, I can run my script from my own server and automate it. I think I need to use special IDE and language to develop my own API no ? – M. Ozn Jun 14 '16 at 14:29
  • Er, no. You can create a client for the Facebook API. This client can run from your desktop, server, or anywhere else. This is what is used by the Facebook apps for Android & iOS and also several other social networking apps. – Cobus Kruger Jun 14 '16 at 14:32