12 lines
5.6 KiB
JavaScript
12 lines
5.6 KiB
JavaScript
(function(h,d,x){d.wp=d.wp||{};d.wp.heartbeat=new function(){function k(){return(new Date).getTime()}function p(b){var c=b.src;if(c&&/^https?:\/\//.test(c)){var e=d.location.origin?d.location.origin:d.location.protocol+"//"+d.location.host;if(0!==c.indexOf(e))return!1}try{if(b.contentWindow.document)return!0}catch(f){}return!1}function q(){a.hasFocus&&!document.hasFocus()?a.hasFocus=!1:!a.hasFocus&&document.hasFocus()&&m()}function r(b,c){if(b){switch(b){case "timeout":var e=!0;break;case "error":if(503===
|
|
c&&a.hasConnected){e=!0;break}case "parsererror":case "empty":case "unknown":a.errorcount++,2<a.errorcount&&a.hasConnected&&(e=!0)}e&&!n()&&(a.connectionError=!0,g.trigger("heartbeat-connection-lost",[b,c]))}}function t(){if(!a.connecting&&!a.suspend){a.lastTick=k();var b=h.extend({},a.queue);a.queue={};g.trigger("heartbeat-send",[b]);b={data:b,interval:a.tempInterval?a.tempInterval/1E3:a.mainInterval/1E3,_nonce:"object"===typeof d.heartbeatSettings?d.heartbeatSettings.nonce:"",action:"heartbeat",
|
|
screen_id:a.screenId,has_focus:a.hasFocus};"customize"===a.screenId&&(b.wp_customize="on");a.connecting=!0;a.xhr=h.ajax({url:a.url,type:"post",timeout:3E4,data:b,dataType:"json"}).always(function(){a.connecting=!1;l()}).done(function(c,e,f){if(c){a.hasConnected=!0;n()&&(a.errorcount=0,a.connectionError=!1,g.trigger("heartbeat-connection-restored"));c.nonces_expired&&g.trigger("heartbeat-nonces-expired");if(c.heartbeat_interval){var u=c.heartbeat_interval;delete c.heartbeat_interval}c.heartbeat_nonce&&
|
|
"object"===typeof d.heartbeatSettings&&(d.heartbeatSettings.nonce=c.heartbeat_nonce,delete c.heartbeat_nonce);c.rest_nonce&&"object"===typeof d.wpApiSettings&&(d.wpApiSettings.nonce=c.rest_nonce);g.trigger("heartbeat-tick",[c,e,f]);u&&v(u)}else r("empty")}).fail(function(c,e,f){r(e||"unknown",c.status);g.trigger("heartbeat-error",[c,e,f])})}}function l(){var b=k()-a.lastTick,c=a.mainInterval;a.suspend||(a.hasFocus?0<a.countdown&&a.tempInterval&&(c=a.tempInterval,a.countdown--,1>a.countdown&&(a.tempInterval=
|
|
0)):c=12E4,a.minimalInterval&&c<a.minimalInterval&&(c=a.minimalInterval),d.clearTimeout(a.beatTimer),b<c?a.beatTimer=d.setTimeout(function(){t()},c-b):t())}function m(){a.userActivity=k();a.suspend=!1;a.hasFocus||(a.hasFocus=!0,l())}function w(){a.userActivityEvents=!1;g.off(".wp-heartbeat-active");h("iframe").each(function(b,c){p(c)&&h(c.contentWindow).off(".wp-heartbeat-active")});m()}function y(){var b=a.userActivity?k()-a.userActivity:0;3E5<b&&a.hasFocus&&(a.hasFocus=!1);if(a.suspendEnabled&&
|
|
6E5<b||36E5<b)a.suspend=!0;a.userActivityEvents||(g.on("mouseover.wp-heartbeat-active keyup.wp-heartbeat-active touchend.wp-heartbeat-active",function(){w()}),h("iframe").each(function(c,e){if(p(e))h(e.contentWindow).on("mouseover.wp-heartbeat-active keyup.wp-heartbeat-active touchend.wp-heartbeat-active",function(){w()})}),a.userActivityEvents=!0)}function n(){return a.connectionError}function v(b,c){var e=a.tempInterval?a.tempInterval:a.mainInterval;if(b){switch(b){case "fast":case 5:var f=5E3;
|
|
break;case 15:f=15E3;break;case 30:f=3E4;break;case 60:f=6E4;break;case 120:f=12E4;break;case "long-polling":return a.mainInterval=0;default:f=a.originalInterval}a.minimalInterval&&f<a.minimalInterval&&(f=a.minimalInterval);5E3===f?(c=parseInt(c,10)||30,a.countdown=1>c||30<c?30:c,a.tempInterval=f):(a.countdown=0,a.tempInterval=0,a.mainInterval=f);f!==e&&l()}return a.tempInterval?a.tempInterval/1E3:a.mainInterval/1E3}var g=h(document),a={suspend:!1,suspendEnabled:!0,screenId:"",url:d.ElementorConfig?
|
|
ElementorConfig.ajaxurl:"",lastTick:0,queue:{},mainInterval:60,tempInterval:0,originalInterval:0,minimalInterval:0,countdown:0,connecting:!1,connectionError:!1,errorcount:0,hasConnected:!1,hasFocus:!0,userActivity:0,userActivityEvents:!1,checkFocusTimer:0,beatTimer:0};(function(){"string"===typeof d.pagenow&&(a.screenId=d.pagenow);"string"===typeof d.ajaxurl&&(a.url=d.ajaxurl);if("object"===typeof d.heartbeatSettings){var b=d.heartbeatSettings;!a.url&&b.ajaxurl&&(a.url=b.ajaxurl);b.interval&&(a.mainInterval=
|
|
b.interval,15>a.mainInterval?a.mainInterval=15:120<a.mainInterval&&(a.mainInterval=120));b.minimalInterval&&(b.minimalInterval=parseInt(b.minimalInterval,10),a.minimalInterval=0<b.minimalInterval&&600>=b.minimalInterval?1E3*b.minimalInterval:0);a.minimalInterval&&a.mainInterval<a.minimalInterval&&(a.mainInterval=a.minimalInterval);a.screenId||(a.screenId=b.screenId||"front");"disable"===b.suspension&&(a.suspendEnabled=!1)}a.mainInterval*=1E3;a.originalInterval=a.mainInterval;if("undefined"!==typeof document.hidden){var c=
|
|
"hidden";var e="visibilitychange";var f="visibilityState"}else"undefined"!==typeof document.msHidden?(c="msHidden",e="msvisibilitychange",f="msVisibilityState"):"undefined"!==typeof document.webkitHidden&&(c="webkitHidden",e="webkitvisibilitychange",f="webkitVisibilityState");c&&(document[c]&&(a.hasFocus=!1),g.on(e+".wp-heartbeat",function(){"hidden"===document[f]?(a.hasFocus=!1,d.clearInterval(a.checkFocusTimer)):(m(),document.hasFocus&&(a.checkFocusTimer=d.setInterval(q,1E4)))}));document.hasFocus&&
|
|
(a.checkFocusTimer=d.setInterval(q,1E4));h(d).on("unload.wp-heartbeat",function(){a.suspend=!0;a.xhr&&4!==a.xhr.readyState&&a.xhr.abort()});d.setInterval(y,3E4);g.ready(function(){a.lastTick=k();l()})})();return{hasFocus:function(){return a.hasFocus},connectNow:function(){a.lastTick=0;l()},disableSuspend:function(){a.suspendEnabled=!1},interval:v,hasConnectionError:n,enqueue:function(b,c,e){if(b){if(e&&this.isQueued(b))return!1;a.queue[b]=c;return!0}return!1},dequeue:function(b){b&&delete a.queue[b]},
|
|
isQueued:function(b){if(b)return a.queue.hasOwnProperty(b)},getQueuedItem:function(b){if(b)return this.isQueued(b)?a.queue[b]:x}}}})(jQuery,window); |