Updated check for localStorage

When the H5P is embedded inside and iframe and in an inkognito browser, the current check for local storage fails.
pull/92/head
Thomas Horn Sivertsen 2020-10-06 09:12:34 +02:00 committed by GitHub
parent 1a644dda11
commit 0cde6bdf7f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 4 deletions

View File

@ -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;
}