From eb444f252b5630e43c7d62eac35529918cc44a78 Mon Sep 17 00:00:00 2001 From: Oliver Tacke Date: Tue, 20 Aug 2019 12:19:34 +0200 Subject: [PATCH] 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; }