[英]Invoking javascript file with dom access
I have a javascript file that has code with DOM access 我有一个具有DOM访问代码的JavaScript文件
var a=document.getElementById("abc").value
I have html file, that contains all DOM information 我有html文件,其中包含所有DOM信息
<html>...<input id="abc" ...></html>
Is there anyway to get C# invoke the javascript file, and return the value of a
, back to the c# program? 反正是有得到C#调用javascript文件,并且返回的值
a
,回C#程序?
In reality the JavaScript can be much more complex, and I need to channel those "interested values" back to C#, but let's just consider the simple example mentioning here. 实际上,JavaScript可能要复杂得多,我需要将这些“感兴趣的值”传递回C#,但让我们考虑一下此处提到的简单示例。
Possible directions I could think is using https://jint.codeplex.com/ , or Web browser control. 我认为可能的方向是使用https://jint.codeplex.com/或Web浏览器控件。 The challenge here is that it not only involves the JavaScript, it also involving the HTML file.
这里的挑战在于,它不仅涉及JavaScript,还涉及HTML文件。
What I want to know is: 我想知道的是:
Is there a way to channel variable value from JavaScript back to C#? 有没有一种方法可以将变量值从JavaScript传递回C#?
How to get JavaScript evaluate DOM elements from a HTML file? 如何让JavaScript评估HTML文件中的DOM元素?
Using WebBrowser and Jint: 使用WebBrowser和Jint:
using (WebBrowser browser = new WebBrowser())
{
browser.ScriptErrorsSuppressed = true;
browser.DocumentText = @"<html><head/><body><input id=""abc"" value=""this is the value in input""></body></html>";
// Wait for control to load page
while (browser.ReadyState != WebBrowserReadyState.Complete)
Application.DoEvents();
dynamic d = (dynamic)browser.Document.DomDocument;//get de activex dom
var jengine = new Jint.Engine();
jengine.SetValue("document", d);
try
{
string val=jengine.Execute(@"var a=document.getElementById('abc').value;").GetValue("a").ToString();
Console.WriteLine(val);
}
catch (Jint.Runtime.JavaScriptException je)
{
Console.WriteLine(je);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.