简体   繁体   English

在Windows 10 WebView中执行JS脚本

[英]Execute js script into Windows 10 webView

I'd like to run a js function that is not written into loaded html page, for example we have a simple html page with button, and I'd like to disable the button using an external js script: 我想运行一个未写入加载的html页面的js函数,例如,我们有一个带有按钮的简单html页面,并且我想使用外部js脚本禁用该按钮:

    <!DOCTYPE html>
    <html>
    <body>

    <button id="my_button" type="button" onclick="">Click Me!</button>

    </body>
    </html>

and I have my script form a file: 我有我的脚本形式的文件:

 function disableFun() {
 document.getElementById("my_button").disabled = true;
 }

as is mentioned in official doc there is a API method InvokeScriptAsync(string scriptName, string[] arguments) but is mentioned that we must already have the script into html loaded page. 正如官方文档中提到的那样,有一个API方法InvokeScriptAsync(string scriptName, string[] arguments)但是提到我们必须已经将脚本放入html加载页面中。

Any advices are apreciated! 任何建议都很感激!

PS in android we do it very simple webView.loadUrl('my script code') PS在android中,我们做到了非常简单的webView.loadUrl('my script code')

You can use InvokeScriptAsync with the JavaScript eval function to inject content into the web page. 您可以将InvokeScriptAsync与JavaScript eval函数一起使用,以将内容注入到网页中。

see Interacting with WebView content section of this document . 请参阅本文档的“ Interacting with WebView content部分。

So to disable the button, you can use the following codes in your cs file: 因此,要禁用该按钮,可以在cs文件中使用以下代码:

String scriptStr = @"document.getElementById('your_buttonId').disabled=true";
await myWebView.InvokeScriptAsync("eval", new String[] {scriptStr});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM