www.teamdev.com

This Question is Possibly Answered

1 "correct" answer available (4 pts) 2 "helpful" answers available (2 pts)
9 Replies Last post: Aug 7, 2008 4:17 PM by Victor Gubin  
Click to view Inbar's profile   13 posts since
Jun 24, 2008

Jul 22, 2008 6:30 PM

Capture submit of a form displayed in JxBrowser

Hi,
How do I capture submit of a form displayed in JxBrowser.
To be specific:
I setContent of my browser to display a form.
I'd like to capture user submit action & block it (providing my own customized behavior on submit).

Thanks,

Click to view Victor Gubin's profile TeamDev Ltd. 83 posts since
Dec 17, 2007
1. Jul 22, 2008 8:17 PM in response to: Inbar
Re: Capture submit of a form displayed in JxBrowser
Use Java Script for this. You can do this and from your java code by using the WebBrowser.evaluateScript function.

Or you can use the DOM model of page.
Click to view Victor Gubin's profile TeamDev Ltd. 83 posts since
Dec 17, 2007
3. Jul 23, 2008 4:51 PM in response to: Inbar
Re: Capture submit of a form displayed in JxBrowser

In fact the submit call just jump your browser to reconvene URL. You can trace this in RequestListener.

I did not offer to invoke the submit yourself e.g document.forms[0].submit() , you can simply change the java script code of your page ( <form onSubmit=""> )

Click to view Victor Gubin's profile TeamDev Ltd. 83 posts since
Dec 17, 2007
5. Jul 24, 2008 2:10 PM in response to: Inbar
Re: Capture submit of a form displayed in JxBrowser
In such a case if the HTML code is not yours why do you use the setContent ?

Better one sample

  
import java.awt.BorderLayout;
import java.util.concurrent.atomic.AtomicBoolean;
 
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
 
import com.teamdev.jxbrowser.WebBrowser;
import com.teamdev.jxbrowser.WebBrowserFactory;
import com.teamdev.jxbrowser.event.LocationEvent;
import com.teamdev.jxbrowser.event.RequestAdapter;
import com.teamdev.jxbrowser.event.RequestListener;
import com.teamdev.jxbrowser.event.StateEvent;
import com.teamdev.xpcom.Xpcom;
 
 
public class SubmitDemo extends JFrame {
 
    private static final String THE_URL = "http://www.google.com";
 
    final AtomicBoolean mutex = new AtomicBoolean(false); 
 
    RequestListener requestListener = new RequestAdapter() {
 
        @Override
        public void locationChanged(final LocationEvent e) {
            final String curURL = e.getRequestUrlName();
                 if(!curURL.equals("about:blank") && ! (curURL.startsWith(THE_URL) && curURL.length() < 25) /* if com.ua, com.in com.de etc */ )  {    
 
 
                // now is submit invoked in google search form in this demo 
                System.out.println("The submit URL is:" + curURL);
 
 
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        e.getWebBrowser().stop();
                    }
                });
 
 
 
            }
        }
 
        @Override
        public void stateChanged(StateEvent e) {
            if (!e.isLoadingDocument() && e.isNetwork()) {
                synchronized (mutex) {
                    mutex.set(true);
                    mutex.notifyAll();
                }
            }    
        }
    };
 
    void myMain() throws Throwable {
        final WebBrowser browser = WebBrowserFactory.getInstance()
                .createBrowser();
 
        setLayout(new BorderLayout());
        add(browser.getComponent(), BorderLayout.CENTER);
 
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        setSize(640, 480);
 
        browser.addRequestListener(requestListener);
 
        browser.navigate(THE_URL);
 
        // wait wile loading finish
        synchronized (mutex) {
            if(!mutex.get()) {
                mutex.wait(1000);
            }
        }
        mutex.set(false);
 
    }
 
    public static void main(String[] args) {
        Xpcom.initialize(Xpcom.AWT);
        SwingUtilities.invokeLater(new Runnable() {
 
            public void run() {
                SubmitDemo demo = new SubmitDemo();
                try {
                    demo.myMain();
                } catch (Throwable e) {
                    e.printStackTrace();
                }
            }
        });
 
    }
}
 


The interception could be done and in stateChanged.
Click to view Victor Gubin's profile TeamDev Ltd. 83 posts since
Dec 17, 2007
6. Jul 24, 2008 3:13 PM in response to: Victor Gubin
Re: Capture submit of a form displayed in JxBrowser
And one another solution



 
 import java.awt.BorderLayout;
 
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
 
import org.mozilla.interfaces.nsIDOMElement;
import org.mozilla.interfaces.nsIDOMEvent;
import org.mozilla.interfaces.nsIDOMEventListener;
import org.mozilla.interfaces.nsIDOMEventTarget;
import org.mozilla.interfaces.nsIDOMWindow;
import org.mozilla.interfaces.nsIDOMWindow2;
import org.mozilla.interfaces.nsISupports;
import org.mozilla.interfaces.nsIWebBrowser;
import org.mozilla.interfaces.nsIWebBrowserFocus;
import org.mozilla.interfaces.nsIWebNavigation;
import org.mozilla.xpcom.Mozilla;
 
import com.teamdev.jxbrowser.WebBrowser;
import com.teamdev.jxbrowser.WebBrowserFactory;
import com.teamdev.jxbrowser.mozilla.MozillaWebBrowser;
import com.teamdev.xpcom.Xpcom;
 
public class ButtonClickDemo extends JFrame {
 
    private static final String CONTENT = "<html>"
            + "<head><title>Button Click Demo</title></head>" 
            + "    <body>"
            + "       <h1>Button Click demo</h1>" 
            + "          <form id=f_id>"
            + "            <input id=btn_id name=Submit value=Submit type=\"submit\"/>"
            + "       </form>"
            + "    </body>" 
            + "</html>";
 
    private static final String CONTENT_1 = "<h1>You was click on submit button</h1>";
 
    private void addClickListener(final WebBrowser webBrowser) {
        final nsIWebBrowser browser = ((MozillaWebBrowser) webBrowser)
                .getWebBrowser();
 
        Xpcom.invokeLater(new Runnable() {
 
            public void run() {
                final nsIDOMWindow domWindow = browser.getContentDOMWindow();
                final nsIDOMWindow2 domWindow2 = (nsIDOMWindow2) domWindow
                        .queryInterface(nsIDOMWindow2.NS_IDOMWINDOW2_IID);
                nsIDOMEventTarget domEventTarget = domWindow2.getWindowRoot();
 
                domEventTarget.addEventListener("click",
                        new nsIDOMEventListener() {
 
                            public void handleEvent(nsIDOMEvent event) {
                                nsIWebBrowserFocus focus = (nsIWebBrowserFocus) browser
                                        .queryInterface(nsIWebBrowserFocus.NS_IWEBBROWSERFOCUS_IID);
                                nsIDOMElement element = focus
                                        .getFocusedElement();
                                if (element.getAttribute("type").equals(
                                        "submit")) {
                                    nsIWebNavigation nav = (nsIWebNavigation) browser
                                            .queryInterface(nsIWebNavigation.NS_IWEBNAVIGATION_IID);
                                    nav.stop(nsIWebNavigation.STOP_ALL);
                                    SwingUtilities.invokeLater(new Runnable() {
 
                                        public void run() {
                                            webBrowser.setContent(CONTENT_1,
                                                    "text/html");
                                        }
 
                                    });
                                }
                            }
 
                            public nsISupports queryInterface(String uuid) {
                                return Mozilla.queryInterface(this, uuid);
                            }
 
                        }, true);
            }
 
        });
    }
 
    void myMain() throws Throwable {
        final WebBrowser browser = WebBrowserFactory.getInstance()
                .createBrowser();
 
        setLayout(new BorderLayout());
        add(browser.getComponent(), BorderLayout.CENTER);
 
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        setSize(640, 480);
        addClickListener(browser);
        browser.setContent(CONTENT, "text/html");
    }
 
    public static void main(String[] args) {
        Xpcom.initialize(Xpcom.AWT);
        SwingUtilities.invokeLater(new Runnable() {
 
            public void run() {
                ButtonClickDemo demo = new ButtonClickDemo();
                try {
                    demo.myMain();
                } catch (Throwable e) {
                    e.printStackTrace();
                }
            }
        });
 
    }
}
 
 
Click to view Victor Gubin's profile TeamDev Ltd. 83 posts since
Dec 17, 2007
8. Aug 7, 2008 3:30 PM in response to: Inbar
Re: Capture submit of a form displayed in JxBrowser
Hello Inbar, sorry for the delay in response.(I have got a vacation at last week)

You can get the hold URL of submit if you'll use RequestListener as two posts above. Also you can get information from a DOM Tree your page. There are many cases.

Look at this branch of forum http://support.teamdev.com/message/7154#7154.
I think this should somewhat clarify the situation.

May be are much easier for you to implement your own protocol handler then look for the workarounds.
Here are links that you will needed in that case:
http://developer.mozilla.org/en/docs/Web-based_protocol_handlers
http://www.xulplanet.com/references/xpcomref/ifaces/nsIURI.html
http://www.xulplanet.com/references/xpcomref/ifaces/nsIChannel.html
Click to view Victor Gubin's profile TeamDev Ltd. 83 posts since
Dec 17, 2007
9. Aug 7, 2008 4:17 PM in response to: Victor Gubin
Re: Capture submit of a form displayed in JxBrowser
Maybe you will come and completely different solution.
At the moment you try to use changes in the browser so that it can work with your server with own protocol.
Maybe it will be easier for integrate your server with a web server such as Apache. Ie you need to create a TCP-relay web module for the Apache for example you can take already finished implementing such as Interactive TCP Relay 1.0
This approach will allow you to not implement the http protocol in your server and you does not need to modify the browser.