UNPKG

wxt

Version:

⚡ Next-gen Web Extension Framework

43 lines (42 loc) 1.11 kB
import { applyPosition, createMountFunctions, mountUi } from "./shared.mjs"; import { browser } from "wxt/browser"; //#region src/utils/content-script-ui/iframe.ts /** @module wxt/utils/content-script-ui/iframe */ /** * Create a content script UI using an iframe. * * @see https://wxt.dev/guide/essentials/content-scripts.html#iframe */ function createIframeUi(ctx, options) { const wrapper = document.createElement("div"); const iframe = document.createElement("iframe"); iframe.src = browser.runtime.getURL(options.page); wrapper.appendChild(iframe); let mounted; const mount = () => { applyPosition(wrapper, iframe, options); options.onBeforeMount?.(wrapper, iframe); mountUi(wrapper, options); mounted = options.onMount?.(wrapper, iframe); }; const remove = () => { options.onRemove?.(mounted); wrapper.remove(); mounted = void 0; }; const mountFunctions = createMountFunctions({ mount, remove }, options); ctx.onInvalidated(remove); return { get mounted() { return mounted; }, iframe, wrapper, ...mountFunctions }; } //#endregion export { createIframeUi };