2022-05-12 18:41:49 +00:00
|
|
|
/**
|
|
|
|
* @description API URL
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
|
2022-07-20 08:27:05 +00:00
|
|
|
const apiUrl = 'https://api.cookie-dialog-monster.com/rest/v2';
|
2022-05-12 18:41:49 +00:00
|
|
|
|
2021-11-04 12:31:10 +00:00
|
|
|
/**
|
2022-07-20 08:27:05 +00:00
|
|
|
* @description Initial state
|
2022-05-06 11:17:43 +00:00
|
|
|
* @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
|
|
|
/**
|
2022-07-20 08:27:05 +00:00
|
|
|
* @description Context menu identifier
|
|
|
|
* @type {string}
|
2021-04-08 21:49:03 +00:00
|
|
|
*/
|
|
|
|
|
2022-07-20 08:27:05 +00:00
|
|
|
const reportMenuItemId = 'REPORT';
|
2021-04-08 21:49:03 +00:00
|
|
|
|
2021-06-22 22:01:56 +00:00
|
|
|
/**
|
2022-07-20 08:27:05 +00:00
|
|
|
* @description Refreshes data
|
|
|
|
* @param {void?} callback
|
2021-06-22 22:01:56 +00:00
|
|
|
*/
|
|
|
|
|
2022-07-20 08:27:05 +00:00
|
|
|
const refreshData = (callback) => {
|
|
|
|
fetch(`${apiUrl}/data/`).then((result) => {
|
|
|
|
result.json().then(({ data }) => {
|
|
|
|
chrome.storage.local.set({ data });
|
|
|
|
callback(data);
|
|
|
|
});
|
2021-06-22 22:01:56 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2021-10-05 07:00:07 +00:00
|
|
|
* @async
|
2022-07-20 08:27:05 +00:00
|
|
|
* @description Reports active tab URL
|
|
|
|
* @param {chrome.tabs.Tab} tab
|
2021-10-05 07:00:07 +00:00
|
|
|
*/
|
|
|
|
|
2022-07-20 08:27:05 +00:00
|
|
|
const report = async (tab) => {
|
|
|
|
const version = chrome.runtime.getManifest().version;
|
|
|
|
const body = JSON.stringify({ url: tab?.url, version });
|
|
|
|
const headers = { 'Content-type': 'application/json' };
|
|
|
|
const url = `${apiUrl}/report/`;
|
2022-05-24 21:31:59 +00:00
|
|
|
|
2022-07-20 08:27:05 +00:00
|
|
|
await fetch(url, { body, headers, method: 'POST' });
|
|
|
|
chrome.tabs.sendMessage(tab.id, { type: 'SHOW_REPORT_CONFIRMATION' });
|
2021-10-05 07:00:07 +00:00
|
|
|
};
|
|
|
|
|
2022-05-19 12:17:31 +00:00
|
|
|
/**
|
2022-08-02 10:36:48 +00:00
|
|
|
* @description Listens to context menus clicked
|
2022-05-19 12:17:31 +00:00
|
|
|
*/
|
|
|
|
|
2022-07-20 08:27:05 +00:00
|
|
|
chrome.contextMenus.onClicked.addListener((info, tab) => {
|
|
|
|
switch (info.menuItemId) {
|
|
|
|
case reportMenuItemId:
|
|
|
|
if (tab) report(tab);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2022-05-19 12:17:31 +00:00
|
|
|
}
|
2022-07-20 08:27:05 +00:00
|
|
|
});
|
2022-05-19 12:17:31 +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
|
|
|
*/
|
|
|
|
|
2022-07-20 08:27:05 +00:00
|
|
|
chrome.runtime.onMessage.addListener((message, sender, callback) => {
|
|
|
|
const hostname = message.hostname;
|
2022-05-06 11:17:43 +00:00
|
|
|
const tabId = sender.tab?.id;
|
2021-07-01 16:55:04 +00:00
|
|
|
|
2022-07-20 08:27:05 +00:00
|
|
|
switch (message.type) {
|
2022-05-05 15:19:35 +00:00
|
|
|
case 'DISABLE_ICON':
|
2022-08-02 10:36:48 +00:00
|
|
|
if (tabId) chrome.action.setIcon({ path: '/assets/icons/disabled.png', tabId });
|
2021-07-01 15:53:54 +00:00
|
|
|
break;
|
2022-05-05 15:19:35 +00:00
|
|
|
case 'ENABLE_ICON':
|
2022-08-02 10:36:48 +00:00
|
|
|
if (tabId) chrome.action.setIcon({ path: '/assets/icons/enabled.png', tabId });
|
2021-07-01 15:53:54 +00:00
|
|
|
break;
|
2022-05-05 15:19:35 +00:00
|
|
|
case 'ENABLE_POPUP':
|
2022-08-02 10:36:48 +00:00
|
|
|
if (tabId) chrome.action.setPopup({ popup: '/popup.html', tabId });
|
2021-07-01 15:53:54 +00:00
|
|
|
break;
|
2022-05-19 12:17:31 +00:00
|
|
|
case 'GET_DATA':
|
2022-07-20 08:27:05 +00:00
|
|
|
chrome.storage.local.get('data', ({ data }) => {
|
|
|
|
if (data) callback(data);
|
|
|
|
else refreshData(callback);
|
|
|
|
});
|
2021-07-01 15:53:54 +00:00
|
|
|
break;
|
2022-07-20 08:27:05 +00:00
|
|
|
case 'GET_STATE':
|
|
|
|
// prettier-ignore
|
|
|
|
if (hostname) chrome.storage.local.get(hostname, (state) => callback(state[hostname] ?? initial));
|
2022-05-19 14:50:14 +00:00
|
|
|
break;
|
2022-05-05 15:19:35 +00:00
|
|
|
case 'GET_TAB':
|
2022-07-20 08:27:05 +00:00
|
|
|
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => callback(tabs[0]));
|
2021-07-01 15:53:54 +00:00
|
|
|
break;
|
2022-07-20 08:27:05 +00:00
|
|
|
case 'UPDATE_STATE':
|
|
|
|
if (hostname) chrome.storage.local.set({ [hostname]: message.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
|
|
|
});
|
2022-08-02 10:36:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @description Listens to extension installed
|
|
|
|
*/
|
|
|
|
|
|
|
|
chrome.runtime.onInstalled.addListener(() => {
|
|
|
|
chrome.contextMenus.create({
|
|
|
|
contexts: ['all'],
|
|
|
|
documentUrlPatterns: chrome.runtime.getManifest().content_scripts[0].matches,
|
|
|
|
id: reportMenuItemId,
|
|
|
|
title: chrome.i18n.getMessage('contextMenuText'),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description Listens to first start
|
|
|
|
*/
|
|
|
|
|
|
|
|
chrome.runtime.onStartup.addListener(() => {
|
|
|
|
refreshData();
|
|
|
|
});
|