From 752e6676e1c18eb269f4e2b19415c0f1f6c7c7cc Mon Sep 17 00:00:00 2001 From: Vincent Petry Date: Tue, 22 Mar 2016 16:54:01 +0100 Subject: [PATCH] Detect user navigating away, don't interpret as ajax error Whenever a user navigates away, all ajax calls will fail with the same result like a cross-domain redirect (SSO). To distinguish these cases, we need to detect whether the error is a result of the user navigating away. For this, we introduce a new flag that will be set in "beforeunload". Additional handling was required for false positives in case "beforeunload" is used (ex: cancelled upload) and the user cancelled the navigation. --- core/js/js.js | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/core/js/js.js b/core/js/js.js index e90ceaf4e1..584bf1c1c8 100644 --- a/core/js/js.js +++ b/core/js/js.js @@ -741,7 +741,9 @@ var OC={ */ _processAjaxError: function(xhr) { // purposefully aborted request ? - if (xhr.status === 0 && (xhr.statusText === 'abort' || xhr.statusText === 'timeout')) { + // this._userIsNavigatingAway needed to distinguish ajax calls cancelled by navigating away + // from calls cancelled by failed cross-domain ajax due to SSO redirect + if (xhr.status === 0 && (xhr.statusText === 'abort' || xhr.statusText === 'timeout' || this._userIsNavigatingAway)) { return; } @@ -1438,6 +1440,29 @@ function initCore() { $('html').addClass('edge'); } + $(window).on('unload', function() { + OC._unloadCalled = true; + }); + $(window).on('beforeunload', function() { + // super-trick thanks to http://stackoverflow.com/a/4651049 + // in case another handler displays a confirmation dialog (ex: navigating away + // during an upload), there are two possible outcomes: user clicked "ok" or + // "cancel" + + // first timeout handler is called after unload dialog is closed + setTimeout(function() { + OC._userIsNavigatingAway = true; + + // second timeout event is only called if user cancelled (Chrome), + // but in other browsers it might still be triggered, so need to + // set a higher delay... + setTimeout(function() { + if (!OC._unloadCalled) { + OC._userIsNavigatingAway = false; + } + }, 10000); + },1); + }); $(document).on('ajaxError.main', function( event, request, settings ) { if (settings && settings.allowAuthErrors) { return;