1

For my company, I would like to map values from a form that are stored in the db to a form of a third party company (part of our daily job).

Our form is on site1.com, the form of the third party company is on site2.com, and displayed in an iframe on site1.com.

I have full control over site1.com and none over site2.com

Obviously with jQuery, mapping will not work because of the Same-origin policy.

I thought, since I can manipulate the DOM-tree manually with "inspect element", a browser plugin may work. But till now I haven't been able to get this to work either as I am running into the same-origin policy again.

Is there any other way to get this to work? Do I need to take it one level further and create a cocoa-app for my Mac?

xfscrypt
  • 16
  • 5
  • 28
  • 59
  • What do you mean by "map values from a form to another form"? Are you going to manually fill the first form, then you want the values to be copied to the second form? – abl Jan 30 '15 at 19:36
  • exactly, except that the form on site1.com are filled in by users, not by me. when I view the form after it has been submitted, these details need then to be copied to another form on site2.com – xfscrypt Jan 31 '15 at 05:10

2 Answers2

6

You can use user scripts to achieve your goal. I made an examle siteone.com - a page that has input, button, iframe(looks at sitetwo.com). On a button click a value of the input from siteone.com shows in input from sitetwo.com. To install userscript you may use browser extensions Tampermonkey for Chrome and Safari, or Greasemonkey for Firefox. For communication between page and iframe I used Window.postMessage look documentation. If you need javascript libraries, you can use @require meta block link.

siteone.com index.html

<html>
<head>
</head>
<body>
    <input id="parent-input" type="text"/>
    <button id="send-data-to-iframe">Send data to iframe</button>
    <br /><br />
    <iframe id="my-iframe" style="border:1px solid red; height:50px; width:200px;" src="http://sitetwo.com:8080">
    </iframe>
    <script>
        var win = document.getElementById("my-iframe").contentWindow;

        document.getElementById("send-data-to-iframe").onclick = function(){
            win.postMessage(
                document.getElementById("parent-input").value,
                "http://sitetwo.com:8080" // target domain
            );
            return false;
        }
    </script>
</body>

sitetwo.com index.html

<html>
<head>
</head>
<body>
    <input id="iframe-input" type="text"/>
</body>

myUserJS.user.js

// ==UserScript==
// @name myUserJS
// @license MIT
// @version 1.0
// @include http://sitetwo.com*
// ==/UserScript==
(function (window, undefined) {
    var w;
    if (typeof unsafeWindow != undefined) {
        w = unsafeWindow
    } else {
        w = window;
    }
    // extended check, sometimes @include section is ignored
    if (/http:\/\/sitetwo.com/.test(w.location.href)) {
        function listener(event){
            document.getElementById("iframe-input").value = event.origin + " send: " + event.data;
        }
        if (w.addEventListener){
            w.addEventListener("message", listener,false);
        } else {
            w.attachEvent("onmessage", listener);
        }
    }
})(window);
Dmitriy Krupin
  • 591
  • 3
  • 6
  • 1
    yes! that's it! thanks a lot! one change I had to make is changing **undefined** to **"undefined"**. for those who want to include jquery add: **// @require http://code.jquery.com/jquery-latest.js**. what I am not sure of is whether a browser extension would also work. – xfscrypt Feb 04 '15 at 05:10
0

Have you ever tried "window.postmessage" and "window.onmessage" function. It works in chrome, firefox and IE8+.

jilykate
  • 5,430
  • 2
  • 17
  • 26