import { ComponentPublicInstance } from 'vue'; import { CreateWebviewContext, WebviewContext } from '../interface.uts'; export const createWebviewContext : CreateWebviewContext = function (webviewId : string.WebviewIdString, component ?: ComponentPublicInstance | null) : WebviewContext | null { let webviewElement : UniElement | null = null; if (component == null) { const pages = getCurrentPages(); if (pages.length > 0) { webviewElement = pages[pages.length - 1].$el?.parentNode?.querySelector('#' + webviewId); } } else { webviewElement = component.$el?.parentNode?.querySelector('#' + webviewId); } if (webviewElement == null) return null; return new WebviewContextImpl(webviewElement as UniWebViewElement); } class WebviewContextImpl implements WebviewContext { private webviewElement : UniWebViewElement | null = null; constructor(webviewElement : UniWebViewElement) { this.webviewElement = webviewElement; } override back() { this.webviewElement?.back(); } override forward() { this.webviewElement?.forward(); } override reload() { this.webviewElement?.reload(); } override stop() { this.webviewElement?.stop(); } override evalJS(js : string) { this.webviewElement?.evalJS(js); } }