2016-07-28 88 views
0

我在我的VB.NET(Windows Form App)程序中使用GeckoWebBrowserGeckoWebBrowser加載本地文件html。該html嵌入了內嵌svg文件(human body diagram with bones and internal organs)與javascript函數,用於從svg文檔中提取元素的所有「ID」。我想從VB.NET(Windows窗體應用程序)調用前述javascript函數,但我不知道如何去做。任何人都可以幫助我,或者請給我一個源代碼示例嗎?所有我發現的東西是基於在C# ... 這是我javascript功能在我html文件:VB.NET Gecko Web瀏覽器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> 

什麼代碼,我應該在VB.NET使用,每次我點擊一個打電話給我的javascript功能GeckoWebBrowser加載的人體圖中特定的骨或器官? 我想將通過調用獲取的「id」保存爲string變量,以便將其用作SQL語句中的參數並填充DataGridView。 我一直在尋找,所有我能找到的與C#有關,而不是一個VB.NET的例子。儘管我試圖找出VB.NET嘗試將C#的示例轉換爲VB.NET的等價性,但我對如何執行javascript調用存在一些疑問。據我javascript功能它可能是這樣的:

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

請,任何人都可以幫我解決這個問題?我會非常感謝...

回答

1

那麼既然你已經設置點擊EventListener的我認爲你不是在尋找一種方式來調用VB.NET最終的功能,但這是相當不清楚根據您的文章,所以我我將給你舉例說明如何使用GeckoWebBrowser通過javascript調用javascript函數以及如何觸發VB.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(); 

上面的代碼會查找元素具有匹配id在GeckoWebBrowser並點擊它。既然你已經設置了點擊EventListener's,通過點擊其中一個元素,這將觸發分配給它們的功能來運行。

繼續前進,爲了將元素的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); 

接着上面的代碼段應該在您的VB代碼,這樣的處理方式:

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

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