1 line
4.8 KiB
JavaScript
1 line
4.8 KiB
JavaScript
!function(p,h){h.wp=h.wp||{},h.wp.heartbeat=new function(){var e,r=p(document),o={suspend:!1,suspendEnabled:!0,screenId:"",url:"",lastTick:0,queue:{},mainInterval:60,tempInterval:0,originalInterval:0,countdown:0,connecting:!1,connectionError:!1,errorcount:0,hasConnected:!1,hasFocus:!0,userActivity:0,userActivityEvents:!1,beatTimer:0,winBlurTimer:0,frameBlurTimer:0};function n(){return(new Date).getTime()}function a(e){var t,n=e.src;if(!n||!/^https?:\/\//.test(n)||(t=h.location.origin||h.location.protocol+"//"+h.location.host,0===n.indexOf(t)))try{if(e.contentWindow.document)return 1}catch(e){}}function i(e,t){var n;if(e){switch(e){case"abort":break;case"timeout":n=!0;break;case"error":if(503===t&&o.hasConnected){n=!0;break}case"parsererror":case"empty":case"unknown":o.errorcount++,2<o.errorcount&&o.hasConnected&&(n=!0)}n&&!v()&&(o.connectionError=!0,r.trigger("heartbeat-connection-lost",[e,t]))}}function c(){var e;o.connecting||o.suspend||(o.lastTick=n(),e=p.extend({},o.queue),o.queue={},r.trigger("heartbeat-send",[e]),e={data:e,interval:o.tempInterval?o.tempInterval/1e3:o.mainInterval/1e3,_nonce:"object"==typeof h.heartbeatSettings?h.heartbeatSettings.nonce:"",action:"heartbeat",screen_id:o.screenId,has_focus:o.hasFocus},o.connecting=!0,o.xhr=p.ajax({url:o.url,type:"post",timeout:3e4,data:e,dataType:"json"}).always(function(){o.connecting=!1,u()}).done(function(e,t,n){var a;e?(o.hasConnected=!0,v()&&(o.errorcount=0,o.connectionError=!1,r.trigger("heartbeat-connection-restored")),e.nonces_expired?r.trigger("heartbeat-nonces-expired"):(e.heartbeat_interval&&(a=e.heartbeat_interval,delete e.heartbeat_interval),r.trigger("heartbeat-tick",[e,t,n]),a&&d(a))):i("empty")}).fail(function(e,t,n){i(t||"unknown",e.status),r.trigger("heartbeat-error",[e,t,n])}))}function u(){var e=n()-o.lastTick,t=o.mainInterval;o.suspend||(o.hasFocus?0<o.countdown&&o.tempInterval&&(t=o.tempInterval,o.countdown--,o.countdown<1&&(o.tempInterval=0)):t=12e4,h.clearTimeout(o.beatTimer),e<t?o.beatTimer=h.setTimeout(function(){c()},t-e):c())}function s(){t(),o.hasFocus=!1}function l(){t(),o.userActivity=n(),o.suspend=!1,o.hasFocus||(o.hasFocus=!0,u())}function t(){h.clearTimeout(o.winBlurTimer),h.clearTimeout(o.frameBlurTimer)}function f(){o.userActivityEvents=!1,r.off(".wp-heartbeat-active"),p("iframe").each(function(e,t){a(t)&&p(t.contentWindow).off(".wp-heartbeat-active")}),l()}function v(){return o.connectionError}function d(e,t){var n,a=o.tempInterval||o.mainInterval;if(e){switch(e){case"fast":case 5:n=5e3;break;case 15:n=15e3;break;case 30:n=3e4;break;case 60:n=6e4;break;case"long-polling":return o.mainInterval=0;default:n=o.originalInterval}5e3===n?(t=(t=parseInt(t,10)||30)<1||30<t?30:t,o.countdown=t,o.tempInterval=n):(o.countdown=0,o.tempInterval=0,o.mainInterval=n),n!==a&&u()}return o.tempInterval?o.tempInterval/1e3:o.mainInterval/1e3}return"string"==typeof h.pagenow&&(o.screenId=h.pagenow),"string"==typeof h.ajaxurl&&(o.url=h.ajaxurl),"object"==typeof h.heartbeatSettings&&(e=h.heartbeatSettings,!o.url&&e.ajaxurl&&(o.url=e.ajaxurl),e.interval&&(o.mainInterval=e.interval,o.mainInterval<15?o.mainInterval=15:60<o.mainInterval&&(o.mainInterval=60)),o.screenId||(o.screenId=e.screenId||"front"),"disable"===e.suspension&&(o.suspendEnabled=!1)),o.mainInterval=1e3*o.mainInterval,o.originalInterval=o.mainInterval,p(h).on("blur.wp-heartbeat-focus",function(){!function n(){p("iframe").each(function(e,t){a(t)&&(p.data(t,"wp-heartbeat-focus")||(p.data(t,"wp-heartbeat-focus",1),p(t.contentWindow).on("focus.wp-heartbeat-focus",function(){l()}).on("blur.wp-heartbeat-focus",function(){n(),o.frameBlurTimer=h.setTimeout(function(){s()},500)})))})}(),o.winBlurTimer=h.setTimeout(function(){s()},500)}).on("focus.wp-heartbeat-focus",function(){p("iframe").each(function(e,t){a(t)&&(p.removeData(t,"wp-heartbeat-focus"),p(t.contentWindow).off(".wp-heartbeat-focus"))}),l()}).on("unload.wp-heartbeat",function(){o.suspend=!0,o.xhr&&4!==o.xhr.readyState&&o.xhr.abort()}),h.setInterval(function(){var e;3e5<(e=o.userActivity?n()-o.userActivity:0)&&o.hasFocus&&s(),o.suspendEnabled&&12e5<e&&(o.suspend=!0),o.userActivityEvents||(r.on("mouseover.wp-heartbeat-active keyup.wp-heartbeat-active",function(){f()}),p("iframe").each(function(e,t){a(t)&&p(t.contentWindow).on("mouseover.wp-heartbeat-active keyup.wp-heartbeat-active",function(){f()})}),o.userActivityEvents=!0)},3e4),r.ready(function(){o.lastTick=n(),u()}),{hasFocus:function(){return o.hasFocus},connectNow:function(){o.lastTick=0,u()},disableSuspend:function(){o.suspendEnabled=!1},interval:d,hasConnectionError:v,enqueue:function(e,t,n){return!!e&&((!n||!this.isQueued(e))&&(o.queue[e]=t,!0))},dequeue:function(e){e&&delete o.queue[e]},isQueued:function(e){if(e)return o.queue.hasOwnProperty(e)},getQueuedItem:function(e){if(e)return this.isQueued(e)?o.queue[e]:void 0}}}}(jQuery,window); |