Skip to content

Commit

Permalink
Limit danger badge only for SERP tabs & minor perf optimization (#1477)
Browse files Browse the repository at this point in the history
  • Loading branch information
smalluban authored Feb 14, 2024
1 parent 7718b76 commit 04f8211
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
13 changes: 8 additions & 5 deletions extension-manifest-v3/src/background/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { order } from '@ghostery/ui/categories';
import DailyStats from '/store/daily-stats.js';
import Options, { observe } from '/store/options.js';

import { shouldShowOperaSerpAlert } from '/notifications/opera-serp.js';
import { shouldSetDangerBadgeForTabId } from '/notifications/opera-serp.js';
import AutoSyncingMap from '/utils/map.js';

import Request from './utils/request.js';
Expand All @@ -38,10 +38,7 @@ function setBadgeColor(color = '#3f4146' /* gray-600 */) {
}

observe('terms', async (terms) => {
if (
!terms ||
(__PLATFORM__ === 'opera' && (await shouldShowOperaSerpAlert()))
) {
if (!terms) {
await chromeAction.setBadgeText({ text: '!' });
setBadgeColor('#f13436' /* danger-500 */);
} else {
Expand All @@ -53,6 +50,12 @@ observe('terms', async (terms) => {
async function refreshIcon(tabId) {
const options = await store.resolve(Options);

if (options.terms && __PLATFORM__ === 'opera') {
shouldSetDangerBadgeForTabId(tabId).then((danger) => {
setBadgeColor(danger ? '#f13436' /* danger-500 */ : undefined);
});
}

const stats = tabStats.get(tabId);
if (!stats) return;

Expand Down
30 changes: 28 additions & 2 deletions extension-manifest-v3/src/notifications/opera-serp.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,34 @@ export async function showOperaSerpNotification(tabId) {
}

export async function shouldShowOperaSerpAlert() {
if (await isSerpSupported()) return false;
const options = await store.resolve(Options);
if (options.onboarding.serpShown < NOTIFICATION_SHOW_LIMIT) {
if (await isSerpSupported()) {
store.set(options, {
onboarding: { serpShown: NOTIFICATION_SHOW_LIMIT },
});
return false;
}

return true;
}

return false;
}

export async function shouldSetDangerBadgeForTabId(tabId) {
const options = await store.resolve(Options);
return options.onboarding.serpShown < NOTIFICATION_SHOW_LIMIT;

if (options.onboarding.serpShown < NOTIFICATION_SHOW_LIMIT) {
try {
await chrome.scripting.insertCSS({
target: { tabId },
css: '',
});

return false;
} catch (e) {
return true;
}
}
}

0 comments on commit 04f8211

Please sign in to comment.