2021-11-04 12:31:10 +00:00
|
|
|
/**
|
|
|
|
* @description Context menu identifier
|
|
|
|
*/
|
|
|
|
|
2022-05-05 15:19:35 +00:00
|
|
|
const contextMenuId = 'CDM_MENU';
|
2021-11-04 12:31:10 +00:00
|
|
|
|
2021-06-22 22:01:56 +00:00
|
|
|
/**
|
|
|
|
* @description Cache initial state
|
2021-10-30 12:25:58 +00:00
|
|
|
* @type {{ enabled: boolean }}
|
2021-06-22 22:01:56 +00:00
|
|
|
*/
|
|
|
|
|
2021-10-28 11:25:02 +00:00
|
|
|
const initial = {
|
2021-06-22 22:01:56 +00:00
|
|
|
enabled: true,
|
|
|
|
};
|
|
|
|
|
2021-06-23 11:25:43 +00:00
|
|
|
/**
|
|
|
|
* @description Check cache validity
|
|
|
|
* @param {object} [cache]
|
|
|
|
*/
|
|
|
|
|
2022-05-05 15:19:35 +00:00
|
|
|
const check = (cache) => typeof cache.enabled === 'boolean';
|
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
|
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({
|
2022-05-05 15:19:35 +00:00
|
|
|
path: 'assets/icons/disabled.png',
|
2021-07-01 15:53:54 +00:00
|
|
|
tabId,
|
|
|
|
});
|
2021-04-08 21:49:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
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({
|
2022-05-05 15:19:35 +00:00
|
|
|
popup: '',
|
2021-07-01 15:53:54 +00:00
|
|
|
tabId,
|
|
|
|
});
|
2021-04-08 21:49:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
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({
|
2022-05-05 15:19:35 +00:00
|
|
|
path: 'assets/icons/enabled.png',
|
2021-07-01 15:53:54 +00:00
|
|
|
tabId,
|
|
|
|
});
|
2021-04-08 21:49:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
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({
|
2022-05-05 15:19:35 +00:00
|
|
|
popup: 'popup.html',
|
2021-07-01 15:53:54 +00:00
|
|
|
tabId,
|
|
|
|
});
|
2021-04-08 21:49:03 +00:00
|
|
|
};
|
|
|
|
|
2021-06-22 22:01:56 +00:00
|
|
|
/**
|
|
|
|
* @description Retrieves cache state
|
2021-06-23 11:25:43 +00:00
|
|
|
* @param {string} [hostname]
|
2021-10-28 11:25:02 +00:00
|
|
|
* @param {void} [callback]
|
2021-10-30 12:25:58 +00:00
|
|
|
* @returns {Promise<{ 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) => {
|
2021-06-23 11:25:43 +00:00
|
|
|
try {
|
|
|
|
const cache = store[hostname];
|
|
|
|
|
2021-10-28 11:25:02 +00:00
|
|
|
if (!check(cache)) throw new Error();
|
2021-06-22 22:01:56 +00:00
|
|
|
|
2021-10-28 11:25:02 +00:00
|
|
|
callback(cache);
|
2021-06-23 11:25:43 +00:00
|
|
|
} catch {
|
2021-10-28 11:25:02 +00:00
|
|
|
chrome.storage.local.set({ [hostname]: initial });
|
|
|
|
callback(initial);
|
2021-06-22 22:01:56 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2021-10-05 07:00:07 +00:00
|
|
|
* @async
|
|
|
|
* @description Retrieves a selectors list
|
2021-10-28 11:25:02 +00:00
|
|
|
* @param {void} [callback]
|
2021-10-30 12:25:58 +00:00
|
|
|
* @returns {Promise<{ classes: string[] }>}
|
2021-06-22 22:01:56 +00:00
|
|
|
*/
|
|
|
|
|
2021-10-28 11:25:02 +00:00
|
|
|
const getClasses = async (callback) => {
|
2021-10-05 07:00:07 +00:00
|
|
|
try {
|
|
|
|
const url =
|
2022-05-05 15:19:35 +00:00
|
|
|
'https://raw.githubusercontent.com/wanhose/cookie-dialog-monster/master/data/classes.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-05 15:19:35 +00:00
|
|
|
callback({ classes: data.split('\n') });
|
2021-10-05 07:00:07 +00:00
|
|
|
} catch {
|
2021-10-28 11:25:02 +00:00
|
|
|
callback({ classes: [] });
|
2021-10-05 07:00:07 +00:00
|
|
|
}
|
2021-06-22 22:01:56 +00:00
|
|
|
};
|
|
|
|
|
2021-11-10 10:17:35 +00:00
|
|
|
/**
|
|
|
|
* @async
|
|
|
|
* @description Retrieves a selectors list
|
|
|
|
* @param {void} [callback]
|
|
|
|
* @returns {Promise<{ classes: string[] }>}
|
|
|
|
*/
|
|
|
|
|
|
|
|
const getFixes = async (callback) => {
|
|
|
|
try {
|
|
|
|
const url =
|
2022-05-05 15:19:35 +00:00
|
|
|
'https://raw.githubusercontent.com/wanhose/cookie-dialog-monster/master/data/fixes.txt';
|
2021-11-10 10:17:35 +00:00
|
|
|
const response = await fetch(url);
|
|
|
|
const data = await response.text();
|
|
|
|
|
|
|
|
if (response.status !== 200) throw new Error();
|
|
|
|
|
2022-05-05 15:19:35 +00:00
|
|
|
callback({ fixes: data.split('\n') });
|
2021-11-10 10:17:35 +00:00
|
|
|
} catch {
|
|
|
|
callback({ fixes: [] });
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-06-22 22:01:56 +00:00
|
|
|
/**
|
|
|
|
* @async
|
2021-10-05 07:00:07 +00:00
|
|
|
* @description Retrieves a selectors list
|
2021-10-28 11:25:02 +00:00
|
|
|
* @param {void} [callback]
|
2021-10-30 12:25:58 +00:00
|
|
|
* @returns {Promise<{ selectors: string }>}
|
2021-06-22 22:01:56 +00:00
|
|
|
*/
|
|
|
|
|
2021-10-28 11:25:02 +00:00
|
|
|
const getSelectors = async (callback) => {
|
2021-06-22 22:01:56 +00:00
|
|
|
try {
|
|
|
|
const url =
|
2022-05-05 15:19:35 +00:00
|
|
|
'https://raw.githubusercontent.com/wanhose/cookie-dialog-monster/master/data/elements.txt';
|
2021-06-22 22:01:56 +00:00
|
|
|
const response = await fetch(url);
|
|
|
|
const data = await response.text();
|
|
|
|
|
|
|
|
if (response.status !== 200) throw new Error();
|
|
|
|
|
2022-05-05 15:19:35 +00:00
|
|
|
callback({ selectors: data.split('\n') });
|
2021-06-22 22:01:56 +00:00
|
|
|
} catch {
|
2021-10-28 11:25:02 +00:00
|
|
|
callback({ selectors: [] });
|
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
|
|
|
*/
|
|
|
|
|
2021-10-28 11:25:02 +00:00
|
|
|
const getTab = (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({
|
2021-10-05 07:00:07 +00:00
|
|
|
id: tabs[0].id,
|
|
|
|
hostname: new URL(tabs[0].url).hostname,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
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-05 15:19:35 +00:00
|
|
|
fetch('https://cdm-report-service.herokuapp.com/rest/v1/report/', {
|
2021-11-08 16:11:44 +00:00
|
|
|
body: JSON.stringify({
|
2021-11-10 10:40:26 +00:00
|
|
|
text: `There's a problem with ${tab.url} using ${userAgent} in CDM ${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]: {
|
|
|
|
enabled:
|
2022-05-05 15:19:35 +00:00
|
|
|
typeof state.enabled === 'undefined'
|
2021-06-23 11:25:43 +00:00
|
|
|
? current.enabled
|
|
|
|
: state.enabled,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
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) => {
|
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;
|
|
|
|
|
2021-07-01 15:53:54 +00:00
|
|
|
switch (request.type) {
|
2022-05-05 15:19:35 +00:00
|
|
|
case 'DISABLE_ICON':
|
2021-11-10 13:07:18 +00:00
|
|
|
if (hasPermission && tabId) disableIcon(tabId);
|
2021-07-01 15:53:54 +00:00
|
|
|
break;
|
2022-05-05 15:19:35 +00:00
|
|
|
case 'DISABLE_POPUP':
|
2021-11-10 13:07:18 +00:00
|
|
|
if (hasPermission && tabId) disablePopup(tabId);
|
2021-07-01 15:53:54 +00:00
|
|
|
break;
|
2022-05-05 15:19:35 +00:00
|
|
|
case 'ENABLE_ICON':
|
2021-11-10 13:07:18 +00:00
|
|
|
if (hasPermission && tabId) enableIcon(tabId);
|
2021-07-01 15:53:54 +00:00
|
|
|
break;
|
2022-05-05 15:19:35 +00:00
|
|
|
case 'ENABLE_POPUP':
|
2021-11-10 13:07:18 +00:00
|
|
|
if (hasPermission && tabId) enablePopup(tabId);
|
2021-07-01 15:53:54 +00:00
|
|
|
break;
|
2022-05-05 15:19:35 +00:00
|
|
|
case 'GET_CACHE':
|
2021-10-28 11:25:02 +00:00
|
|
|
getCache(request.hostname, callback);
|
2021-07-01 15:53:54 +00:00
|
|
|
break;
|
2022-05-05 15:19:35 +00:00
|
|
|
case 'GET_CLASSES':
|
2021-10-28 11:25:02 +00:00
|
|
|
getClasses(callback);
|
2021-10-05 07:00:07 +00:00
|
|
|
break;
|
2022-05-05 15:19:35 +00:00
|
|
|
case 'GET_FIXES':
|
2021-11-10 10:17:35 +00:00
|
|
|
getFixes(callback);
|
|
|
|
break;
|
2022-05-05 15:19:35 +00:00
|
|
|
case 'GET_SELECTORS':
|
2021-10-28 11:25:02 +00:00
|
|
|
getSelectors(callback);
|
2021-07-01 15:53:54 +00:00
|
|
|
break;
|
2022-05-05 15:19:35 +00:00
|
|
|
case 'GET_TAB':
|
2021-10-28 11:25:02 +00:00
|
|
|
getTab(callback);
|
2021-07-01 15:53:54 +00:00
|
|
|
break;
|
2022-05-05 15:19:35 +00:00
|
|
|
case 'UPDATE_CACHE':
|
2021-07-01 15:53:54 +00:00
|
|
|
updateCache(request.hostname, 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
|
|
|
|
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
|
|
|
});
|