From 0cde6bdf7f33af398a841aa56c8e9effa975c6d3 Mon Sep 17 00:00:00 2001 From: Thomas Horn Sivertsen <4159138+tsivert@users.noreply.github.com> Date: Tue, 6 Oct 2020 09:12:34 +0200 Subject: [PATCH] Updated check for localStorage When the H5P is embedded inside and iframe and in an inkognito browser, the current check for local storage fails. --- 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..52bfd91 100644 --- a/js/request-queue.js +++ b/js/request-queue.js @@ -19,6 +19,19 @@ H5P.RequestQueue = (function ($, EventDispatcher) { this.itemName = 'requestQueue'; }; + /** + * Check to see if localStorage is available + * + * @return {boolean} + */ + const hasLocalStorage = function () { + try { + return !!window.localStorage; + } catch (err){ + 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 (!hasLocalStorage()) { return false; } @@ -56,7 +69,7 @@ H5P.RequestQueue = (function ($, EventDispatcher) { * @returns {boolean|Array} Stored requests */ RequestQueue.prototype.getStoredRequests = function () { - if (!window.localStorage) { + if (!hasLocalStorage()) { 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 (!hasLocalStorage()) { 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 || !hasLocalStorage()) { return false; }