在Windows 10 UAP webview中注入JS文件

本文关键字:注入 JS 文件 webview Windows UAP | 更新日期: 2023-09-27 17:54:28

如何将整个JS文件从应用程序资源注入到Windows 10应用程序webview中的HTML页面?

我尝试在Webview NavigationCompleted加载javascript文件从应用程序资源,并注入它使用:

webview.InvokeScriptAsync ("eva", jsDataLoadedFromResources)

在Windows 10 UAP webview中注入JS文件

你可以在WebView中注入Javascript。DOMContentLoaded事件内。

例如:

private async void WebView_DOMContentLoaded(WebView sender, WebViewDOMContentLoadedEventArgs args)
{
    await this.WebView.InvokeScriptAsync("eval", new string[] { "var script = document.createElement('script');script.innerHTML = 'var el = document.createElement('"div'");el.innerHTML='"My injected content'";document.body.appendChild(el);';document.body.appendChild(script);" });
}

我已经创建了一个标记script,然后注入了脚本的内容,然后在body元素的末尾添加它。