gpt4 book ai didi

c# - 如何在 C# WinForms 中调用 Javascript 函数到 Web 内容(可能是 WebBrowser)

转载 作者:行者123 更新时间:2023-12-03 12:27:44 26 4
gpt4 key购买 nike

我找到了几个关于如何在 C# 代码中运行 Javascript 的教程,但是,如果我正确理解它们,它们都不允许我做我需要的事情。我有一个 C# WinFowms 应用程序,带有一个加载页面的 WebBrowser 。现在,我需要在浏览器中加载的页面上运行一些 javascript,例如填充或读取一些文本框值。我已成功在我的应用程序中运行 JS,但无论浏览器中加载了什么,JS 都会独立运行。如果我可以读取 JS 中的文本框值并将其传递回 C# 应用程序,那就太好了,但如果这不可能,我会找到另一种方法,比如将它们保存到 .txt 文件中JS 并用 C# 读取它们。首先我需要让 JS 在加载的网页上运行。有任何想法吗?提前致谢

最佳答案

如果我理解正确的话

webBrowser1.ObjectForScripting = new MyForm();
string html = "<script>external.DoSomething('test');</script>";
webBrowser1.DocumentText = html;
<小时/>
[ComVisible(true)]
public class MyForm : Form
{
public void DoSomething(string s)
{
MessageBox.Show("Called from JS: " + s);
}
}

另一个 html 示例,使用 html 中文本框的值调用 DoSomething 函数(用 C# 编写)

string html = 
"<input id=txt type=text/>" +
"<input type=button value='click' onclick='external.DoSomething(txt.value)'/>";

关于c# - 如何在 C# WinForms 中调用 Javascript 函数到 Web 内容(可能是 WebBrowser),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24100113/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com