Skip to content

Commit

Permalink
Make ContextMenus permanent permission to fix issues with menu items …
Browse files Browse the repository at this point in the history
…being non-responding
  • Loading branch information
tfedor committed Sep 15, 2024
1 parent 0e91fe2 commit 007a63b
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 43 deletions.
3 changes: 2 additions & 1 deletion scripts/tools/manifest.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export default {
open_in_tab: true
},
permissions: [
"storage"
"storage",
"contextMenus"
],
host_permissions: [
"*://*.steampowered.com/*",
Expand Down
2 changes: 0 additions & 2 deletions scripts/tools/manifestBuilder.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ export default class ManifestBuilder {

if (browser === "chrome" || browser === "edge") {
this._manifest.permissions.push("offscreen");
this._manifest.optional_permissions.push("contextMenus");
this._manifest.background.service_worker = "js/background.js";
}

Expand All @@ -56,7 +55,6 @@ export default class ManifestBuilder {
}
};

this._manifest.permissions.push("contextMenus");
this._manifest.background.scripts = ["js/background.js"];
}

Expand Down
35 changes: 9 additions & 26 deletions src/js/Background/Modules/ContextMenu/ContextMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
import Localization, {L} from "@Core/Localization/Localization";
import Settings, {SettingsStore} from "@Options/Data/Settings";
import browser, {type Menus} from "webextension-polyfill";
import Permissions from "@Core/Permissions";

export default class ContextMenu {

Expand Down Expand Up @@ -45,28 +44,23 @@ export default class ContextMenu {
]
};

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

// @ts-ignore
return Permissions.when("contextMenus", () => {
if (!browser.contextMenus.onClicked.hasListener(ContextMenu.onClick)) {
browser.contextMenus.onClicked.addListener(ContextMenu.onClick);
}, async () => {
browser.contextMenus.onClicked.removeListener(ContextMenu.onClick);
await browser.contextMenus.removeAll();
});

}
}

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

// @ts-ignore
const url: string|undefined = ContextMenu.queryLinks[info.menuItemId][1];
if (!url) {
const menuItem = ContextMenu.queryLinks[info.menuItemId];
if (!menuItem) {
return;
}

const url: string = menuItem[1];
let query = info.selectionText!.trim();

if (info.menuItemId === "context_steam_keys") {
Expand Down Expand Up @@ -101,27 +95,16 @@ export default class ContextMenu {

/*
* 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
* 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();
if (!await Permissions.contains(["contextMenus"])) {
Settings.context_steam_store = false;
Settings.context_steam_market = false
Settings.context_itad = false
Settings.context_bartervg = false
Settings.context_steamdb = false
Settings.context_steamdb_instant = false
Settings.context_steam_keys = false
return;
}

await browser.contextMenus.removeAll();
return ContextMenu.build();
return this.build();
}
}
4 changes: 0 additions & 4 deletions src/js/Core/Permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ export default class Permissions {
}

static async remove(permissions: Manifest.OptionalPermission[]): Promise<boolean> {
// @ts-ignore
if (permissions.includes("contextMenus")) {
await browser.contextMenus.removeAll();
}
return browser.permissions.remove({permissions});
}

Expand Down
10 changes: 0 additions & 10 deletions src/js/Options/Modules/Options/Settings/ContextMenuOptions.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
} from "@Strings/_strings";
import {L} from "@Core/Localization/Localization";
import Toggle from "../Components/Toggle.svelte";
import Permissions from "@Core/Permissions";
import ContextMenu from "@Background/Modules/ContextMenu/ContextMenu";
type ContextMenuKeys = keyof SettingsSchema & (
Expand All @@ -30,15 +29,6 @@
export let settings: Writable<SettingsSchema>;
async function handleChange(key: ContextMenuKeys, value: boolean): Promise<void> {
if (value) {
const permissions = ["contextMenus"];
const hasPermissions = await Permissions.contains(permissions);
if (!hasPermissions) {
// @ts-expect-error
value = await Permissions.request(permissions);
}
}
$settings[key] = value;
ContextMenu.update();
}
Expand Down

0 comments on commit 007a63b

Please sign in to comment.