From 4153b4cae3fab4471463c817048bf4bd446d79d8 Mon Sep 17 00:00:00 2001 From: Oliver Tacke Date: Tue, 17 Mar 2020 18:20:54 +0100 Subject: [PATCH] Add site specific prefix to localStorage item identifier Required for multisite instances on the same domain --- js/h5p.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/js/h5p.js b/js/h5p.js index 556776d..f74710a 100644 --- a/js/h5p.js +++ b/js/h5p.js @@ -2398,7 +2398,7 @@ H5P.createTitle = function (rawTitle, maxLength) { */ if (preloadedData && preloadedData[subContentId] && preloadedData[subContentId][dataId] === '{}') { if (H5PIntegration.saveContentStorages && H5PIntegration.saveContentStorages.localStorage && H5P.localStorageSupported) { - const localStorageData = window.localStorage.getItem('H5P-cid-' + contentId + '-sid-' + subContentId); + const localStorageData = window.localStorage.getItem(H5P.getLocalStoragePrefix() + 'H5P-cid-' + contentId + '-sid-' + subContentId); if (localStorageData) { let data = {}; @@ -2541,7 +2541,7 @@ H5P.createTitle = function (rawTitle, maxLength) { ) { // Add checksum of params to detect changes for resetting localStorage window.localStorage.setItem( - 'H5P-cid-' + contentId + '-sid-' + options.subContentId, + H5P.getLocalStoragePrefix() + 'H5P-cid-' + contentId + '-sid-' + options.subContentId, '{"checksum":' + H5P.getNumericalHash(content.jsonContent) + ',"state":' + data + '}' ); } @@ -2573,7 +2573,7 @@ H5P.createTitle = function (rawTitle, maxLength) { contentUserDataAjax(contentId, dataId, subContentId, function (error) { // When done deleting user data in DB, delete in localStorage if ((!error || error === 'Not signed in.') && H5P.localStorageSupported) { - window.localStorage.removeItem('H5P-cid-' + contentId + '-sid-' + subContentId); + window.localStorage.removeItem(H5P.getLocalStoragePrefix() + 'H5P-cid-' + contentId + '-sid-' + subContentId); } }, null); }; @@ -2695,6 +2695,19 @@ H5P.createTitle = function (rawTitle, maxLength) { }, 0); }; + /** + * Get prefix for localStorage. Relevant for multiple instances on same domain. + * + * @return {string} Prefix for localStorage. + */ + H5P.getLocalStoragePrefix = function () { + if (!H5PIntegration.saveContentStorages || !H5PIntegration.saveContentStorages.localStorage || typeof(H5PIntegration.saveContentStorages.localStorage) !== 'string') { + return ''; + } + + return H5PIntegration.saveContentStorages.localStorage; + }; + /** * Get config for a library *