-
Notifications
You must be signed in to change notification settings - Fork 86
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
tfedor
wants to merge
1
commit into
develop
Choose a base branch
from
improved-context-menu-update
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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, | ||||||
|
@@ -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); | ||||||
|
@@ -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; | ||||||
} | ||||||
|
@@ -83,28 +94,29 @@ export default class ContextMenu { | |||||
|
||||||
for (const [option, entry] of Object.entries(ContextMenu.queryLinks)) { | ||||||
let [locale, query_, enabled] = entry; | ||||||
if (!enabled()) { | ||||||
continue | ||||||
} | ||||||
|
||||||
browser.contextMenus.create({ | ||||||
id: option, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
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() | ||||||
}); | ||||||
} | ||||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.