2022-05-12 18:41:49 +00:00
|
|
|
/**
|
|
|
|
* @description API URL
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
|
2022-05-12 20:27:40 +00:00
|
|
|
const apiUrl = 'https://api.cookie-dialog-monster.com/rest/v1';
|
2022-05-12 18:41:49 +00:00
|
|
|
|
2021-11-04 12:31:10 +00:00
|
|
|
/**
|
2022-05-06 11:17:43 +00:00
|
|
|
* @description Base data URL
|
|
|
|
* @type {string}
|
2021-11-04 12:31:10 +00:00
|
|
|
*/
|
|
|
|
|
2022-05-06 11:17:43 +00:00
|
|
|
const baseDataUrl = 'https://raw.githubusercontent.com/wanhose/cookie-dialog-monster/main/data';
|
2021-11-04 12:31:10 +00:00
|
|
|
|
2021-06-22 22:01:56 +00:00
|
|
|
/**
|
2022-05-06 11:17:43 +00:00
|
|
|
* @description Context menu identifier
|
|
|
|
* @type {string}
|
2021-06-22 22:01:56 +00:00
|
|
|
*/
|
|
|
|
|
2022-05-06 11:17:43 +00:00
|
|
|
const contextMenuId = 'CDM_MENU';
|
2021-06-22 22:01:56 +00:00
|
|
|
|
2021-06-23 11:25:43 +00:00
|
|
|
/**
|
2022-05-06 11:17:43 +00:00
|
|
|
* @description Cache initial state
|
|
|
|
* @type {{ enabled: boolean }}
|
2021-06-23 11:25:43 +00:00
|
|
|
*/
|
|
|
|
|
2022-05-06 11:17:43 +00:00
|
|
|
const initial = { enabled: true };
|
2021-06-23 11:25:43 +00:00
|
|
|
|
2021-04-08 21:49:03 +00:00
|
|
|
/**
|
2021-07-01 15:53:54 +00:00
|
|
|
* @description Disables icon
|
2022-05-06 11:17:43 +00:00
|
|
|
* @param {string} tabId
|
2021-04-08 21:49:03 +00:00
|
|
|
*/
|
|
|
|
|
2022-05-06 11:17:43 +00:00
|
|
|
const disableIcon = (tabId) =>
|
|
|
|
chrome.browserAction.setIcon({ path: 'assets/icons/disabled.png', tabId });
|
2021-04-08 21:49:03 +00:00
|
|
|
|
|
|
|
/**
|
2021-07-01 15:53:54 +00:00
|
|
|
* @description Enables icon
|
2022-05-06 11:17:43 +00:00
|
|
|
* @param {string} tabId
|
2021-04-08 21:49:03 +00:00
|
|
|
*/
|
|
|
|
|
2022-05-06 11:17:43 +00:00
|
|
|
const enableIcon = (tabId) =>
|
|
|
|
chrome.browserAction.setIcon({ path: 'assets/icons/enabled.png', tabId });
|
2021-04-08 21:49:03 +00:00
|
|
|
|
|
|
|
/**
|
2021-07-01 15:53:54 +00:00
|
|
|
* @description Enables popup
|
2022-05-06 11:17:43 +00:00
|
|
|
* @param {string} tabId
|
2021-04-08 21:49:03 +00:00
|
|
|
*/
|
|
|
|
|
2022-05-06 11:17:43 +00:00
|
|
|
const enablePopup = (tabId) => chrome.browserAction.setPopup({ popup: 'popup.html', tabId });
|
2021-04-08 21:49:03 +00:00
|
|
|
|
2021-06-22 22:01:56 +00:00
|
|
|
/**
|
|
|
|
* @description Retrieves cache state
|
2022-05-06 11:17:43 +00:00
|
|
|
* @param {string} hostname
|
|
|
|
* @param {void} callback
|
2022-05-12 17:58:52 +00:00
|
|
|
* @returns {{ enabled: boolean }}
|
2021-06-22 22:01:56 +00:00
|
|
|
*/
|
|
|
|
|
2021-10-28 11:25:02 +00:00
|
|
|
const getCache = (hostname, callback) => {
|
2021-06-22 22:01:56 +00:00
|
|
|
chrome.storage.local.get(null, (store) => {
|
2022-05-06 11:17:43 +00:00
|
|
|
callback(store[hostname] ?? initial);
|
2021-06-22 22:01:56 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2021-10-05 07:00:07 +00:00
|
|
|
* @async
|
2022-05-06 11:17:43 +00:00
|
|
|
* @description Retrieves data from GitHub
|
|
|
|
* @param {string} key
|
|
|
|
* @param {void} callback
|
|
|
|
* @returns {Promise<{ any: string[] }>}
|
2021-06-22 22:01:56 +00:00
|
|
|
*/
|
|
|
|
|
2022-05-06 11:17:43 +00:00
|
|
|
const query = async (key, callback) => {
|
2021-10-05 07:00:07 +00:00
|
|
|
try {
|
2022-05-06 11:17:43 +00:00
|
|
|
const url = `${baseDataUrl}/${key}.txt`;
|
2021-10-05 07:00:07 +00:00
|
|
|
const response = await fetch(url);
|
|
|
|
const data = await response.text();
|
|
|
|
|
|
|
|
if (response.status !== 200) throw new Error();
|
|
|
|
|
2022-05-06 11:17:43 +00:00
|
|
|
callback({ [key]: data.split('\n') });
|
2021-10-05 07:00:07 +00:00
|
|
|
} catch {
|
2022-05-06 11:17:43 +00:00
|
|
|
callback({ [key]: [] });
|
2021-06-22 22:01:56 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-05 07:00:07 +00:00
|
|
|
/**
|
|
|
|
* @description Retrieves current tab information
|
2021-10-28 11:25:02 +00:00
|
|
|
* @param {void} [callback]
|
2021-10-30 12:25:58 +00:00
|
|
|
* @returns {Promise<{ id: string, location: string }>}
|
2021-10-05 07:00:07 +00:00
|
|
|
*/
|
|
|
|
|
2022-05-06 11:17:43 +00:00
|
|
|
const queryTab = (callback) => {
|
2021-10-05 07:00:07 +00:00
|
|
|
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
|
2021-10-28 11:25:02 +00:00
|
|
|
callback({
|
2022-05-17 15:04:50 +00:00
|
|
|
id: tabs[0]?.id,
|
|
|
|
hostname: new URL(tabs[0].url).hostname.split('.').slice(-2).join('.'),
|
2021-10-05 07:00:07 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-11-08 16:11:44 +00:00
|
|
|
/**
|
|
|
|
* @description Reports active tab URL
|
|
|
|
*/
|
|
|
|
|
|
|
|
const report = () => {
|
|
|
|
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
|
|
|
|
const tab = tabs[0];
|
2021-11-10 10:40:26 +00:00
|
|
|
const userAgent = window.navigator.userAgent;
|
|
|
|
const version = chrome.runtime.getManifest().version;
|
2021-11-08 16:11:44 +00:00
|
|
|
|
|
|
|
if (tab) {
|
2022-05-12 18:41:49 +00:00
|
|
|
fetch(`${apiUrl}/report/`, {
|
2021-11-08 16:11:44 +00:00
|
|
|
body: JSON.stringify({
|
2022-05-17 15:04:50 +00:00
|
|
|
html: `<b>Browser:</b> ${userAgent}<br/><b>Site:</b> ${tab.url}<br/> <b>Version:</b> ${version}`,
|
2022-05-05 15:19:35 +00:00
|
|
|
to: 'wanhose.development@gmail.com',
|
|
|
|
subject: 'Cookie Dialog Monster Report',
|
2021-11-08 16:11:44 +00:00
|
|
|
}),
|
|
|
|
headers: {
|
2022-05-05 15:19:35 +00:00
|
|
|
'Content-type': 'application/json',
|
2021-11-08 16:11:44 +00:00
|
|
|
},
|
2022-05-05 15:19:35 +00:00
|
|
|
method: 'POST',
|
2021-11-08 16:11:44 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-06-23 11:25:43 +00:00
|
|
|
/**
|
|
|
|
* @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]: {
|
2022-05-06 11:17:43 +00:00
|
|
|
enabled: typeof state.enabled === 'undefined' ? current.enabled : state.enabled,
|
2021-06-23 11:25:43 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
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-10-28 11:25:02 +00:00
|
|
|
chrome.runtime.onMessage.addListener((request, sender, callback) => {
|
2022-05-06 11:17:43 +00:00
|
|
|
const hostname = request.hostname;
|
|
|
|
const state = request.state;
|
|
|
|
const tabId = sender.tab?.id;
|
2021-07-01 16:55:04 +00:00
|
|
|
|
2021-07-01 15:53:54 +00:00
|
|
|
switch (request.type) {
|
2022-05-05 15:19:35 +00:00
|
|
|
case 'DISABLE_ICON':
|
2022-05-06 11:17:43 +00:00
|
|
|
if (tabId) disableIcon(tabId);
|
2021-07-01 15:53:54 +00:00
|
|
|
break;
|
2022-05-05 15:19:35 +00:00
|
|
|
case 'ENABLE_ICON':
|
2022-05-06 11:17:43 +00:00
|
|
|
if (tabId) enableIcon(tabId);
|
2021-07-01 15:53:54 +00:00
|
|
|
break;
|
2022-05-05 15:19:35 +00:00
|
|
|
case 'ENABLE_POPUP':
|
2022-05-06 11:17:43 +00:00
|
|
|
if (tabId) enablePopup(tabId);
|
2021-07-01 15:53:54 +00:00
|
|
|
break;
|
2022-05-05 15:19:35 +00:00
|
|
|
case 'GET_CACHE':
|
2022-05-06 11:17:43 +00:00
|
|
|
getCache(hostname, callback);
|
2021-07-01 15:53:54 +00:00
|
|
|
break;
|
2022-05-05 15:19:35 +00:00
|
|
|
case 'GET_CLASSES':
|
2022-05-06 11:17:43 +00:00
|
|
|
query('classes', callback);
|
|
|
|
break;
|
|
|
|
case 'GET_SKIPS':
|
|
|
|
query('skips', callback);
|
2021-10-05 07:00:07 +00:00
|
|
|
break;
|
2022-05-05 15:19:35 +00:00
|
|
|
case 'GET_FIXES':
|
2022-05-06 11:17:43 +00:00
|
|
|
query('fixes', callback);
|
2021-11-10 10:17:35 +00:00
|
|
|
break;
|
2022-05-05 15:19:35 +00:00
|
|
|
case 'GET_SELECTORS':
|
2022-05-06 11:17:43 +00:00
|
|
|
query('elements', callback);
|
2021-07-01 15:53:54 +00:00
|
|
|
break;
|
2022-05-05 15:19:35 +00:00
|
|
|
case 'GET_TAB':
|
2022-05-06 11:17:43 +00:00
|
|
|
queryTab(callback);
|
2021-07-01 15:53:54 +00:00
|
|
|
break;
|
2022-05-05 15:19:35 +00:00
|
|
|
case 'UPDATE_CACHE':
|
2022-05-06 11:17:43 +00:00
|
|
|
updateCache(hostname, state);
|
2021-07-01 15:53:54 +00:00
|
|
|
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
|
|
|
|
2021-11-04 12:31:10 +00:00
|
|
|
/**
|
|
|
|
* @description Creates context menu
|
|
|
|
*/
|
|
|
|
|
|
|
|
chrome.contextMenus.create({
|
2022-05-05 15:19:35 +00:00
|
|
|
contexts: ['all'],
|
2021-11-04 12:31:10 +00:00
|
|
|
id: contextMenuId,
|
2022-05-05 15:19:35 +00:00
|
|
|
title: chrome.i18n.getMessage('contextMenuText'),
|
2021-11-04 12:31:10 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description Listens to context menus
|
|
|
|
*/
|
|
|
|
|
2021-11-08 16:11:44 +00:00
|
|
|
chrome.contextMenus.onClicked.addListener((info) => {
|
2021-11-04 12:31:10 +00:00
|
|
|
if (info.menuItemId !== contextMenuId) return;
|
2021-11-08 16:11:44 +00:00
|
|
|
report();
|
2021-07-05 16:07:37 +00:00
|
|
|
});
|