2021-06-22 22:01:56 +00:00
|
|
|
/**
|
|
|
|
* @var cacheInitialState
|
|
|
|
* @description Cache initial state
|
|
|
|
* @type {{ enabled: boolean, matches: string[] }}
|
|
|
|
*/
|
|
|
|
|
|
|
|
const cacheInitialState = {
|
|
|
|
enabled: true,
|
|
|
|
matches: [],
|
|
|
|
};
|
|
|
|
|
2021-06-23 11:25:43 +00:00
|
|
|
/**
|
|
|
|
* @function isValid
|
|
|
|
* @description Check cache validity
|
|
|
|
*
|
|
|
|
* @param {object} [cache]
|
|
|
|
*/
|
|
|
|
|
|
|
|
const isValid = (cache) =>
|
|
|
|
typeof cache.enabled === "boolean" &&
|
|
|
|
Array.isArray(cache.matches) &&
|
|
|
|
cache.matches.every((match) => typeof match === "string");
|
|
|
|
|
2021-04-08 21:49:03 +00:00
|
|
|
/**
|
|
|
|
* @function disableIcon
|
2021-07-01 15:53:54 +00:00
|
|
|
* @description Disables icon
|
2021-04-08 21:49:03 +00:00
|
|
|
*
|
|
|
|
* @param {string} [tabId]
|
|
|
|
*/
|
|
|
|
|
|
|
|
const disableIcon = (tabId) => {
|
2021-07-01 15:53:54 +00:00
|
|
|
chrome.browserAction.setIcon({
|
|
|
|
path: "assets/icons/disabled.png",
|
|
|
|
tabId,
|
|
|
|
});
|
2021-04-08 21:49:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @function disablePopup
|
2021-07-01 15:53:54 +00:00
|
|
|
* @description Disables popup
|
2021-04-08 21:49:03 +00:00
|
|
|
*
|
|
|
|
* @param {string} [tabId]
|
|
|
|
*/
|
|
|
|
|
|
|
|
const disablePopup = (tabId) => {
|
2021-07-01 15:53:54 +00:00
|
|
|
chrome.browserAction.setPopup({
|
|
|
|
popup: "",
|
|
|
|
tabId,
|
|
|
|
});
|
2021-04-08 21:49:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @function enableIcon
|
2021-07-01 15:53:54 +00:00
|
|
|
* @description Enables icon
|
2021-04-08 21:49:03 +00:00
|
|
|
*
|
|
|
|
* @param {string} [tabId]
|
|
|
|
*/
|
|
|
|
|
|
|
|
const enableIcon = (tabId) => {
|
2021-07-01 15:53:54 +00:00
|
|
|
chrome.browserAction.setIcon({
|
|
|
|
path: "assets/icons/enabled.png",
|
|
|
|
tabId,
|
|
|
|
});
|
2021-04-08 21:49:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @function enablePopup
|
2021-07-01 15:53:54 +00:00
|
|
|
* @description Enables popup
|
2021-04-08 21:49:03 +00:00
|
|
|
*
|
|
|
|
* @param {string} [tabId]
|
|
|
|
*/
|
|
|
|
|
|
|
|
const enablePopup = (tabId) => {
|
2021-07-01 15:53:54 +00:00
|
|
|
chrome.browserAction.setPopup({
|
|
|
|
popup: "popup.html",
|
|
|
|
tabId,
|
|
|
|
});
|
2021-04-08 21:49:03 +00:00
|
|
|
};
|
|
|
|
|
2021-06-22 22:01:56 +00:00
|
|
|
/**
|
|
|
|
* @function getCache
|
|
|
|
* @description Retrieves cache state
|
|
|
|
*
|
2021-06-23 11:25:43 +00:00
|
|
|
* @param {string} [hostname]
|
|
|
|
* @param {void} [responseCallback]
|
2021-06-22 22:01:56 +00:00
|
|
|
* @returns {Promise<{ enabled: boolean, matches: string[] }>} Cache state
|
|
|
|
*/
|
|
|
|
|
2021-06-27 11:17:03 +00:00
|
|
|
const getCache = (hostname, responseCallback) => {
|
2021-06-22 22:01:56 +00:00
|
|
|
chrome.storage.local.get(null, (store) => {
|
2021-06-23 11:25:43 +00:00
|
|
|
try {
|
|
|
|
const cache = store[hostname];
|
|
|
|
|
|
|
|
if (!isValid(cache)) throw new Error();
|
2021-06-22 22:01:56 +00:00
|
|
|
|
|
|
|
responseCallback(cache);
|
2021-06-23 11:25:43 +00:00
|
|
|
} catch {
|
2021-06-22 22:01:56 +00:00
|
|
|
chrome.storage.local.set({ [hostname]: cacheInitialState });
|
|
|
|
responseCallback(cacheInitialState);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2021-06-23 11:25:43 +00:00
|
|
|
* @function getTab
|
|
|
|
* @description Retrieves current tab information
|
|
|
|
*
|
|
|
|
* @param {void} [responseCallback]
|
|
|
|
* @returns {Promise<{ id: string, location: string }>} Current tab information
|
2021-06-22 22:01:56 +00:00
|
|
|
*/
|
|
|
|
|
2021-06-23 11:25:43 +00:00
|
|
|
const getTab = (responseCallback) => {
|
|
|
|
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
|
|
|
|
responseCallback({
|
|
|
|
id: tabs[0].id,
|
|
|
|
hostname: new URL(tabs[0].url).hostname,
|
2021-06-22 22:01:56 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @async
|
|
|
|
* @function getList
|
|
|
|
* @description Retrieves selectors list
|
|
|
|
*
|
2021-06-23 11:25:43 +00:00
|
|
|
* @param {void} [responseCallback]
|
2021-06-22 22:01:56 +00:00
|
|
|
* @returns {Promise<{ matches: string[] }>} A selectors list
|
|
|
|
*/
|
|
|
|
|
|
|
|
const getList = async (responseCallback) => {
|
|
|
|
try {
|
|
|
|
const url =
|
|
|
|
"https://raw.githubusercontent.com/wanhose/do-not-consent/master/data/elements.txt";
|
|
|
|
const response = await fetch(url);
|
|
|
|
const data = await response.text();
|
|
|
|
|
|
|
|
if (response.status !== 200) throw new Error();
|
|
|
|
|
2021-06-23 11:25:43 +00:00
|
|
|
responseCallback({ selectors: data.split("\n") });
|
2021-06-22 22:01:56 +00:00
|
|
|
} catch {
|
2021-06-23 11:25:43 +00:00
|
|
|
responseCallback({ selectors: [] });
|
2021-06-22 22:01:56 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-06-23 11:25:43 +00:00
|
|
|
/**
|
|
|
|
* @function updateCache
|
|
|
|
* @description Update cache state
|
|
|
|
*
|
|
|
|
* @param {string} [hostname]
|
|
|
|
* @param {object} [state]
|
|
|
|
*/
|
|
|
|
|
2021-06-27 11:17:03 +00:00
|
|
|
const updateCache = (hostname, state) => {
|
2021-06-23 11:25:43 +00:00
|
|
|
chrome.storage.local.get(null, (cache) => {
|
|
|
|
const current = cache[hostname];
|
|
|
|
|
|
|
|
chrome.storage.local.set({
|
|
|
|
[hostname]: {
|
|
|
|
enabled:
|
|
|
|
typeof state.enabled === "undefined"
|
|
|
|
? current.enabled
|
|
|
|
: state.enabled,
|
|
|
|
matches:
|
|
|
|
typeof state.matches === "undefined"
|
|
|
|
? current.matches
|
|
|
|
: [...new Set([...current.matches, ...state.matches])],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-07-01 13:11:09 +00:00
|
|
|
/**
|
|
|
|
* @function updateState
|
|
|
|
* @description Set an extension state
|
|
|
|
*
|
|
|
|
* @param {string} [tabId]
|
|
|
|
* @param {string} [state]
|
|
|
|
*/
|
|
|
|
|
|
|
|
const updateState = (tabId, state) => {
|
|
|
|
switch (state) {
|
|
|
|
case "loading":
|
2021-07-01 15:53:54 +00:00
|
|
|
chrome.tabs.insertCSS(
|
|
|
|
tabId,
|
|
|
|
{
|
|
|
|
file: "styles/content.css",
|
|
|
|
runAt: "document_start",
|
|
|
|
},
|
|
|
|
() => chrome.runtime.lastError
|
|
|
|
);
|
2021-07-01 13:11:09 +00:00
|
|
|
break;
|
|
|
|
case "ready":
|
2021-07-01 15:53:54 +00:00
|
|
|
chrome.tabs.removeCSS(
|
|
|
|
tabId,
|
|
|
|
{
|
|
|
|
file: "styles/content.css",
|
|
|
|
},
|
|
|
|
() => chrome.runtime.lastError
|
|
|
|
);
|
2021-07-01 13:11:09 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-04-08 21:49:03 +00:00
|
|
|
/**
|
2021-07-05 16:07:37 +00:00
|
|
|
* @description Listens to messages
|
2021-04-08 21:49:03 +00:00
|
|
|
*/
|
|
|
|
|
2021-06-22 22:01:56 +00:00
|
|
|
chrome.runtime.onMessage.addListener((request, sender, responseCallback) => {
|
2021-07-01 15:53:54 +00:00
|
|
|
const hasPermission = !sender.frameId || sender.frameId === 0;
|
2021-07-01 16:55:04 +00:00
|
|
|
let tabId = sender.tab ? sender.tab.id : undefined;
|
|
|
|
|
|
|
|
if (!tabId) {
|
|
|
|
chrome.tabs.query(
|
|
|
|
{ active: true, currentWindow: true },
|
|
|
|
(tabs) => (tabId = tabs[0] ? tabs[0].id : 0)
|
|
|
|
);
|
|
|
|
}
|
2021-07-01 15:53:54 +00:00
|
|
|
|
|
|
|
switch (request.type) {
|
|
|
|
case "DISABLE_ICON":
|
|
|
|
if (hasPermission) disableIcon(tabId);
|
|
|
|
break;
|
|
|
|
case "DISABLE_POPUP":
|
|
|
|
if (hasPermission) disablePopup(tabId);
|
|
|
|
break;
|
|
|
|
case "ENABLE_ICON":
|
|
|
|
if (hasPermission) enableIcon(tabId);
|
|
|
|
break;
|
|
|
|
case "ENABLE_POPUP":
|
|
|
|
if (hasPermission) enablePopup(tabId);
|
|
|
|
break;
|
|
|
|
case "GET_CACHE":
|
|
|
|
getCache(request.hostname, responseCallback);
|
|
|
|
break;
|
|
|
|
case "GET_LIST":
|
|
|
|
getList(responseCallback);
|
|
|
|
break;
|
|
|
|
case "GET_TAB":
|
|
|
|
getTab(responseCallback);
|
|
|
|
break;
|
|
|
|
case "UPDATE_CACHE":
|
|
|
|
updateCache(request.hostname, request.state);
|
|
|
|
break;
|
|
|
|
case "UPDATE_STATE":
|
|
|
|
if (hasPermission) updateState(tabId, request.state);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2021-06-22 22:01:56 +00:00
|
|
|
|
|
|
|
return true;
|
2021-04-08 21:49:03 +00:00
|
|
|
});
|
2021-07-05 16:07:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @description Listens to updates
|
|
|
|
*/
|
|
|
|
|
|
|
|
chrome.runtime.onInstalled.addListener((reason) => {
|
|
|
|
if (reason === chrome.runtime.OnInstalledReason.UPDATE) {
|
|
|
|
chrome.storage.local.clear();
|
|
|
|
}
|
|
|
|
});
|