feat(browser-extension): add get skips to background script and various improvements

This commit is contained in:
wanhose 2022-05-06 13:17:43 +02:00
parent 2cac1a0ff1
commit 32c0499eb3

View File

@ -1,5 +1,13 @@
/**
* @description Base data URL
* @type {string}
*/
const baseDataUrl = 'https://raw.githubusercontent.com/wanhose/cookie-dialog-monster/main/data';
/** /**
* @description Context menu identifier * @description Context menu identifier
* @type {string}
*/ */
const contextMenuId = 'CDM_MENU'; const contextMenuId = 'CDM_MENU';
@ -9,150 +17,63 @@ const contextMenuId = 'CDM_MENU';
* @type {{ enabled: boolean }} * @type {{ enabled: boolean }}
*/ */
const initial = { const initial = { enabled: true };
enabled: true,
};
/**
* @description Check cache validity
* @param {object} [cache]
*/
const check = (cache) => typeof cache.enabled === 'boolean';
/** /**
* @description Disables icon * @description Disables icon
* @param {string} [tabId] * @param {string} tabId
*/ */
const disableIcon = (tabId) => { const disableIcon = (tabId) =>
chrome.browserAction.setIcon({ chrome.browserAction.setIcon({ path: 'assets/icons/disabled.png', tabId });
path: 'assets/icons/disabled.png',
tabId,
});
};
/**
* @description Disables popup
* @param {string} [tabId]
*/
const disablePopup = (tabId) => {
chrome.browserAction.setPopup({
popup: '',
tabId,
});
};
/** /**
* @description Enables icon * @description Enables icon
* @param {string} [tabId] * @param {string} tabId
*/ */
const enableIcon = (tabId) => { const enableIcon = (tabId) =>
chrome.browserAction.setIcon({ chrome.browserAction.setIcon({ path: 'assets/icons/enabled.png', tabId });
path: 'assets/icons/enabled.png',
tabId,
});
};
/** /**
* @description Enables popup * @description Enables popup
* @param {string} [tabId] * @param {string} tabId
*/ */
const enablePopup = (tabId) => { const enablePopup = (tabId) => chrome.browserAction.setPopup({ popup: 'popup.html', tabId });
chrome.browserAction.setPopup({
popup: 'popup.html',
tabId,
});
};
/** /**
* @description Retrieves cache state * @description Retrieves cache state
* @param {string} [hostname] * @param {string} hostname
* @param {void} [callback] * @param {void} callback
* @returns {Promise<{ enabled: boolean }>} * @returns {Promise<{ enabled: boolean }>}
*/ */
const getCache = (hostname, callback) => { const getCache = (hostname, callback) => {
chrome.storage.local.get(null, (store) => { chrome.storage.local.get(null, (store) => {
try { callback(store[hostname] ?? initial);
const cache = store[hostname];
if (!check(cache)) throw new Error();
callback(cache);
} catch {
chrome.storage.local.set({ [hostname]: initial });
callback(initial);
}
}); });
}; };
/** /**
* @async * @async
* @description Retrieves a selectors list * @description Retrieves data from GitHub
* @param {void} [callback] * @param {string} key
* @returns {Promise<{ classes: string[] }>} * @param {void} callback
* @returns {Promise<{ any: string[] }>}
*/ */
const getClasses = async (callback) => { const query = async (key, callback) => {
try { try {
const url = const url = `${baseDataUrl}/${key}.txt`;
'https://raw.githubusercontent.com/wanhose/cookie-dialog-monster/master/data/classes.txt';
const response = await fetch(url); const response = await fetch(url);
const data = await response.text(); const data = await response.text();
if (response.status !== 200) throw new Error(); if (response.status !== 200) throw new Error();
callback({ classes: data.split('\n') }); callback({ [key]: data.split('\n') });
} catch { } catch {
callback({ classes: [] }); callback({ [key]: [] });
}
};
/**
* @async
* @description Retrieves a selectors list
* @param {void} [callback]
* @returns {Promise<{ classes: string[] }>}
*/
const getFixes = async (callback) => {
try {
const url =
'https://raw.githubusercontent.com/wanhose/cookie-dialog-monster/master/data/fixes.txt';
const response = await fetch(url);
const data = await response.text();
if (response.status !== 200) throw new Error();
callback({ fixes: data.split('\n') });
} catch {
callback({ fixes: [] });
}
};
/**
* @async
* @description Retrieves a selectors list
* @param {void} [callback]
* @returns {Promise<{ selectors: string }>}
*/
const getSelectors = async (callback) => {
try {
const url =
'https://raw.githubusercontent.com/wanhose/cookie-dialog-monster/master/data/elements.txt';
const response = await fetch(url);
const data = await response.text();
if (response.status !== 200) throw new Error();
callback({ selectors: data.split('\n') });
} catch {
callback({ selectors: [] });
} }
}; };
@ -162,7 +83,7 @@ const getSelectors = async (callback) => {
* @returns {Promise<{ id: string, location: string }>} * @returns {Promise<{ id: string, location: string }>}
*/ */
const getTab = (callback) => { const queryTab = (callback) => {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => { chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
callback({ callback({
id: tabs[0].id, id: tabs[0].id,
@ -209,10 +130,7 @@ const updateCache = (hostname, state) => {
chrome.storage.local.set({ chrome.storage.local.set({
[hostname]: { [hostname]: {
enabled: enabled: typeof state.enabled === 'undefined' ? current.enabled : state.enabled,
typeof state.enabled === 'undefined'
? current.enabled
: state.enabled,
}, },
}); });
}); });
@ -223,39 +141,40 @@ const updateCache = (hostname, state) => {
*/ */
chrome.runtime.onMessage.addListener((request, sender, callback) => { chrome.runtime.onMessage.addListener((request, sender, callback) => {
const hasPermission = !sender.frameId || sender.frameId === 0; const hostname = request.hostname;
let tabId = sender.tab ? sender.tab.id : undefined; const state = request.state;
const tabId = sender.tab?.id;
switch (request.type) { switch (request.type) {
case 'DISABLE_ICON': case 'DISABLE_ICON':
if (hasPermission && tabId) disableIcon(tabId); if (tabId) disableIcon(tabId);
break;
case 'DISABLE_POPUP':
if (hasPermission && tabId) disablePopup(tabId);
break; break;
case 'ENABLE_ICON': case 'ENABLE_ICON':
if (hasPermission && tabId) enableIcon(tabId); if (tabId) enableIcon(tabId);
break; break;
case 'ENABLE_POPUP': case 'ENABLE_POPUP':
if (hasPermission && tabId) enablePopup(tabId); if (tabId) enablePopup(tabId);
break; break;
case 'GET_CACHE': case 'GET_CACHE':
getCache(request.hostname, callback); getCache(hostname, callback);
break; break;
case 'GET_CLASSES': case 'GET_CLASSES':
getClasses(callback); query('classes', callback);
break;
case 'GET_SKIPS':
query('skips', callback);
break; break;
case 'GET_FIXES': case 'GET_FIXES':
getFixes(callback); query('fixes', callback);
break; break;
case 'GET_SELECTORS': case 'GET_SELECTORS':
getSelectors(callback); query('elements', callback);
break; break;
case 'GET_TAB': case 'GET_TAB':
getTab(callback); queryTab(callback);
break; break;
case 'UPDATE_CACHE': case 'UPDATE_CACHE':
updateCache(request.hostname, request.state); updateCache(hostname, state);
break; break;
default: default:
break; break;