feat(browser-extension): enable report only on supported pages

This commit is contained in:
wanhose 2024-10-10 14:28:09 +02:00
parent 82f2c23549
commit 4490b35f50
2 changed files with 49 additions and 8 deletions

View File

@ -125,7 +125,16 @@ async function getData() {
} }
/** /**
* @description Calculate current hostname * @async
* @description Disable report context menu option
* @returns {Promise<void>}
*/
async function disableReport() {
return browser.contextMenus.update(reportMenuItemId, { enabled: false });
}
/**
* @description Get current hostname
* @param {string} url * @param {string} url
* @returns {string} * @returns {string}
*/ */
@ -133,6 +142,17 @@ function getHostname(url) {
return new URL(url).hostname.split('.').slice(-3).join('.').replace('www.', ''); return new URL(url).hostname.split('.').slice(-3).join('.').replace('www.', '');
} }
/**
* @async
* @description Get current active tab
* @returns {Promise<browser.tabs.Tab>}
*/
async function getTab() {
const tabs = await browser.tabs.query({ active: true, currentWindow: true });
return tabs[0];
}
/** /**
* @async * @async
* @description Get state for the given hostname * @description Get state for the given hostname
@ -222,7 +242,7 @@ async function refreshIssue(hostname) {
/** /**
* @async * @async
* @description Report active tab URL * @description Report given page
* @param {any} message * @param {any} message
* @param {browser.tabs.Tab} tab * @param {browser.tabs.Tab} tab
* @param {void?} callback * @param {void?} callback
@ -284,6 +304,9 @@ browser.runtime.onMessage.addListener((message, sender, callback) => {
const tabId = sender.tab?.id; const tabId = sender.tab?.id;
switch (message.type) { switch (message.type) {
case 'DISABLE_REPORT':
if (isPage && tabId !== undefined) disableReport();
break;
case 'DISABLE_ICON': case 'DISABLE_ICON':
if (isPage && tabId !== undefined) { if (isPage && tabId !== undefined) {
browser.action.setIcon({ path: '/assets/icons/off.png', tabId }, suppressLastError); browser.action.setIcon({ path: '/assets/icons/off.png', tabId }, suppressLastError);
@ -300,6 +323,11 @@ browser.runtime.onMessage.addListener((message, sender, callback) => {
browser.action.setPopup({ popup: '/popup.html', tabId }, suppressLastError); browser.action.setPopup({ popup: '/popup.html', tabId }, suppressLastError);
} }
break; break;
case 'ENABLE_REPORT':
if (isPage && tabId !== undefined) {
browser.contextMenus.update(reportMenuItemId, { enabled: true });
}
break;
case 'GET_DATA': case 'GET_DATA':
getData().then(callback); getData().then(callback);
return true; return true;
@ -318,9 +346,7 @@ browser.runtime.onMessage.addListener((message, sender, callback) => {
} }
break; break;
case 'GET_TAB': case 'GET_TAB':
browser.tabs.query({ active: true, currentWindow: true }, (tabs) => { getTab().then(callback);
callback(tabs[0]);
});
return true; return true;
case 'REFRESH_DATA': case 'REFRESH_DATA':
refreshData().then(callback); refreshData().then(callback);
@ -328,6 +354,7 @@ browser.runtime.onMessage.addListener((message, sender, callback) => {
case 'REPORT': case 'REPORT':
report(message).then(callback); report(message).then(callback);
return true; return true;
case 'UPDATE_BADGE': case 'UPDATE_BADGE':
if (isPage && tabId !== undefined) { if (isPage && tabId !== undefined) {
browser.action.setBadgeBackgroundColor({ color: '#6b7280' }); browser.action.setBadgeBackgroundColor({ color: '#6b7280' });
@ -371,6 +398,7 @@ browser.runtime.onInstalled.addListener((details) => {
{ {
contexts: ['all'], contexts: ['all'],
documentUrlPatterns, documentUrlPatterns,
enabled: false,
id: reportMenuItemId, id: reportMenuItemId,
parentId: extensionMenuItemId, parentId: extensionMenuItemId,
title: browser.i18n.getMessage('contextMenu_reportOption'), title: browser.i18n.getMessage('contextMenu_reportOption'),
@ -397,6 +425,13 @@ browser.runtime.onStartup.addListener(() => {
refreshData(); refreshData();
}); });
/**
* @description Listen to tab changes
*/
browser.tabs.onActivated.addListener(() => {
disableReport();
});
/** /**
* @description Listen to the moment before a request is made to apply the rules * @description Listen to the moment before a request is made to apply the rules
* @returns {Promise<void>} * @returns {Promise<void>}

View File

@ -425,11 +425,13 @@ async function setUp(params = {}) {
skips = data?.skips ?? skips; skips = data?.skips ?? skips;
tokens = data?.tokens ?? tokens; tokens = data?.tokens ?? tokens;
dispatch({ type: 'ENABLE_REPORT' });
dispatch({ hostname, type: 'ENABLE_ICON' }); dispatch({ hostname, type: 'ENABLE_ICON' });
dispatch({ type: 'UPDATE_BADGE', value: actions.size }); dispatch({ type: 'UPDATE_BADGE', value: actions.size });
observer.observe(document.body ?? document.documentElement, options); observer.observe(document.body ?? document.documentElement, options);
if (!params.skipRunFn) run({ containers: tokens.containers }); if (!params.skipRunFn) run({ containers: tokens.containers });
} else { } else {
dispatch({ type: 'DISABLE_REPORT' });
dispatch({ type: 'DISABLE_ICON' }); dispatch({ type: 'DISABLE_ICON' });
dispatch({ type: 'UPDATE_BADGE', value: actions.size }); dispatch({ type: 'UPDATE_BADGE', value: actions.size });
observer.disconnect(); observer.disconnect();
@ -495,9 +497,13 @@ window.addEventListener('pageshow', async (event) => {
* @returns {void} * @returns {void}
*/ */
window.addEventListener('visibilitychange', async () => { window.addEventListener('visibilitychange', async () => {
if (document.visibilityState === 'visible' && !initiallyVisible) { if (document.visibilityState === 'visible') {
initiallyVisible = true; if (!initiallyVisible) {
await setUp(); initiallyVisible = true;
await setUp();
}
dispatch({ type: state.on ? 'ENABLE_REPORT' : 'DISABLE_REPORT' });
} }
}); });