Skip to content

Commit 1e9905b

Browse files
authored
Refactor: use contextMenus.onClicked.addListener() (darkreader#9405)
1 parent b014ac7 commit 1e9905b

3 files changed

Lines changed: 13 additions & 14 deletions

File tree

src/background/extension.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import createCSSFilterStylesheet from '../generators/css-filter';
1515
import {getDynamicThemeFixesFor} from '../generators/dynamic-theme';
1616
import createStaticStylesheet from '../generators/static-theme';
1717
import {createSVGFilterStylesheet, getSVGFilterMatrixValue, getSVGReverseFilterMatrixValue} from '../generators/svg-filter';
18-
import type {ExtensionData, FilterConfig, News, Shortcuts, UserSettings, TabInfo, TabData} from '../definitions';
18+
import type {ExtensionData, FilterConfig, News, Shortcuts, UserSettings, TabInfo, TabData, Command} from '../definitions';
1919
import {isSystemDarkModeEnabled} from '../utils/media-query';
2020
import {isFirefox, isThunderbird} from '../utils/platform';
2121
import {MessageType} from '../utils/message';
@@ -88,7 +88,7 @@ export class Extension {
8888
// As far as we know, this code is never actually run because there
8989
// is no browser UI for removing 'contextMenus' permission.
9090
// This code exists for future-proofing in case browsers ever add such UI.
91-
if (!permissions.permissions.includes('contextMenus')) {
91+
if (!permissions?.permissions?.includes('contextMenus')) {
9292
this.registeredContextMenus = false;
9393
}
9494
});
@@ -244,7 +244,7 @@ export class Extension {
244244
};
245245
}
246246

247-
private onCommandInternal = async (command: string, frameURL?: string) => {
247+
private onCommandInternal = async (command: Command, frameURL?: string) => {
248248
if (this.startBarrier.isPending()) {
249249
await this.startBarrier.entry();
250250
}
@@ -283,9 +283,8 @@ export class Extension {
283283
onCommand = debounce(75, this.onCommandInternal);
284284

285285
private registerContextMenus() {
286-
const onCommandToggle = async () => this.onCommand('toggle');
287-
const onCommandAddSite = async (data: chrome.contextMenus.OnClickData) => this.onCommand('addSite', data.frameUrl);
288-
const onCommandSwitchEngine = async () => this.onCommand('switchEngine');
286+
chrome.contextMenus.onClicked.addListener(async ({menuItemId, frameUrl, pageUrl}) =>
287+
this.onCommand(menuItemId as Command, frameUrl || pageUrl));
289288
chrome.contextMenus.removeAll(() => {
290289
this.registeredContextMenus = false;
291290
chrome.contextMenus.create({
@@ -300,22 +299,19 @@ export class Extension {
300299
const msgAddSite = chrome.i18n.getMessage('toggle_current_site');
301300
const msgSwitchEngine = chrome.i18n.getMessage('theme_generation_mode');
302301
chrome.contextMenus.create({
303-
id: 'DarkReader-toggle',
302+
id: 'toggle',
304303
parentId: 'DarkReader-top',
305304
title: msgToggle || 'Toggle everywhere',
306-
onclick: onCommandToggle,
307305
});
308306
chrome.contextMenus.create({
309-
id: 'DarkReader-addSite',
307+
id: 'addSite',
310308
parentId: 'DarkReader-top',
311309
title: msgAddSite || 'Toggle for current site',
312-
onclick: onCommandAddSite,
313310
});
314311
chrome.contextMenus.create({
315-
id: 'DarkReader-switchEngine',
312+
id: 'switchEngine',
316313
parentId: 'DarkReader-top',
317314
title: msgSwitchEngine || 'Switch engine',
318-
onclick: onCommandSwitchEngine,
319315
});
320316
this.registeredContextMenus = true;
321317
});

src/background/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {Extension} from './extension';
22
import {getHelpURL, UNINSTALL_URL} from '../utils/links';
33
import {canInjectScript} from '../background/utils/extension-api';
4-
import type {ExtensionData, Message, UserSettings} from '../definitions';
4+
import type {Command, ExtensionData, Message, UserSettings} from '../definitions';
55
import {MessageType} from '../utils/message';
66
import {makeChromiumHappy} from './make-chromium-happy';
77
import {logInfo} from '../utils/log';
@@ -11,7 +11,7 @@ const extension = new Extension();
1111
extension.start();
1212
if (chrome.commands) {
1313
// Firefox Android does not support chrome.commands
14-
chrome.commands.onCommand.addListener(async (command) => extension.onCommand(command));
14+
chrome.commands.onCommand.addListener(async (command) => extension.onCommand(command as Command));
1515
}
1616

1717
const welcome = ` /''''\\

src/definitions.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,6 @@ export interface News {
198198
badge?: string;
199199
icon?: string;
200200
}
201+
202+
// These values need to match those in Manifest
203+
export type Command = 'toggle' | 'addSite' | 'switchEngine';

0 commit comments

Comments
 (0)