Fix uncaught exception when accessing localStorage

pull/66/head
Oliver Tacke 2019-08-20 12:19:34 +02:00
parent 7bc8666f30
commit eb444f252b
1 changed files with 17 additions and 4 deletions

View File

@ -19,6 +19,19 @@ H5P.RequestQueue = (function ($, EventDispatcher) {
this.itemName = 'requestQueue'; 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. * Add request to queue. Only supports posts currently.
* *
@ -27,7 +40,7 @@ H5P.RequestQueue = (function ($, EventDispatcher) {
* @returns {boolean} * @returns {boolean}
*/ */
RequestQueue.prototype.add = function (url, data) { RequestQueue.prototype.add = function (url, data) {
if (!window.localStorage) { if (!this.isLocalStorageAccessible()) {
return false; return false;
} }
@ -56,7 +69,7 @@ H5P.RequestQueue = (function ($, EventDispatcher) {
* @returns {boolean|Array} Stored requests * @returns {boolean|Array} Stored requests
*/ */
RequestQueue.prototype.getStoredRequests = function () { RequestQueue.prototype.getStoredRequests = function () {
if (!window.localStorage) { if (!this.isLocalStorageAccessible()) {
return false; return false;
} }
@ -74,7 +87,7 @@ H5P.RequestQueue = (function ($, EventDispatcher) {
* @returns {boolean} True if the storage was successfully cleared * @returns {boolean} True if the storage was successfully cleared
*/ */
RequestQueue.prototype.clearQueue = function () { RequestQueue.prototype.clearQueue = function () {
if (!window.localStorage) { if (!this.isLocalStorageAccessible()) {
return false; return false;
} }
@ -89,7 +102,7 @@ H5P.RequestQueue = (function ($, EventDispatcher) {
*/ */
RequestQueue.prototype.resumeQueue = function () { RequestQueue.prototype.resumeQueue = function () {
// Not supported // Not supported
if (!H5PIntegration || !window.navigator || !window.localStorage) { if (!H5PIntegration || !window.navigator || !this.isLocalStorageAccessible()) {
return false; return false;
} }