![](/img/trans.png)
[英]How to inject webkitRequestFullscreen() into UWP WebView
[英]How to inject CSS inside an iframe in a WebView? UWP
我想知道是否可以在UWP中做一些与时尚的浏览器类似的事情,因为我已经为WebView
使用的网页编写了CSS,但是该网页具有iframe,因此我需要将CSS应用于所有网址以"https://webchat.botframework.com/"
开头的网址(在Fashion上)
<WebView x:Name="WebView1" Source="https://www.blabla.net/bla/webchat.html" Width="490" Height="490" HorizontalAlignment="Center"/>
网页代码
<body>
<iframe lang="fr-FR" id="chat" style="width: 60vw; height: 95vh; margin-left: 20vw" src=''></iframe>
<script>
document.getElementById("chat").src =
"https://webchat.botframework.com/embed/BitWcVaClient2?userid=" + create_UUID() +
"&username=WW&v=4.2&l=fr-FR&s=ceraZE8go0A.lpa7tzuJ1H_rOJdJKWXibqg_aBrVQrHhhd432kU2e-M";
function create_UUID() {
var dt = new Date().getTime();
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = (dt + Math.random() * 16) % 16 | 0;
dt = Math.floor(dt / 16);
return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
});
return uuid;
}
</script>
</body>
您可以编写一个js脚本并使用js进行css注入,然后通过InvokeScriptAsync使用eval函数运行该脚本,但是注入是在原始网页中添加新内容,因此每次加载该网页时,该过程重复注入的过程意味着每次将触发InvokeScriptAsync 。您可以在WebView的DOMContentLoaded中编写以下代码。
string styleContent = "here is style content";
string injectContent = $"var st= document.createElement('style');document.body.appendChild(st);st.innerHTML = `{styleContent}`;";
await TestWeb.InvokeScriptAsync("eval", new string[] { injectContent });
更新:
如果要在iframe中注入CSS,可以使用以下js代码进行注入。“聊天”是iframe的名称,需要在iframe中添加一个名称。
private async void WebView_DOMContentLoaded(WebView sender, WebViewDOMContentLoadedEventArgs args)
{
string cssContent = "body{background:black;}";
string eval = "var frame = window.frames['chat'];" +
"var style = frame.document.createElement('style');" +
"frame.document.body.appendChild(style);" +
$"style.innerHTML = {cssContent}; ";
await sender.InvokeScriptAsync("eval", new string[] { cssContent });
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.