Heartbeat API: fix error in IE < 9, props SergeyBiryukov, don't attempt to bind events to cross-domain iframes, see #23216

git-svn-id: http://core.svn.wordpress.org/trunk@23389 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Andrew Ozz 2013-02-06 07:10:04 +00:00
parent 5f05d09dc2
commit 6985523d00
1 changed files with 22 additions and 1 deletions

View File

@ -31,7 +31,7 @@ window.wp = window.wp || {};
if ( typeof( window.heartbeatSettings != 'undefined' ) ) { if ( typeof( window.heartbeatSettings != 'undefined' ) ) {
settings = $.extend( {}, window.heartbeatSettings ); settings = $.extend( {}, window.heartbeatSettings );
delete window.heartbeatSettings; window.heartbeatSettings = null;
// Add private vars // Add private vars
nonce = settings.nonce || ''; nonce = settings.nonce || '';
@ -63,6 +63,15 @@ window.wp = window.wp || {};
return (new Date()).getTime(); return (new Date()).getTime();
} }
function isLocalFrame(frame) {
try {
if ( frame.contentWindow.document )
return true;
} catch(e) {}
return false;
}
// Set error state and fire an event if errors persist for over 2 min when the window has focus // Set error state and fire an event if errors persist for over 2 min when the window has focus
// or 6 min when the window is in the background // or 6 min when the window is in the background
function errorstate() { function errorstate() {
@ -187,6 +196,9 @@ window.wp = window.wp || {};
function setFrameEvents() { function setFrameEvents() {
$('iframe').each( function(i, frame){ $('iframe').each( function(i, frame){
if ( !isLocalFrame(frame) )
return;
if ( $.data(frame, 'wp-heartbeat-focus') ) if ( $.data(frame, 'wp-heartbeat-focus') )
return; return;
@ -206,6 +218,9 @@ window.wp = window.wp || {};
winBlurTimeout = window.setTimeout( function(){ blurred(); }, 500 ); winBlurTimeout = window.setTimeout( function(){ blurred(); }, 500 );
}).on('focus.wp-heartbeat-focus', function(){ }).on('focus.wp-heartbeat-focus', function(){
$('iframe').each( function(i, frame){ $('iframe').each( function(i, frame){
if ( !isLocalFrame(frame) )
return;
$.removeData(frame, 'wp-heartbeat-focus'); $.removeData(frame, 'wp-heartbeat-focus');
$(frame.contentWindow).off('.wp-heartbeat-focus'); $(frame.contentWindow).off('.wp-heartbeat-focus');
}); });
@ -217,6 +232,9 @@ window.wp = window.wp || {};
userActiveEvents = false; userActiveEvents = false;
$(document).off('.wp-heartbeat-active'); $(document).off('.wp-heartbeat-active');
$('iframe').each( function(i, frame){ $('iframe').each( function(i, frame){
if ( !isLocalFrame(frame) )
return;
$(frame.contentWindow).off('.wp-heartbeat-active'); $(frame.contentWindow).off('.wp-heartbeat-active');
}); });
@ -243,6 +261,9 @@ window.wp = window.wp || {};
if ( !userActiveEvents ) { if ( !userActiveEvents ) {
$(document).on('mouseover.wp-heartbeat-active keyup.wp-heartbeat-active', function(){ userIsActive(); }); $(document).on('mouseover.wp-heartbeat-active keyup.wp-heartbeat-active', function(){ userIsActive(); });
$('iframe').each( function(i, frame){ $('iframe').each( function(i, frame){
if ( !isLocalFrame(frame) )
return;
$(frame.contentWindow).on('mouseover.wp-heartbeat-active keyup.wp-heartbeat-active', function(){ userIsActive(); }); $(frame.contentWindow).on('mouseover.wp-heartbeat-active keyup.wp-heartbeat-active', function(){ userIsActive(); });
}); });
userActiveEvents = true; userActiveEvents = true;