-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
moves rest of exported backgroudn contextMenu functions to separate f…
…iles
- Loading branch information
1 parent
6e67105
commit f174f1f
Showing
15 changed files
with
127 additions
and
87 deletions.
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
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
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
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright (C) 2024 PixieBrix, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import { ContextError } from "@/errors/genericErrors"; | ||
import { resolveExtensionInnerDefinitions } from "@/registry/internal"; | ||
import { ContextMenuStarterBrickABC } from "@/starterBricks/contextMenu/contextMenu"; | ||
import { type ContextMenuConfig } from "@/starterBricks/contextMenu/types"; | ||
import { selectEventData } from "@/telemetry/deployments"; | ||
import { | ||
type ModComponentBase, | ||
type ResolvedModComponent, | ||
} from "@/types/modComponentTypes"; | ||
import { expectContext } from "@/utils/expectContext"; | ||
import { allSettled } from "@/utils/promiseUtils"; | ||
import extensionPointRegistry from "@/starterBricks/registry"; | ||
|
||
/** | ||
* Add context menu items to the Chrome context menu on all tabs, in anticipation that on Page Load, the content | ||
* script will register a handler for the item. | ||
* @param extensions the ModComponent to preload. | ||
*/ | ||
export async function preloadContextMenus( | ||
extensions: ModComponentBase[], | ||
): Promise<void> { | ||
expectContext("background"); | ||
const promises = extensions.map(async (definition) => { | ||
const resolved = await resolveExtensionInnerDefinitions(definition); | ||
|
||
const extensionPoint = await extensionPointRegistry.lookup( | ||
resolved.extensionPointId, | ||
); | ||
if (extensionPoint instanceof ContextMenuStarterBrickABC) { | ||
await extensionPoint.registerMenuItem( | ||
definition as unknown as ResolvedModComponent<ContextMenuConfig>, | ||
() => { | ||
throw new ContextError( | ||
"Context menu was preloaded, but no handler was registered", | ||
{ | ||
context: selectEventData(resolved), | ||
}, | ||
); | ||
}, | ||
); | ||
} | ||
}); | ||
await allSettled(promises, { catch: "ignore" }); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright (C) 2024 PixieBrix, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import { makeMenuId } from "@/background/contextMenus/makeMenuId"; | ||
import { type UUID } from "@/types/stringTypes"; | ||
|
||
/** | ||
* Uninstall the contextMenu UI for `extensionId` from browser context menu on all tabs. | ||
* | ||
* Safe to call on non-context menu extension ids. | ||
* | ||
* @returns true if the contextMenu was removed, or false if the contextMenu was not found. | ||
*/ | ||
export async function uninstallContextMenu({ | ||
extensionId, | ||
}: { | ||
extensionId: UUID; | ||
}): Promise<boolean> { | ||
try { | ||
await browser.contextMenus.remove(makeMenuId(extensionId)); | ||
console.debug(`Uninstalled context menu ${extensionId}`); | ||
return true; | ||
} catch (error) { | ||
// Will throw if extensionId doesn't refer to a context menu. The callers don't have an easy way to check the type | ||
// without having to resolve the extensionPointId. So instead we'll just expect some of the calls to fail. | ||
console.debug("Could not uninstall context menu %s", extensionId, { | ||
error, | ||
}); | ||
return false; | ||
} | ||
} |
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
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
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
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
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
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
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
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
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
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