From eb444f252b5630e43c7d62eac35529918cc44a78 Mon Sep 17 00:00:00 2001 From: Oliver Tacke Date: Tue, 20 Aug 2019 12:19:34 +0200 Subject: [PATCH 1/2] Fix uncaught exception when accessing localStorage --- js/request-queue.js | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/js/request-queue.js b/js/request-queue.js index 5b367e1..5cb70eb 100644 --- a/js/request-queue.js +++ b/js/request-queue.js @@ -19,6 +19,19 @@ H5P.RequestQueue = (function ($, EventDispatcher) { this.itemName = 'requestQueue'; }; + /** + * Check whether localStorage is accessible. + * @return {boolean} True, if window.localStorage is accessible, else false. + */ + RequestQueue.prototype.isLocalStorageAccessible = function () { + try { + return (window.localStorage) ? true : false; + } + catch (event) { + return false; + } + }; + /** * Add request to queue. Only supports posts currently. * @@ -27,7 +40,7 @@ H5P.RequestQueue = (function ($, EventDispatcher) { * @returns {boolean} */ RequestQueue.prototype.add = function (url, data) { - if (!window.localStorage) { + if (!this.isLocalStorageAccessible()) { return false; } @@ -56,7 +69,7 @@ H5P.RequestQueue = (function ($, EventDispatcher) { * @returns {boolean|Array} Stored requests */ RequestQueue.prototype.getStoredRequests = function () { - if (!window.localStorage) { + if (!this.isLocalStorageAccessible()) { return false; } @@ -74,7 +87,7 @@ H5P.RequestQueue = (function ($, EventDispatcher) { * @returns {boolean} True if the storage was successfully cleared */ RequestQueue.prototype.clearQueue = function () { - if (!window.localStorage) { + if (!this.isLocalStorageAccessible()) { return false; } @@ -89,7 +102,7 @@ H5P.RequestQueue = (function ($, EventDispatcher) { */ RequestQueue.prototype.resumeQueue = function () { // Not supported - if (!H5PIntegration || !window.navigator || !window.localStorage) { + if (!H5PIntegration || !window.navigator || !this.isLocalStorageAccessible()) { return false; } From 7c123cd114cfe68c34aa1c165bd1e7f507031eba Mon Sep 17 00:00:00 2001 From: Oliver Tacke Date: Thu, 29 Aug 2019 11:52:04 +0200 Subject: [PATCH 2/2] Use dummy OfflineRequestQueue if no local storage access --- js/request-queue.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/js/request-queue.js b/js/request-queue.js index 5cb70eb..c8d4682 100644 --- a/js/request-queue.js +++ b/js/request-queue.js @@ -217,6 +217,21 @@ H5P.RequestQueue = (function ($, EventDispatcher) { */ H5P.OfflineRequestQueue = (function (RequestQueue, Dialog) { + let localStorageSupported; + try { + localStorageSupported = (window.localStorage !== undefined); + } + catch (exception) { + localStorageSupported = false; + } + + if (!localStorageSupported) { + // Return dummy class with the same interface, but which does nothing: + return function () { + this.add = function () {}; + }; + } + /** * Constructor *