-iom-

Library which bridges the crossdomain execution of javascript

posted: 25 Apr 2012

Library to help bridge the communication gap between domains. Execute crossdomain javascript functions from within your sandbox. While you are at it, execute crossdomain and cross user-agent as well. ;)

Setup

Download git clone git@github.com:IOMediums/cmd-proxy.git github

Dependent files Make sure the following files and folder remain together:

domain1

Include the following snippet into your source html code. Preferably before the closing </head> tag.

 1 <script type="text/javascript"> 
 2     var _c = { path:'', pub:'domain1', sub:'domain2' };
 3     // DO NOT MODIFY BELOW THIS LINE
 4     var cbridge, PROXYCMD;
 5     (function() {
 6         var prxy = document.createElement('script'); prxy.type = "text/javascript"; prxy.async = true;
 7         prxy.src = _c.path+"/cmdproxy.js";
 8         var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(prxy, s);
 9     })();
10 </script>

Expose function/s to be called.

1 <script type="text/javascript"> 
2     var hello = function(msg) {
3         document.getElementsByTagName('body').appendChild(document.createTextNode(msg));
4     };
5 </script>

domain2

Same steps taken as in domain1.

 1 <script type="text/javascript"> 
 2     var _c = { path:'', pub:'domain2', sub:'domain1' };
 3     // DO NOT MODIFY BELOW THIS LINE
 4     var cbridge, PROXYCMD;
 5     (function() {
 6         var prxy = document.createElement('script'); prxy.type = "text/javascript"; prxy.async = true;
 7         prxy.src = _c.path+"/cmdproxy.js";
 8         var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(prxy, s);
 9     })();
10 </script>

Expose function/s

1 <script type="text/javascript"> 
2     var sayHi = function() {
3         document.getElementsByTagName('body').appendChild(document.createTextNode('OH HAI!'));
4     };
5 </script>

You noticed everything is pretty much the same with the exception of the _c configuration variable. The values for each key are important for successful communication.

Usage

domain1 -> domain2

To execute sayHi function on domain2 from domain1:

cbridge._cmdProxy([“sayHi”]);

domain1 <- domain2

To execute hello function on domain1 from domain2

cbridge._cmdProxy([“hello”, “world”]);

See an implementation in action :^P> ---- [domain1] [domain2]