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-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
|
|
|
*/
|
2023-10-11 07:04:40 +00:00
|
|
|
const extensionMenuItemId = 'CDM-MENU';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description Context menu identifier
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
const reportMenuItemId = 'CDM-REPORT';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description Context menu identifier
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
const settingsMenuItemId = 'CDM-SETTINGS';
|
2021-04-08 21:49:03 +00:00
|
|
|
|
2022-12-01 15:54:15 +00:00
|
|
|
/**
|
|
|
|
* @description A shortcut for chrome.scripting
|
2022-12-01 16:56:15 +00:00
|
|
|
* @type {chrome.scripting}
|
2022-12-01 15:54:15 +00:00
|
|
|
*/
|
|
|
|
const script = chrome.scripting;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description The storage to use
|
|
|
|
* @type {chrome.storage.LocalStorageArea}
|
|
|
|
*/
|
|
|
|
const storage = chrome.storage.local;
|
|
|
|
|
2021-06-22 22:01:56 +00:00
|
|
|
/**
|
2024-02-24 18:13:40 +00:00
|
|
|
* @description Refresh data
|
2022-07-20 08:27:05 +00:00
|
|
|
* @param {void?} callback
|
2021-06-22 22:01:56 +00:00
|
|
|
*/
|
2022-07-20 08:27:05 +00:00
|
|
|
const refreshData = (callback) => {
|
2023-11-08 08:35:26 +00:00
|
|
|
try {
|
|
|
|
fetch(`${apiUrl}/data/`).then((result) => {
|
|
|
|
result.json().then(({ data }) => {
|
2024-02-24 18:13:40 +00:00
|
|
|
chrome.storage.local.set({ data }, suppressLastError);
|
2023-11-08 08:35:26 +00:00
|
|
|
callback?.(data);
|
|
|
|
});
|
2022-07-20 08:27:05 +00:00
|
|
|
});
|
2023-11-08 08:35:26 +00:00
|
|
|
} catch {
|
|
|
|
refreshData(callback);
|
|
|
|
}
|
2021-06-22 22:01:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2021-10-05 07:00:07 +00:00
|
|
|
* @async
|
2024-02-24 18:13:40 +00:00
|
|
|
* @description Report active tab URL
|
2022-10-10 17:52:05 +00:00
|
|
|
* @param {any} message
|
2022-07-20 08:27:05 +00:00
|
|
|
* @param {chrome.tabs.Tab} tab
|
2021-10-05 07:00:07 +00:00
|
|
|
*/
|
2022-10-10 17:52:05 +00:00
|
|
|
const report = async (message, tab) => {
|
2024-02-24 18:13:40 +00:00
|
|
|
try {
|
|
|
|
const reason = message.reason;
|
|
|
|
const userAgent = message.userAgent;
|
|
|
|
const version = chrome.runtime.getManifest().version;
|
|
|
|
const body = JSON.stringify({ reason, url: tab.url, userAgent, version });
|
|
|
|
const headers = { 'Content-type': 'application/json' };
|
|
|
|
const url = `${apiUrl}/report/`;
|
|
|
|
|
|
|
|
await fetch(url, { body, headers, method: 'POST' });
|
|
|
|
} catch {
|
|
|
|
console.error("Can't send report");
|
|
|
|
}
|
2021-10-05 07:00:07 +00:00
|
|
|
};
|
|
|
|
|
2022-05-19 12:17:31 +00:00
|
|
|
/**
|
2024-02-24 18:13:40 +00:00
|
|
|
* @description Supress `chrome.runtime.lastError`
|
2022-05-19 12:17:31 +00:00
|
|
|
*/
|
2024-02-24 18:13:40 +00:00
|
|
|
const suppressLastError = () => void chrome.runtime.lastError;
|
2022-05-19 12:17:31 +00:00
|
|
|
|
2024-02-24 18:13:40 +00:00
|
|
|
/**
|
|
|
|
* @description Listen to context menus clicked
|
|
|
|
*/
|
2022-07-20 08:27:05 +00:00
|
|
|
chrome.contextMenus.onClicked.addListener((info, tab) => {
|
2024-02-24 18:13:40 +00:00
|
|
|
const tabId = tab?.id;
|
|
|
|
|
2022-07-20 08:27:05 +00:00
|
|
|
switch (info.menuItemId) {
|
|
|
|
case reportMenuItemId:
|
2024-02-24 18:13:40 +00:00
|
|
|
if (tabId) chrome.tabs.sendMessage(tabId, { type: 'SHOW_REPORT_DIALOG' }, suppressLastError);
|
2022-07-20 08:27:05 +00:00
|
|
|
break;
|
2023-10-11 07:04:40 +00:00
|
|
|
case settingsMenuItemId:
|
|
|
|
chrome.runtime.openOptionsPage();
|
|
|
|
break;
|
2022-07-20 08:27:05 +00:00
|
|
|
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-12-01 16:56:15 +00:00
|
|
|
const isPage = sender.frameId === 0;
|
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':
|
2023-09-25 16:38:52 +00:00
|
|
|
if (isPage && tabId) {
|
2024-02-24 18:13:40 +00:00
|
|
|
chrome.action.setIcon({ path: '/assets/icons/disabled.png', tabId }, suppressLastError);
|
2024-02-27 18:43:22 +00:00
|
|
|
chrome.action.setBadgeText({ tabId, text: '' });
|
2023-09-25 16:38:52 +00:00
|
|
|
}
|
2021-07-01 15:53:54 +00:00
|
|
|
break;
|
2022-05-05 15:19:35 +00:00
|
|
|
case 'ENABLE_ICON':
|
2023-09-25 16:38:52 +00:00
|
|
|
if (isPage && tabId) {
|
2024-02-24 18:13:40 +00:00
|
|
|
chrome.action.setIcon({ path: '/assets/icons/enabled.png', tabId }, suppressLastError);
|
2023-09-25 16:38:52 +00:00
|
|
|
}
|
2021-07-01 15:53:54 +00:00
|
|
|
break;
|
2022-05-05 15:19:35 +00:00
|
|
|
case 'ENABLE_POPUP':
|
2023-09-25 16:38:52 +00:00
|
|
|
if (isPage && tabId) {
|
2024-02-24 18:13:40 +00:00
|
|
|
chrome.action.setPopup({ popup: '/popup.html', tabId }, suppressLastError);
|
2023-09-25 16:38:52 +00:00
|
|
|
}
|
2021-07-01 15:53:54 +00:00
|
|
|
break;
|
2022-05-19 12:17:31 +00:00
|
|
|
case 'GET_DATA':
|
2023-09-25 16:38:52 +00:00
|
|
|
storage.get('data', ({ data }) => {
|
|
|
|
if (data) {
|
|
|
|
callback(data);
|
|
|
|
} else {
|
|
|
|
refreshData(callback);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
case 'GET_EXCLUSION_LIST':
|
|
|
|
storage.get(null, (exclusions) => {
|
|
|
|
const exclusionList = Object.entries(exclusions || {}).flatMap((x) =>
|
|
|
|
x[0] !== 'data' && !x[1]?.enabled ? [x[0]] : []
|
|
|
|
);
|
|
|
|
callback(exclusionList);
|
|
|
|
});
|
2022-12-01 15:57:38 +00:00
|
|
|
return true;
|
2023-09-25 16:38:52 +00:00
|
|
|
case 'GET_HOSTNAME_STATE':
|
|
|
|
if (hostname) {
|
|
|
|
storage.get(hostname, (state) => {
|
|
|
|
callback(state[hostname] ?? { enabled: true });
|
|
|
|
});
|
|
|
|
}
|
2022-12-01 15:57:38 +00:00
|
|
|
return true;
|
2022-05-05 15:19:35 +00:00
|
|
|
case 'GET_TAB':
|
2023-09-25 16:38:52 +00:00
|
|
|
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
|
|
|
|
callback(tabs[0]);
|
|
|
|
});
|
2022-12-01 15:57:38 +00:00
|
|
|
return true;
|
2022-12-01 15:54:15 +00:00
|
|
|
case 'INSERT_DIALOG_CSS':
|
2023-09-25 16:38:52 +00:00
|
|
|
if (isPage && tabId) {
|
|
|
|
script.insertCSS({ files: ['styles/dialog.css'], target: { tabId } });
|
|
|
|
}
|
2022-12-01 15:54:15 +00:00
|
|
|
break;
|
2022-10-10 17:52:05 +00:00
|
|
|
case 'REPORT':
|
2023-09-25 16:38:52 +00:00
|
|
|
if (tabId) {
|
|
|
|
report(message, sender.tab);
|
|
|
|
}
|
2022-10-10 17:52:05 +00:00
|
|
|
break;
|
2024-02-27 18:43:22 +00:00
|
|
|
case 'SET_BADGE':
|
|
|
|
if (tabId) {
|
|
|
|
chrome.action.setBadgeBackgroundColor({ color: '#6b7280' });
|
|
|
|
chrome.action.setBadgeText({ tabId, text: message.value });
|
|
|
|
}
|
|
|
|
break;
|
2023-09-25 16:38:52 +00:00
|
|
|
case 'SET_HOSTNAME_STATE':
|
|
|
|
if (hostname && message.state.enabled === false) {
|
|
|
|
storage.set({ [hostname]: message.state });
|
|
|
|
} else if (hostname) {
|
|
|
|
storage.remove(hostname);
|
|
|
|
}
|
2021-07-01 15:53:54 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
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(() => {
|
2024-02-24 18:13:40 +00:00
|
|
|
chrome.contextMenus.create(
|
|
|
|
{
|
|
|
|
contexts: ['all'],
|
|
|
|
documentUrlPatterns: chrome.runtime.getManifest().content_scripts[0].matches,
|
|
|
|
id: extensionMenuItemId,
|
|
|
|
title: 'Cookie Dialog Monster',
|
|
|
|
},
|
|
|
|
suppressLastError
|
|
|
|
);
|
|
|
|
chrome.contextMenus.create(
|
|
|
|
{
|
|
|
|
contexts: ['all'],
|
|
|
|
documentUrlPatterns: chrome.runtime.getManifest().content_scripts[0].matches,
|
|
|
|
id: settingsMenuItemId,
|
|
|
|
parentId: extensionMenuItemId,
|
|
|
|
title: chrome.i18n.getMessage('contextMenu_settingsOption'),
|
|
|
|
},
|
|
|
|
suppressLastError
|
|
|
|
);
|
|
|
|
chrome.contextMenus.create(
|
|
|
|
{
|
|
|
|
contexts: ['all'],
|
|
|
|
documentUrlPatterns: chrome.runtime.getManifest().content_scripts[0].matches,
|
|
|
|
id: reportMenuItemId,
|
|
|
|
parentId: extensionMenuItemId,
|
|
|
|
title: chrome.i18n.getMessage('contextMenu_reportOption'),
|
|
|
|
},
|
|
|
|
suppressLastError
|
|
|
|
);
|
2022-08-02 10:36:48 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
2024-02-24 18:13:40 +00:00
|
|
|
* @description Listen to first start
|
2022-08-02 10:36:48 +00:00
|
|
|
*/
|
|
|
|
chrome.runtime.onStartup.addListener(() => {
|
|
|
|
refreshData();
|
|
|
|
});
|