feat(browser-extension): cache requests

This commit is contained in:
wanhose 2022-05-19 15:42:53 +02:00
parent 7b68541ce0
commit 51bd0e6717

View File

@ -12,6 +12,13 @@ const apiUrl = 'https://api.cookie-dialog-monster.com/rest/v1';
const baseDataUrl = 'https://raw.githubusercontent.com/wanhose/cookie-dialog-monster/main/data'; const baseDataUrl = 'https://raw.githubusercontent.com/wanhose/cookie-dialog-monster/main/data';
/**
* @description Cache data
* @type {{ attributes: string[], classes: string[], fixes: string[], selectors: string[], skips: string[] }}
*/
let cache = undefined;
/** /**
* @description Context menu identifier * @description Context menu identifier
* @type {string} * @type {string}
@ -66,10 +73,15 @@ const getCache = (hostname, callback) => {
* @async * @async
* @description Get all data from GitHub * @description Get all data from GitHub
* @param {void} callback * @param {void} callback
* @returns {Promise<{ attributes: string[], classes: string[], fixes: string[], selectors: string[], skips: [] }>} * @returns {Promise<{ attributes: string[], classes: string[], fixes: string[], selectors: string[], skips: string[] }>}
*/ */
const getData = async (callback) => { const getData = async (callback) => {
if (cache) {
callback(cache);
return;
}
const data = await Promise.all([ const data = await Promise.all([
query('classes'), query('classes'),
query('elements'), query('elements'),
@ -77,14 +89,18 @@ const getData = async (callback) => {
query('skips'), query('skips'),
]); ]);
callback({ const result = {
attributes: [ attributes: [
...new Set( ...new Set(
data[1].elements.flatMap((element) => { data[1].elements.flatMap((element) => {
const attributes = element.match(/(?<=\[)[^(){}[\]]+(?=\])/g); const attributes = element.match(/(?<=\[)[^(){}[\]]+(?=\])/g);
return attributes?.length return attributes?.length
? [...attributes.map((attribute) => attribute.replace(/\".*\"|(=|\^|\*|\$)/g, ''))] ? [
...attributes.flatMap((attribute) => {
return attribute ? [attribute.replace(/\".*\"|(=|\^|\*|\$)/g, '')] : [];
}),
]
: []; : [];
}) })
), ),
@ -93,7 +109,10 @@ const getData = async (callback) => {
fixes: data[2].fixes, fixes: data[2].fixes,
selectors: data[1].elements, selectors: data[1].elements,
skips: data[3].skips, skips: data[3].skips,
}); };
if (Object.keys(result).every((key) => result[key].length > 0)) cache = result;
callback(result);
}; };
/** /**