src/iframe-helper.js
/**
 * Allows listening for message from the iframe. Returns an unlisten
 * function to remove the listener.
 *
 * @param {!Element} iframe.
 * @param {string} typeOfMessage.
 * @param {?function(!JsonObject, !Window, string)} callback Called when a
 *     message of this type arrives for this iframe.
 * @param {boolean=} opt_is3P set to true if the iframe is 3p.
 * @param {boolean=} opt_includingNestedWindows set to true if a messages from
 *     nested frames should also be accepted.
 * @return {!UnlistenDef}
 */
Allows listening for message from the iframe. Returns an unlisten
function to remove the listener.

@param {!Element} iframe.
Allows listening for message from the iframe . Returns an unlisten function to remove the listener . @param { !Element } iframe .
function listenFor(
    iframe, typeOfMessage, callback, opt_is3P, opt_includingNestedWindows) {
  dev().assert(iframe.src, 'only iframes with src supported');
  dev().assert(!iframe.parentNode, 'cannot register events on an attached ' +
      'iframe. It will cause hair-pulling bugs like #2942');
  dev().assert(callback);
  const parentWin = iframe.ownerDocument.defaultView;
  registerGlobalListenerIfNeeded(parentWin);
  const listenForEvents = getOrCreateListenForEvents(
      parentWin,
      iframe,
      opt_is3P
  );
  let events = listenForEvents[typeOfMessage] ||
    (listenForEvents[typeOfMessage] = []);
  let unlisten;
  let listener = function(data, source, origin) {
    if (!opt_includingNestedWindows && source != iframe.contentWindow) {
      return;
    }
    if (data.sentinel == UNLISTEN_SENTINEL) {
      unlisten();
      return;
    }
    callback(data, source, origin);
  };
  events.push(listener);
  return unlisten = function() {
    if (listener) {
      const index = events.indexOf(listener);
      if (index > -1) {
        events.splice(index, 1);
      }
      listener = null;
      events = null;
      callback = null;
    }
  };
}
function listenFor ( iframe , typeOfMessage , callback , opt_is3P , opt_includingNestedWindows ) { dev ( ) . assert ( iframe . src , 'only iframes with src supported' ) ; dev ( ) . assert ( ! iframe . parentNode , 'cannot register events on an attached ' + 'iframe. It will cause hair-pulling bugs like #2942' ) ; dev ( ) . assert ( callback ) ; const parentWin = iframe . ownerDocument . defaultView ; registerGlobalListenerIfNeeded ( parentWin ) ; const listenForEvents = getOrCreateListenForEvents ( parentWin , iframe , opt_is3P ) ; let events = listenForEvents [ typeOfMessage ] || ( listenForEvents [ typeOfMessage ] = [ ] ) ; let unlisten ; let listener = function ( data , source , origin ) { if ( ! opt_includingNestedWindows && source != iframe . contentWindow ) { return ; } if ( data . sentinel == UNLISTEN_SENTINEL ) { unlisten ( ) ; return ; } callback ( data , source , origin ) ; } ; events . push ( listener ) ; return unlisten = function ( ) { if ( listener ) { const index = events . indexOf ( listener ) ; if ( index > - 1 ) { events . splice ( index , 1 ) ; } listener = null ; events = null ; callback = null ; } } ; }
Allows listening for message from the iframe.
Allows listening for message from the iframe .
// Exclude nested frames if necessary. // Note that the source was already verified to be either the contentWindow // of the iframe itself or a descendant window within it. // Make sure references to the unlisten function do not keep // alive too much.