Advertisement
Guest User

nuxPostman

a guest
Aug 30th, 2012
441
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     Simple Frame Postman
  3.    
  4.     version 1.1.0
  5.    
  6.     Usage:
  7.     var oPostman = new nuxPostman({
  8.         strDestFrameId : 'iframe_id',           // id of a frame (or iframe) element that will receive messages
  9.         strDestFrameDomain : "nux:8081",        // domain of the frame e.g. 'www.example.com', 'www.example.com:8080'...
  10.        
  11.         strMsgSourceDomain : "localhost:8081"   // domain of the sender (the window that is sending the message) formated as above
  12.         or
  13.         // domain regexp match (only for advanced users!)
  14.         // use :// match at the begining of your regexp and $ at the end to avoid to broad matches
  15.         reMsgSourceBaseUrls : /:\/\/(localhost(:[0-9]+)?|nux(:[0-9]+)?|nuxlap7(:[0-9]+)?)$/
  16.     });
  17. */
  18. function nuxPostman(oParams)
  19. {
  20.     this.strDestFrameId = oParams.strDestFrameId;
  21.     this.strDestFrameBaseUrl = "http://"+oParams.strDestFrameDomain;
  22.     if (typeof(oParams.strMsgSourceDomain)!='undefined')
  23.     {
  24.         this.strMsgSourceBaseUrl = "http://"+oParams.strMsgSourceDomain;
  25.     }
  26.     if (typeof(oParams.reMsgSourceBaseUrls)!='undefined')
  27.     {
  28.         this.reMsgSourceBaseUrls = oParams.reMsgSourceBaseUrls;
  29.     }
  30.    
  31.     /*
  32.         Send given message (strMsg) - should be run outside the frame
  33.     */
  34.     this.send = function(strMsg)
  35.     {
  36.         // debug
  37.         if (typeof(window.console)=="object" && typeof(window.console.log)=="function")
  38.         {
  39.             console.log ('send to:'+this.strDestFrameBaseUrl);
  40.         }
  41.        
  42.         var winFrame = document.getElementById(this.strDestFrameId).contentWindow;
  43.         winFrame.postMessage(
  44.             strMsg,
  45.             this.strDestFrameBaseUrl
  46.         );
  47.     }
  48.  
  49.     /*
  50.         Inits reciever - should be run inside the frame
  51.        
  52.         funMsgReceiver is a function that will be called upon receiving the message
  53.     */
  54.     this.initReceiver = function(funMsgReceiver)
  55.     {
  56.         var _this = this;
  57.         window.addEventListener("message", function(e)
  58.         {
  59.             // debug
  60.             if (typeof(window.console)=="object" && typeof(window.console.log)=="function")
  61.             {
  62.                 console.log ('received from:'+e.origin);
  63.                 //debugger;
  64.             }
  65.            
  66.             if (typeof(_this.reMsgSourceBaseUrls)!='undefined')
  67.             {
  68.                 if (e.origin.search(_this.reMsgSourceBaseUrls)<0)
  69.                 {
  70.                     return;
  71.                 }
  72.             }
  73.             else if (e.origin !== _this.strMsgSourceBaseUrl)
  74.             {
  75.                 return;
  76.             }
  77.             funMsgReceiver(e.data);
  78.         }, false);
  79.     }
  80. }
  81.  
  82.  
  83. /*
  84.    
  85. *
  86. framemsg.initSender = function ()
  87. {
  88.     document.getElementById("form").onsubmit = function(e)
  89.     {
  90.         oPostman.send(document.getElementById("msg").value);
  91.         e.preventDefault();
  92.     };
  93. }
  94.  
  95. /*
  96.    
  97. *
  98. framemsg.initReceiver = function ()
  99. {
  100.     oPostman.initReceiver(function(strMsg)
  101.     {
  102.         document.getElementById("test").textContent = "parent said: " + strMsg;
  103.     });
  104. }
  105. /**/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement