VB.NET Gecko Web浏览器javascript函数调用

VB.NET Gecko Web Browser javascript function calling?

本文关键字:javascript 函数调用 浏览器 Web NET Gecko VB      更新时间:2023-09-26

我在VB.NET(Windows窗体应用程序)程序中使用GeckoWebBrowserGeckoWebBrowser加载本地html文件。这个html内嵌了一个svg文件(带有骨骼和内脏的人体图),其中包含javascript函数,用于从svg文档中提取元素的所有"id"。我想从VB.NET(Windows窗体应用程序)调用前面提到的javascript函数,但我不知道如何调用。有人能帮我吗,或者给我一个源代码示例?我发现的所有东西都是基于C#。。。这是我的html文件中的javascript函数:

<script type="text/javascript">
(funcion () {
// Function to be called in VB.NET when the DOM is loaded

var SVGHandler = function () {
    // Picking up the id Root Node="CUERPO_HUMANO" into svg variable
    var svg = document.querySelector('#CUERPO_HUMANO');

    // In Items we save all the <g> which have an ID
    var items = svg.querySelectorAll('g[id], path[id]');
    //var items = svg.querySelectorAll('g[id]');
    // We loop all the nodes saved in Items and add them to click event listener 
    forEach(items, function (index, value) {
        value.addEventListener('click', function (event) {

                event.preventDefault();
                //We avoid the spread of events
                event.stopPropagation();


                return event.currentTarget.id
                // console.log(event.currentTarget.id)
            });
    });

}
// https://toddmotto.com/ditch-the-array-foreach-call-nodelist-hack/
var forEach = function (array, callback, scope) {
  for (var i = 0; i < array.length; i++) {
    callback.call(scope, i, array[i]); // passes back stuff we need
  }
};

// With this method, we call a SVGHandler when DOM is totally loaded 
document.addEventListener('DOMContentLoaded', SVGHandler);
})();
</script> 

每次单击GeckoWebBrowser中加载的人体图中的特定骨骼或器官时,我应该在VB.NET中使用什么代码来调用javascript函数?我想将调用中获得的"id"保存到string变量中,以便将其用作SQL语句中的参数并填充DataGridView。我一直在搜索,我能找到的都与C#有关,而不是一个VB.NET的例子。尽管我试图找出VB.NET中的等价性,试图将C#的示例转换为VB.NET,但我对如何调用javascript有一些疑问。根据我的javascript函数,它可能是这样的:

browserControl.Navigate("javascript:void(funcion())");

求你了,有人能帮我解决这个问题吗?我会非常感谢。。。

既然你已经设置了点击EventListener的,我认为你不是在寻找从VB.NET调用最终函数的方法,但根据你的帖子,这还不清楚,所以我会给你举一些例子,说明如何调用javascript函数,以及如何使用GeckoWebBrowser通过javascriptVB.NET代码中触发反应。

您尝试从vb代码中调用js函数的代码片段是正确的。唯一的问题是您在html文件中没有定义任何可调用的js函数。在您的情况下,您应该这样做来从vb:触发您的主js函数

//Sorry I don't know vb. I'll give example in c# keeping it as simple as possible so that you can easily convert it to vb
Gecko.GeckoHtmlElement humanBodyPart = (Gecko.GeckoHtmlElement) browserControl.Document.GetElementById("your id");
humanBodyPart.Click();

上面的代码在GeckoWebBrowser中找到具有匹配id的元素并单击它。由于您已经设置了click EventListener的,通过单击其中一个元素,这将触发分配给它们的函数运行。

接下来,为了将元素的id保存到vb代码中的string变量中,您需要将这一小段js代码添加到forEach函数中作为"callback"参数传递的代码中:

var event = document.createEvent('MessageEvent');
var origin = window.location.protocol + '//' + window.location.host;
var event = new MessageEvent('jsCall', { 'view': window, 'bubbles': false, 'cancelable': false, 'data': 'YOUR EVENTUAL ID AS A STRING (THIS STUFF GOES BACK TO THE VB/C# CODE)' });
document.dispatchEvent (event);

然后,上面的代码片段应该在您的vb代码中处理,如下所示:

browserControl.AddMessageEventListener("jsCall", (id) =>
{
   //Here in the variable id you have your clicked id as a string. Do what you wanted to do...
});

VB侧:您需要等到文档完成后才能添加侦听器例如:_DocumentCompleted

Private Sub GeckoWebBrowser1_DocumentCompleted(sender As Object, e As Gecko.Events.GeckoDocumentCompletedEventArgs) Handles GeckoWebBrowser1.DocumentCompleted GeckoWebBrowser1.AddMessageEventListener("my_function_name JS_side", AddressOf my_sub_for_treatment) End Sub

JS侧:

var event = document.createEvent('MessageEvent');
var origin = window.location.protocol + '//' + window.location.host;
var event = new MessageEvent('my_function_name JS_side', { 'view': window, 'bubbles': false, 'cancelable': false, 'data': my_data_to transfer });
document.dispatchEvent (event);