You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
PackageHelper/www/assets/js/storage.js

93 lines
2.4 KiB
JavaScript

/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/**
* Save something to persistent storage.
* @param {string} key
* @param {string} value non-string values are converted to strings.
* @param {bool} nochangeupdate If true, the settings version won't be updated.
* @returns {undefined}
*/
function setStorage(key, value, nochangeupdate) {
if (typeof nochangeupdate == 'undefined') {
nochangeupdate = false;
}
localStorage.setItem(key, value);
try {
NativeStorage.setItem(key, value);
} catch (ex) {
// skip
}
if (!nochangeupdate && !SETTINGS.synckeyblacklist.includes(key)) {
var version = getStorage("syncstateversion") == null ? 0 : getStorage("syncstateversion");
localStorage.setItem("syncstateversion", Number(version) + 1);
}
}
/**
* Get an item from persistent storage.
* @param {type} key
* @returns {DOMString}
*/
function getStorage(key) {
return localStorage.getItem(key);
}
/**
* Check if an item is in the persistent storage.
* @param {string} key
* @returns {Boolean}
*/
function inStorage(key) {
return localStorage.getItem(key) != null;
}
/**
* Erase the key and its value from the persistent storage.
* @param {string} key
* @returns {undefined}
*/
function removeStorage(key) {
localStorage.removeItem(key);
}
/**
* Get all item from persistent storage.
* @returns {Array} [{key: "", value: ""},...]
*/
function getAllStorage() {
var all = [];
for (var key in localStorage) {
if (localStorage.hasOwnProperty(key)) {
all.push({
key: key,
value: getStorage(key)
});
}
}
return all;
}
function copyLocalStorageToNativeStorage() {
for (var key in localStorage) {
if (localStorage.hasOwnProperty(key)) {
NativeStorage.setItem(key, localStorage.getItem(key));
}
}
}
function copyNativeStorageToLocalStorage() {
NativeStorage.keys(function (keys) {
for (var i = 0; i < keys.length; i++) {
(function (key) {
NativeStorage.getItem(key, function (val) {
localStorage.setItem(key, val);
});
})(keys[i]);
}
});
}