Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Context Menus update process #2050

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 29 additions & 17 deletions src/js/Background/Modules/ContextMenu/ContextMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,21 @@ import {
import Localization, {L} from "@Core/Localization/Localization";
import Settings, {SettingsStore} from "@Options/Data/Settings";
import browser, {type Menus} from "webextension-polyfill";
import type {SettingsSchema} from "@Options/Data/_types";

export type ContextMenuKeys = keyof SettingsSchema & (
"context_steam_store"
| "context_steam_market"
| "context_itad"
| "context_bartervg"
| "context_steamdb"
| "context_steamdb_instant"
| "context_steam_keys"
);

export default class ContextMenu {

private static readonly queryLinks: Record<string, [string, string, () => boolean]> = {
private static readonly queryLinks: Record<ContextMenuKeys, [string, string, () => boolean]> = {
"context_steam_store": [
__options_contextSteamStore, "https://store.steampowered.com/search/?term=__query__",
() => Settings.context_steam_store,
Expand Down Expand Up @@ -45,8 +56,8 @@ export default class ContextMenu {
};

static register(): void {
browser.runtime.onStartup.addListener(ContextMenu.update);
browser.runtime.onInstalled.addListener(ContextMenu.update);
browser.runtime.onStartup.addListener(ContextMenu.build);
browser.runtime.onInstalled.addListener(ContextMenu.build);

if (!browser.contextMenus.onClicked.hasListener(ContextMenu.onClick)) {
browser.contextMenus.onClicked.addListener(ContextMenu.onClick);
Expand All @@ -55,7 +66,7 @@ export default class ContextMenu {

private static onClick(info: Menus.OnClickData): void {

const menuItem = ContextMenu.queryLinks[info.menuItemId];
const menuItem = ContextMenu.queryLinks[info.menuItemId as ContextMenuKeys];
if (!menuItem) {
return;
}
Expand Down Expand Up @@ -83,28 +94,29 @@ export default class ContextMenu {

for (const [option, entry] of Object.entries(ContextMenu.queryLinks)) {
let [locale, query_, enabled] = entry;
Comment on lines 95 to 96
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for (const [option, entry] of Object.entries(ContextMenu.queryLinks)) {
let [locale, query_, enabled] = entry;
for (const [id, menuItem] of Object.entries(ContextMenu.queryLinks)) {
const [locale, query_, enabled] = menuItem;

if (!enabled()) {
continue
}

browser.contextMenus.create({
id: option,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
id: option,
id,

title: L(locale, {"query": "%s"}),
contexts: ["selection"]
contexts: ["selection"],
visible: enabled()
},

/*
* TODO don't recreate the context menu entries on each change, only update
* the affected entry (which should also prevent this error)
* Error when you create an entry with duplicate id
*/
() => browser.runtime.lastError);
}
}

public static async update(): Promise<void> {
await SettingsStore.init();
await browser.contextMenus.removeAll();
return ContextMenu.build();
public static update(id: ContextMenuKeys): void {
const menuItem = this.queryLinks[id];
if (!menuItem) {
return;
}
const [locale, query_, enabled] = menuItem;

browser.contextMenus.update(id, {
title: L(locale, {"query": "%s"}),
contexts: ["selection"],
visible: enabled()
});
}
}
14 changes: 2 additions & 12 deletions src/js/Options/Modules/Options/Settings/ContextMenuOptions.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,13 @@
} from "@Strings/_strings";
import {L} from "@Core/Localization/Localization";
import Toggle from "../Components/Toggle.svelte";
import ContextMenu from "@Background/Modules/ContextMenu/ContextMenu";

type ContextMenuKeys = keyof SettingsSchema & (
"context_steam_store"
| "context_steam_market"
| "context_itad"
| "context_bartervg"
| "context_steamdb"
| "context_steamdb_instant"
| "context_steam_keys"
);
import ContextMenu, {type ContextMenuKeys} from "@Background/Modules/ContextMenu/ContextMenu";

export let settings: Writable<SettingsSchema>;

async function handleChange(key: ContextMenuKeys, value: boolean): Promise<void> {
$settings[key] = value;
ContextMenu.update();
ContextMenu.update(key);
}
</script>

Expand Down