adnbn
Version:
Addon Bone - Cross-browser web extension framework with shared code base
469 lines (462 loc) • 21 kB
TypeScript
import { FC, ReactNode } from 'react';
import { Optional, Required } from 'utility-types';
import { Mode } from './types/app.js';
import { Browser } from './types/browser.js';
import { Awaiter, PickNonFunctionProperties } from './types/helpers.js';
import { Language } from './types/locale.js';
type ExecutionWorld = chrome.scripting.ExecutionWorld;
type RunAt = chrome.extensionTypes.RunAt;
declare const ContentScriptMatches: string[];
declare enum ContentScriptDeclarative {
Required = "required",
Optional = "optional"
}
interface ContentScriptConfig {
matches?: string[];
/**
* See https://developer.chrome.com/docs/extensions/mv3/content_scripts/
* @default []
*/
excludeMatches?: string[];
/**
* See https://developer.chrome.com/docs/extensions/mv3/content_scripts/
* @default []
*/
includeGlobs?: string[];
/**
* See https://developer.chrome.com/docs/extensions/mv3/content_scripts/
* @default []
*/
excludeGlobs?: string[];
/**
* See https://developer.chrome.com/docs/extensions/mv3/content_scripts/
* @default false
*/
allFrames?: boolean;
/**
* See https://developer.chrome.com/docs/extensions/mv3/content_scripts/
* @default "documentIdle"
*/
runAt?: RunAt;
/**
* See https://developer.chrome.com/docs/extensions/develop/concepts/content-scripts#isolated_world
*/
world?: ExecutionWorld;
/**
* See https://developer.chrome.com/docs/extensions/mv3/content_scripts/
* @default false
*/
matchAboutBlank?: boolean;
/**
* See https://developer.chrome.com/docs/extensions/mv3/content_scripts/
* @default false
*/
matchOriginAsFallback?: boolean;
/**
* Whether this content script requires explicit host permission declaration in the extension manifest.
*
* Accepted values:
* - ContentScriptDeclarative.Required — adds the corresponding host patterns to manifest.host_permissions.
* - ContentScriptDeclarative.Optional — adds the corresponding host patterns to manifest.optional_host_permissions.
*
* Backward compatibility:
* - true ≡ ContentScriptDeclarative.Required
* - false ≡ undefined (as if the value is not set)
*
* @see https://developer.chrome.com/docs/extensions/mv3/declare_permissions/
*
* @default undefined
*/
declarative?: boolean | `${ContentScriptDeclarative}` | ContentScriptDeclarative;
}
type ContentScriptOptions = ContentScriptConfig & EntrypointOptions;
type ContentScriptEntrypointOptions = Partial<ContentScriptOptions>;
declare enum ContentScriptAppend {
Last = "last",
First = "first",
Replace = "replace",
Before = "before",
After = "after"
}
type ContentScriptMountFunction = (anchor: Element, container: Element) => void | (() => void);
interface ContentScriptMount {
mount(): boolean | undefined | void;
unmount(): boolean | undefined | void;
}
interface ContentScriptProps extends ContentScriptEntrypointOptions {
anchor: Element;
}
type ContentScriptAnchor = string | Element | null | undefined;
type ContentScriptAnchorGetter = () => Awaiter<ContentScriptAnchor>;
type ContentScriptAnchorResolver = () => Awaiter<Element[]>;
type ContentScriptRenderReactComponent = FC<ContentScriptProps>;
type ContentScriptRenderValue = Element | ReactNode | ContentScriptRenderReactComponent;
type ContentScriptRenderHandler = (props: ContentScriptProps) => Awaiter<undefined | ContentScriptRenderValue>;
type ContentScriptContainerTag = Exclude<keyof HTMLElementTagNameMap, "html" | "body">;
type ContentScriptContainerOptions = {
[Tag in ContentScriptContainerTag]: {
tagName: Tag;
} & Exclude<Optional<PickNonFunctionProperties<HTMLElementTagNameMap[Tag]>>, "id">;
}[ContentScriptContainerTag];
type ContentScriptContainerFactory = (props: ContentScriptProps) => Awaiter<Element | ContentScriptContainerTag | ContentScriptContainerOptions>;
type ContentScriptContainerCreator = (props: ContentScriptProps) => Awaiter<Element>;
type ContentScriptWatchStrategy = (update: () => void, context: ContentScriptContext) => () => void;
declare enum ContentScriptEvent {
Mount = "mount",
Unmount = "unmount",
Add = "add",
Remove = "remove"
}
type ContentScriptEventCallback = (event: ContentScriptEvent, node: ContentScriptNode) => void;
interface ContentScriptEventEmitter {
on(callback: ContentScriptEventCallback): void;
off(callback: ContentScriptEventCallback): void;
emit(event: ContentScriptEvent, node: ContentScriptNode): void;
emitMount(node: ContentScriptNode): void;
emitUnmount(node: ContentScriptNode): void;
emitAdd(node: ContentScriptNode): void;
emitRemove(node: ContentScriptNode): void;
removeAllListeners(): void;
listenerCount(): number;
hasListeners(): boolean;
}
interface ContentScriptContext extends ContentScriptMount {
nodes: ReadonlySet<ContentScriptNode>;
/**
* Registers a callback function that will be invoked when a specific content script context event occurs.
*
* @param {ContentScriptEventCallback} callback - The function to be executed when the event is triggered. Receives event-related data as its argument.
* @return {Function} A function that can be called to unsubscribe the callback from the event.
*/
watch(callback: ContentScriptEventCallback): () => void;
/**
* Stops watching for changes or events that were previously being observed.
* Unsubscribes from all event listeners that were registered through the watch method.
*
* @return {void} No return value.
*/
unwatch(): void;
}
type ContentScriptMainFunction = (context: ContentScriptContext, options: ContentScriptOptions) => Awaiter<void>;
interface ContentScriptNode extends ContentScriptMount {
anchor: Element;
container?: Element;
}
type ContentScriptNodeSet = Set<ContentScriptNode>;
interface ContentScriptDefinition extends ContentScriptEntrypointOptions {
anchor?: ContentScriptAnchor | ContentScriptAnchorGetter;
mount?: ContentScriptMountFunction;
render?: ContentScriptRenderValue | ContentScriptRenderHandler;
container?: ContentScriptContainerTag | ContentScriptContainerOptions | ContentScriptContainerFactory;
watch?: true | ContentScriptWatchStrategy;
main?: ContentScriptMainFunction;
}
interface ContentScriptResolvedDefinition extends Omit<ContentScriptDefinition, "anchor" | "mount" | "container" | "render" | "watch"> {
anchor: ContentScriptAnchorResolver;
mount: ContentScriptMountFunction;
render?: ContentScriptRenderHandler;
container: ContentScriptContainerCreator;
watch: ContentScriptWatchStrategy;
}
interface ContentScriptAppendDefinition extends Omit<ContentScriptDefinition, "mount"> {
append?: ContentScriptAppend;
}
interface ContentScriptBuilder extends EntrypointBuilder {
getContext(): ContentScriptContext;
}
type Tab = chrome.tabs.Tab;
declare const CommandExecuteActionName = "_execute_action";
interface CommandConfig extends BackgroundConfig {
name: string;
description?: string;
global?: boolean;
defaultKey?: string;
windowsKey?: string;
macKey?: string;
chromeosKey?: string;
linuxKey?: string;
}
type CommandOptions = CommandConfig & EntrypointOptions;
type CommandEntrypointOptions = Partial<CommandOptions>;
type CommandExecute = (tab: Tab | undefined, options: CommandOptions) => Awaiter<void>;
interface CommandDefinition extends CommandEntrypointOptions {
execute: CommandExecute;
}
type ExecuteActionCommandDefinition = Omit<CommandDefinition, "name">;
type CommandUnresolvedDefinition = Partial<CommandDefinition>;
type CommandResolvedDefinition = Required<CommandDefinition, "name" | "execute">;
type CommandBuilder = EntrypointBuilder;
type ManifestCommon = chrome.runtime.Manifest;
type ManifestBase = chrome.runtime.ManifestBase;
type ManifestPermission = chrome.runtime.ManifestPermissions;
type ManifestOptionalPermission = chrome.runtime.ManifestOptionalPermissions;
declare const ManifestMatchSchemes: ReadonlySet<string>;
declare const ManifestSpecialSchemes: ReadonlySet<string>;
type ManifestFixed<T extends ManifestBase> = Omit<T, "manifest_version"> & {
manifest_version: ManifestVersion;
};
interface ManifestUnstable {
permissions?: (ManifestPermission | (string & Record<never, never>))[];
web_accessible_resources?: string[] | chrome.runtime.ManifestV3["web_accessible_resources"];
}
type ManifestVersion = 2 | 3;
declare enum ManifestIncognito {
Spanning = "spanning",
Split = "split",
NotAllowed = "not_allowed"
}
type ManifestIncognitoValue = ManifestIncognito | `${ManifestIncognito}`;
type CoreManifest = ManifestFixed<ManifestBase>;
type ChromeManifest = ManifestFixed<ManifestCommon>;
type FirefoxManifest = ChromeManifest & {
action?: chrome.runtime.ManifestV3["action"] & {
browser_style?: boolean;
};
browser_action?: chrome.runtime.ManifestV2["browser_action"] & {
browser_style?: boolean;
};
page_action?: chrome.runtime.ManifestV2["page_action"] & {
browser_style?: boolean;
};
browser_specific_settings?: {
gecko?: {
id?: string;
strict_min_version?: string;
strict_max_version?: string;
update_url?: string;
};
gecko_android?: {
strict_min_version?: string;
strict_max_version?: string;
};
};
} & ManifestUnstable;
type SafariManifest = ChromeManifest & {
browser_specific_settings?: {
safari?: {
strict_min_version?: string;
strict_max_version?: string;
};
};
} & ManifestUnstable;
type Manifest = ChromeManifest | FirefoxManifest | SafariManifest;
interface ManifestBuilder<T extends CoreManifest = Manifest> {
setName(name: string): this;
setShortName(shortName?: string): this;
setDescription(description?: string): this;
setEmail(email?: string): this;
setAuthor(author?: string): this;
setHomepage(homepage?: string): this;
setVersion(version?: string): this;
setIncognito(incognito?: ManifestIncognitoValue): this;
setMinimumVersion(minimumVersion?: string): this;
setLocale(lang?: Language): this;
setIcons(icons?: ManifestIcons): this;
setIcon(icon?: string): this;
setBackground(background?: ManifestBackground): this;
setCommands(commands?: ManifestCommands): this;
setContentScripts(contentScripts?: ManifestContentScripts): this;
setPopup(popup?: ManifestPopup): this;
setSidebar(sidebar?: ManifestSidebar): this;
setDependencies(dependencies: ManifestDependencies): this;
addPermission(permission: ManifestPermission): this;
setPermissions(permissions: ManifestPermissions$1): this;
appendPermissions(permissions: ManifestPermissions$1): this;
addOptionalPermission(permission: ManifestOptionalPermission): this;
setOptionalPermissions(permissions: ManifestOptionalPermissions$1): this;
appendOptionalPermissions(permissions: ManifestOptionalPermissions$1): this;
addHostPermission(permission: string): this;
setHostPermissions(permissions: ManifestHostPermissions): this;
appendHostPermissions(permissions: ManifestHostPermissions): this;
addOptionalHostPermission(permission: string): this;
setOptionalHostPermissions(permissions: ManifestHostPermissions): this;
appendOptionalHostPermissions(permissions: ManifestHostPermissions): this;
setManifestAccessibleResource(accessibleResources: ManifestAccessibleResources): this;
appendAccessibleResources(accessibleResources: ManifestAccessibleResources): this;
addAccessibleResource(accessibleResource: ManifestAccessibleResource): this;
getWebAccessibleResources(): ManifestAccessibleResource[];
get(): T;
}
type Entry = string;
interface ManifestEntry {
entry: Entry;
}
type ManifestBackground = ManifestEntry & BackgroundConfig;
type ManifestContentScript = ManifestEntry & ContentScriptConfig;
type ManifestContentScripts = Set<ManifestContentScript>;
type ManifestCommand = CommandConfig;
type ManifestCommands = Set<ManifestCommand>;
interface ManifestPopup {
/**
* Represents the group name of an icon that can be used in UI elements.
* This property is optional and, if defined, should typically refer to a string
* that matches the name of an icon resource available in the application or icon library.
*/
icon?: string;
/**
* Represents an optional title or name or locale key that can be assigned to an entity.
*/
title?: string;
/**
* Represents an optional HTML path.
*/
path?: string;
}
interface ManifestSidebar {
/**
* Represents the group name of an icon that can be used in UI elements.
* This property is optional and, if defined, should typically refer to a string
* that matches the name of an icon resource available in the application or icon library.
*/
icon?: string;
/**
* Represents an optional title or name or locale key that can be assigned to an entity.
*/
title?: string;
/**
* Represents an optional HTML path.
*/
path?: string;
}
interface ManifestAccessibleResource {
resources: string[];
matches?: string[];
extensionIds?: string[];
useDynamicUrl?: boolean;
}
type ManifestAccessibleResources = Set<ManifestAccessibleResource>;
interface ManifestDependency {
js: Set<string>;
css: Set<string>;
assets: Set<string>;
}
type ManifestIcon = Map<number, string>;
type ManifestIcons = Map<string, ManifestIcon>;
type ManifestDependencies = Map<Entry, ManifestDependency>;
type ManifestPermissions$1 = Set<ManifestPermission>;
type ManifestOptionalPermissions$1 = Set<ManifestOptionalPermission>;
type ManifestHostPermissions = Set<string>;
declare const EntrypointFileExtensions: ReadonlySet<string>;
declare enum EntrypointType {
Background = "background",
Command = "command",
ContentScript = "content",
Page = "page",
Service = "service",
Relay = "relay",
Options = "options",
Popup = "popup",
Sidebar = "sidebar",
Offscreen = "offscreen"
}
interface EntrypointOptions {
/**
* List of target browsers to include this entrypoint in. Defaults to being included in all
* builds. Cannot be used with `exclude`. You must choose one of the two options.
*
* @default undefined
*/
includeBrowser?: `${Browser}`[] | Browser[];
/**
* List of target browsers to exclude this entrypoint from. Cannot be used with `include`. You
* must choose one of the two options.
*
* @default undefined
*/
excludeBrowser?: `${Browser}`[] | Browser[];
/**
* List of target apps to include this entrypoint in. Defaults to being included in all builds.
* Cannot be used with `excludeApp`. You must choose one of the two options.
*
* @default undefined
*/
excludeApp?: string[];
/**
* List of target apps to exclude this entrypoint from. Cannot be used with `includeApp`. You
* must choose one of the two options.
*
* @default undefined
*/
includeApp?: string[];
/**
* Build mode for filtering entry points. Entry point will be included only
* if it matches the current build mode (Production or Development).
*
* @default undefined
*/
mode?: `${Exclude<Mode, Mode.None>}` | Exclude<Mode, Mode.None>;
/**
* Manifest version constraint for this entry point. Entry point will be included
* only if it matches the target manifest version.
*
* @default undefined
*/
manifestVersion?: ManifestVersion;
/**
* Debug mode flag. If true, entry point will be included only when building
* with DEBUG flag enabled.
*
* @default undefined
*/
debug?: boolean;
}
interface EntrypointFile {
file: string;
import: string;
external?: string;
}
/**
* Dictionary of entrypoint for the build configuration.
*
* @key {string} - The name of the entrypoint that will be used in the bundler configuration.
* @value {EntrypointFile[]} - Array of files that will be included in this entrypoint.
* These files will be compiled and bundled together as part of the specified entrypoint.
*/
type EntrypointEntries = Map<string, Set<EntrypointFile>>;
interface EntrypointParser<O extends EntrypointOptions> {
options(file: EntrypointFile): O;
contract(file: EntrypointFile): string | undefined;
}
interface EntrypointFinder {
files(): Promise<Set<EntrypointFile>>;
empty(): Promise<boolean>;
exists(): Promise<boolean>;
clear(): this;
holds(file: EntrypointFile): boolean;
}
interface EntrypointOptionsFinder<O extends EntrypointOptions> extends EntrypointFinder {
type(): EntrypointType;
options(): Promise<Map<EntrypointFile, O>>;
contracts(): Promise<Map<EntrypointFile, string | undefined>>;
}
interface EntrypointNameGenerator {
reserve(name: string): this;
name(name: string): string;
file(file: EntrypointFile): string;
likely(name?: string): boolean;
has(name: string): boolean;
reset(): this;
}
interface EntrypointBuilder {
build(): Promise<void>;
destroy(): Promise<void>;
}
type EntrypointConstructorParameter<T> = T extends new (arg: infer P) => any ? P : never;
type ManifestPermissions = chrome.runtime.ManifestPermissions;
type ManifestOptionalPermissions = chrome.runtime.ManifestOptionalPermissions;
declare const BackgroundEntryName = "background";
interface BackgroundConfig {
persistent?: boolean;
permissions?: ManifestPermissions[];
optionalPermissions?: ManifestOptionalPermissions[];
hostPermissions?: string[];
optionalHostPermissions?: string[];
}
type BackgroundOptions = BackgroundConfig & EntrypointOptions;
type BackgroundEntrypointOptions = BackgroundOptions;
type BackgroundMainHandler = (options: BackgroundOptions) => Awaiter<void>;
interface BackgroundDefinition extends BackgroundEntrypointOptions {
main?: BackgroundMainHandler;
}
type BackgroundBuilder = EntrypointBuilder;
export { type BackgroundEntrypointOptions as $, ContentScriptEvent as A, type BackgroundConfig as B, type CommandDefinition as C, type ContentScriptEventCallback as D, type ExecuteActionCommandDefinition as E, type ContentScriptEventEmitter as F, type ContentScriptContext as G, type ContentScriptMainFunction as H, type ContentScriptNode as I, type ContentScriptNodeSet as J, type ContentScriptDefinition as K, type ContentScriptResolvedDefinition as L, type ContentScriptAppendDefinition as M, type ContentScriptBuilder as N, type ManifestBuilder as O, ManifestIncognito as P, type ManifestVersion as Q, type ManifestPopup as R, type ManifestSidebar as S, type EntrypointOptions as T, type EntrypointFile as U, type EntrypointEntries as V, type EntrypointFinder as W, type EntrypointOptionsFinder as X, type EntrypointNameGenerator as Y, type EntrypointParser as Z, EntrypointType as _, type BackgroundDefinition as a, type CommandEntrypointOptions as a0, type CommandOptions as a1, type ManifestIcons as a2, type ManifestIncognitoValue as a3, type BackgroundBuilder as a4, type EntrypointBuilder as a5, type EntrypointConstructorParameter as a6, type CommandBuilder as a7, type CommandResolvedDefinition as a8, type CoreManifest as a9, type ManifestBackground as aa, type ManifestCommands as ab, type ManifestContentScripts as ac, type ManifestDependencies as ad, type ManifestPermissions$1 as ae, type ManifestOptionalPermissions$1 as af, type ManifestHostPermissions as ag, type ManifestAccessibleResources as ah, type ManifestAccessibleResource as ai, type FirefoxManifest as aj, type BackgroundOptions as ak, ManifestMatchSchemes as al, ManifestSpecialSchemes as am, type ChromeManifest as an, type SafariManifest as ao, type Manifest as ap, type ManifestEntry as aq, type ManifestContentScript as ar, type ManifestCommand as as, type ManifestDependency as at, type ManifestIcon as au, EntrypointFileExtensions as av, BackgroundEntryName as aw, type BackgroundMainHandler as b, type CommandUnresolvedDefinition as c, CommandExecuteActionName as d, type CommandConfig as e, type CommandExecute as f, ContentScriptMatches as g, ContentScriptDeclarative as h, type ContentScriptConfig as i, type ContentScriptOptions as j, type ContentScriptEntrypointOptions as k, ContentScriptAppend as l, type ContentScriptMountFunction as m, type ContentScriptMount as n, type ContentScriptProps as o, type ContentScriptAnchor as p, type ContentScriptAnchorGetter as q, type ContentScriptAnchorResolver as r, type ContentScriptRenderReactComponent as s, type ContentScriptRenderValue as t, type ContentScriptRenderHandler as u, type ContentScriptContainerTag as v, type ContentScriptContainerOptions as w, type ContentScriptContainerFactory as x, type ContentScriptContainerCreator as y, type ContentScriptWatchStrategy as z };