Thanks for the additional info. I think I understand your data model and what you’re trying to do. I’m assuming that the JSON object you provided represents the data in storage.local
and that the strings firefox-default
and www.google.de
are they keys used to access those values. Also, if I understand correctly, you’re going to be accessing the firefox-default
and www.google.de
storage keys separately.
If I’ve got that right, then you could do something like this
async function getSiteRules({container, profile, domain}={}) {
const storageKey = container || profile;
const data = await browser.storage.local.get([storageKey]);
if (container) {
data = data[container];
}
return data?.[profile]?.[domain];
}
// Usage examples
await getSiteRules({
container: "flagCookies_logged",
profile: "firefox-default",
domain: "www.google.de",
});
// Returns
// {
// ".google.de": {
// "__Secure-ENID": true
// }
// }
await getSiteRules({
profile: "firefox-default",
domain: "www.google.de",
});
// Returns:
// {
// ".google.de": {
// "AEC": true,
// "SOCS": false
// }
// }
await getSiteRules({
profile: "unset",
domain: "www.google.de",
});
// Returns
// undefined
If you’re doing a lot of work with deeply nested objects, you might want to take a look at a library like Lodash. It has a number of very handy utilities like _.get()
to retrieve a deeply nested property of an object or _.merge()
to perform a deep merge of two objects. You can use them to, for example, get all of the rules for a given site in one go:
async function getAllSiteRules({container, profile, domain} = {}) {
const storageData = browser.storage.local.get([profile, container]);
const globalValue = _.get(storageData, [profile, domain]);
const containerValue = _.get(storageData, [container, profile, domain]);
const final = _.merge(globalValue, containerValue);
return final;
}
// Usage example
await getAllSiteRules(({
container: "flagCookies_logged",
profile: "firefox-default",
domain: "www.google.de",
});
// Returns
// {
// ".google.de": {
// "AEC": true,
// "SOCS": false,
// "__Secure-ENID": true
// }
// }