如何在 WebView 的 iframe 中注入 CSS? UWP
How to inject CSS inside an iframe in a WebView? UWP
我想知道是否可以在 UWP 中做类似于 Stylish 为浏览器所做的事情,因为我已经为我在 [=13= 中使用的网页编写了 CSS ] 但是这个网页有一个 iframe 所以我需要将 CSS 应用到所有以 "https://webchat.botframework.com/"
开头的 URL 以使其工作(在 Stylish 上)
<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 injection.Then 运行脚本通过eval函数通过InvokeScriptAsync.But 注入是添加新的东西到原始网页,所以每次加载网页时,都会重复注入过程,这意味着 InvokeScriptAsync 将在每个 time.You 被触发,可以将以下代码写入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代码 inject.The 'chat'是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 });
}
我想知道是否可以在 UWP 中做类似于 Stylish 为浏览器所做的事情,因为我已经为我在 [=13= 中使用的网页编写了 CSS ] 但是这个网页有一个 iframe 所以我需要将 CSS 应用到所有以 "https://webchat.botframework.com/"
开头的 URL 以使其工作(在 Stylish 上)
<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 injection.Then 运行脚本通过eval函数通过InvokeScriptAsync.But 注入是添加新的东西到原始网页,所以每次加载网页时,都会重复注入过程,这意味着 InvokeScriptAsync 将在每个 time.You 被触发,可以将以下代码写入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代码 inject.The 'chat'是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 });
}