0

I want to make a button in NetBeans, that when clicked, will open a webpage, wait until the webpage loads, and logs in to that site. I have some code here, but I am not sure what to do after this:

javascript:(function (){
    window.open("http://www.stackoverflow.com");
    object.onload="LoginToThisSite"
})();

I also have a bookmarklet that logs you in to the site automatically, I am just not sure how to tie that in to the button.

Here is the code I have in NetBeans for the button:

public void actionPerformed(ActionEvent evt) {
    String cmd = evt.getActionCommand();
    if (cmd == "POST") {
        try {
            String url = "http://www.stackoverflow.com";
            java.awt.Desktop.getDesktop().browse(java.net.URI.create(url));
        }
        catch (java.io.IOException e) {
            System.out.println("Couldn't open browser...");
        }
    }
}

I would appreciate all the help I can get, as I am horrible at coding in Java.

Codeman
  • 12,157
  • 10
  • 53
  • 91
Andrew
  • 63
  • 7
  • Perhaps this should be tagged as Java. You may get a better response. – Micah Henning Aug 13 '12 at 21:12
  • it sounds like you want to run javascript from a site you host that can login to a site hosted by someone else? you won't be able to do this. – DaveAlger Aug 13 '12 at 21:13
  • @DaveAlger My intention is for something along the lines of Facebook. I actually want to use this for a school website so I can automatically log in and post a discussion. – Andrew Aug 13 '12 at 23:05
  • then the only sure way to do this is in the `$(document).ready` function handler of the school website itself. the best you can probably do is probably add your code to an arbitrary delay like this... `setTimeout(function(){alert("now that i have waited 30 seconds i will assume the page is loaded and ready")},30000);` – DaveAlger Aug 15 '12 at 14:02

2 Answers2

1

Use $(document).ready()

This function would excute after document is loaded

(Jquery)

Venkat Reddy
  • 1,046
  • 1
  • 8
  • 13
  • :O I don't know how to use Jquery. Could you write the code for me? – Andrew Aug 13 '12 at 23:05
  • A caveat should be added; document.ready runs when the document is loaded and the page is ready. This is different from page load. It is however, the more appropriate way to handle the problem. – Codeman Aug 13 '12 at 23:06
  • @Andrew do a Google search for document.ready() and the answers appear :) http://www.learningjquery.com/2006/09/introducing-document-ready you need to make sure you are including your jQuery plugin in the header – Codeman Aug 13 '12 at 23:06
  • @Andrew some more good reading on when and why to use document.ready() http://stackoverflow.com/questions/6005789/when-do-you-need-to-use-document-ready – Codeman Aug 13 '12 at 23:08
  • @Pheonixblade9 Can I put the bookmarklet in the document.ready() function? I am not sure if that will work out, as the bookmarklet is a spew of compressed javascript... – Andrew Aug 13 '12 at 23:49
  • if it's valid javascript, I see no reason why you couldn't! I'm not too familiar with bookmarklets. Best way to find out is to try! – Codeman Aug 13 '12 at 23:50
  • @Pheonixblade9 So all I need to do is download JQuery, add it to my libraries, and import it? – Andrew Aug 13 '12 at 23:51
  • @Andrew this is the proper way to do it for production code. If you're just testing, it's fine to reference it from the Google libraries for now. That is not recommended for long term projects, though, as it may present a security risk. – Codeman Aug 13 '12 at 23:52
  • @Pheonixblade9 Where can I find JQuery? – Andrew Aug 14 '12 at 00:01
  • @Andrew and please mark the question as answered if it answered your question :) – Codeman Aug 14 '12 at 00:04
0

since you don't have access to the website you are trying to authenticate with the best you can do is to wait an arbitrary length of time (in this case 30000 milliseconds):

<body onload="setTimeout(authenticationAttempt,30000);">
    <script>
        function authenticationAttempt() {
            // add your code to login here
            alert('after 30 seconds i assume the page is loaded');
        }
    </script>
</body>

here is a jsfiddle example which allows 5 seconds for bing.com to load before running additional code.

http://jsfiddle.net/DaveAlger/CFLh2/

if the website you are trying to authenticate with is public you should post a link to it because i still think what you are attempting will not be allowed unless you have access to change the code on the school website.

DaveAlger
  • 2,376
  • 25
  • 24