【问题标题】:VB.NET Gecko Web Browser javascript function calling?VB.NET Gecko Web Browser javascript函数调用?
【发布时间】:2016-12-02 14:47:41
【问题描述】:

我在我的VB.NET(Windows 窗体应用程序)程序中使用GeckoWebBrowserGeckoWebBrowser 加载本地 html 文件。这个html 嵌入了一个svg 文件(human body diagram with bones and internal organs)和一个javascript 函数,用于从svg 文档中获取元素的所有“id”。我想从VB.NET(Windows 窗体应用程序)调用上述javascript 函数,但我不知道该怎么做。任何人都可以帮助我,或者给我一个源代码示例吗?我找到的所有东西都基于C#... 这是我的javascript 文件中的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())");

请问,谁能帮我解决这个问题?我会非常感谢...

【问题讨论】:

    标签: javascript vb.net web browser gecko


    【解决方案1】:

    好吧,既然你已经设置了 click EventListener's 我认为你不是在寻找一种从 VB.NET 调用最终函数的方法,但根据你的帖子,这很不清楚,所以我会给你例子关于如何调用javascript 函数以及如何使用GeckoWebBrowser 通过javascriptVB.NET 代码中触发反应。

    您尝试从 vb 代码调用 js 函数的代码 sn-p 是正确的。唯一的问题是您没有在 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的元素并点击。由于您已设置单击EventListener,因此单击其中一个元素将触发分配给它们的功能运行。

    继续,为了将元素的 id 保存到您的 vb 代码中的 string 变量中,您需要将这一点 js 代码添加到您作为“回调”参数传递的代码中你的forEach 函数:

    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);
    

    那么上面的sn-p应该在你的vb代码中这样处理:

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

    【讨论】:

      【解决方案2】:

      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);

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-01-15
        • 2010-10-28
        • 2011-10-03
        • 2012-11-06
        • 1970-01-01
        • 1970-01-01
        • 2018-07-14
        • 1970-01-01
        相关资源
        最近更新 更多