1

We have a feature in a Windows application that opens a web browser, navigates to certain pre-configured web sites, and auto-fills the forms with data from our database -- it's a convenience feature for users.

Now we want to build an Asp.Net version of this feature so that from our web app, the user clicks a link/button, and we open the page (with a redirect? with JavaScript?). Then we fill in the form so that the user can review the data and submit it.

To do this, I think that we would either need to inject javascript into the browser frame that is loading the external window, or we would need to be able to interact with that window.

We are worried that browser security might not allow this -- it could look like some sort of spoofing attack. What would be a good way to do this?

JMarsch
  • 21,484
  • 15
  • 77
  • 125
  • Are you in control of the page you are trying to fill? – FJT Aug 20 '12 at 20:27
  • Could you rephrase the second paragraph? I think your missing a couple of words there and i'm hard at understanding the situation due to this – jbkkd Aug 20 '12 at 20:27
  • Asp.net MVC and their `for` controls will populate form data based on the page model. Google some MVC tutorials. – Jack Aug 20 '12 at 20:31
  • take a look at this post http://stackoverflow.com/questions/122614/is-there-a-net-equivalent-of-perls-lwp-wwwmechanize – isJustMe Aug 20 '12 at 20:31
  • If your web app will be an asp.net mvc project, you can populate a view model with the data for the forms and pass that model to the view. Should be fairly easy. – Forty-Two Aug 20 '12 at 20:32
  • @FionaT The page that we want to fill is hosted on a different web site that has no affiliation with us. It is for querying information -- we just want to pre-fill the form to make it easier for the user to complete the query. – JMarsch Aug 20 '12 at 21:44

4 Answers4

2

If the Domains, Protocols, and Ports do not match between the website opening the popup and the website being opened in the popup then your code will violate the Same Origin Policy and either be silently ignored or throw an exception.

Here's an example to demonstrate:

<!doctype html>
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<script type="text/javascript">
    openPopUp = function(href) {
        var props = "width=500,height=500,menubar=no,toolbar=no,scrollbars=yes";
        var win = window.open(href, href, props);
        if (win == null) alert("Your popup blocker is ruining my demo.");
        return win;
    };

    openMe = function(url){
        href = (url=="")?document.location:url;

        pu = openPopUp(href);

        //This will be ignored silently if Same Origin Policy is violated
        pu.onload = function() { 
            p = pu.document.createElement("p");
            p.appendChild(pu.document.createTextNode("onload was here."));
            pu.document.body.appendChild(p);
        };

        //This will throw an exception if Same Origin Policy is violated
        setTimeout(function() { 
            p = pu.document.createElement("p");
            p.appendChild(pu.document.createTextNode("setTimeout was here."));
            pu.document.body.appendChild(p);
        },3000);

        return false;
    }
</script>
<body>
<a href="" onclick="return openMe(this.href);">Self</a>
<a href="https://www.google.com/" onclick="return openMe(this.href);">Google</a>
MyItchyChin
  • 13,733
  • 1
  • 24
  • 44
0

You need to make a bookmarklet. From you asp.net page, make a link which the user drags to his browsers bookmarks. The user clicks a link to the page you want to fill out. Then he clicks the newly created bookmarklet.

In pure javascript this is not possible which is why you need to use bookmarklet.

Esben Skov Pedersen
  • 4,437
  • 2
  • 32
  • 46
0

Scenario: you have two pages base.aspx and popup.aspx. On clicking a link on base.aspx you want to open popup.aspx filled with desired data. You are in control of both base.aspx and popup.aspx

These are few ways I think you can achieve this

  1. If the data you want on popup.aspx is already loaded on base.aspx. Then send this data in querystring (if its not too long)
  2. If the data you want on popup.aspx is already loaded on base.aspx. Read form fields from base.aspx using window.opener in popup.aspx. Note: window.opener will only work if both base and popup are in same domain.
  3. Send an identifier string in popup.aspx URL (http:///popup.aspx?actiodId=1234QWE6789), now in page_load of popup.aspx fetch data from DB quering on actiodId
user961954
  • 3,214
  • 2
  • 17
  • 24
  • The problem is that the page I want to prefill does not belong to me -- I can't make any changes to it. – JMarsch Aug 20 '12 at 21:46
0

The best way to do what you are trying to do is to create a browser plugin. Try looking here for some more information. But with a browser plugin, you get complete control of the DOM and you can inject javascript code to do what you are doing. If needed, you can even call web services from your plugin.

Icemanind
  • 47,519
  • 50
  • 171
  • 296