简体   繁体   English

在C#Webbrowser控件WPF中从Javascript获取返回值

[英]Getting return value from Javascript in C# Webbrowser control WPF

I made JavaScript injection into WebBrowser control in C# (System.Windows.Controls.WebBrowser) such that, <C#> 我在C# (System.Windows.Controls.WebBrowser)中将JavaScript注入到WebBrowser控件中,这样, <C#>

IHTMLDocument2 webdoc = (IHTMLDocument2)webBrowser1.Document;
string var = File.ReadAllText("C:/.../Resources/script.txt");
object retVal = webdoc.parentWindow.execScript(var, "Jscript");

and the JavaScript file script.txt is, 和JavaScript文件script.txt是,

var headID = document.getElementsByTagName('head')[0];
var newScript = document.createElement('script');
newScript.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js'
headID.appendChild(newScript)

$('body').bind('click',function(e){
    var domsArray = [];
    for (var i = 0; i < 15; i++){
        for (var j = 0; j < 15; j++){
            if (document.elementFromPoint(e.clientX+i, e.clientY+j) && (jQuery.inArray(document.elementFromPoint(e.clientX+i, e.clientY+j), domsArray) < 0)){
            domsArray.push(document.elementFromPoint(e.clientX+i, e.clientY+j));
            }if (document.elementFromPoint(e.clientX-i, e.clientY+j) && (jQuery.inArray(document.elementFromPoint(e.clientX-i, e.clientY+j), domsArray) < 0)){
            domsArray.push(document.elementFromPoint(e.clientX-i, e.clientY+j));
            }if (document.elementFromPoint(e.clientX+i, e.clientY-j) && (jQuery.inArray(document.elementFromPoint(e.clientX+i, e.clientY-j), domsArray) < 0)){
            domsArray.push(document.elementFromPoint(e.clientX+i, e.clientY-j));
            }if (document.elementFromPoint(e.clientX-i, e.clientY-j) && (jQuery.inArray(document.elementFromPoint(e.clientX-i, e.clientY-j), domsArray) < 0)){
            domsArray.push(document.elementFromPoint(e.clientX-i, e.clientY-j));
        }}}
for (var p = 0; p < domsArray.length; p++){
    alert(domsArray[p].href);
}});

What it does is, whenever a user clicks on any point in webbrowser page, it collects the href near around that point. 它的作用是,每当用户点击webbrowser页面中的任何一点时,它就会收集该点附近的href。

I wanted to return the href array, to my C# so that I can create buttons with those links. 我想将href数组返回到我的C#,以便我可以使用这些链接创建按钮。

However, when I tried, 但是,当我试过的时候,

Console.WriteLine(retVal);

It didn't print anything on console. 它没有在控制台上打印任何东西。 Even after I cast them into things like string or int with other dummy return values, it didn't print anything. 即使我将它们转换为字符串或int与其他虚拟返回值之类的东西,它也没有打印任何东西。 Am I getting correct return? 我得到了正确的回报吗? Is there any way that I can test the output return from javascript? 有什么办法可以测试javascript的输出回报吗?

I think this could be a scope issue. 我认为这可能是一个范围问题。 Have you tried moving the 你有没有试过移动

var domsArray = [];

outside the (above) the 在(上)之外

$('body').bind('click',function(e){  .... 

function? 功能? such as

var domsArray = [];
$('body').bind('click',function(e){

    for (var i = 0; i < 15; i++){  < ... etc ....>

also I think you are missing a couple of semicolons (lines 3 & 4 of script.txt) though that probably shouldn't make a difference. 我也认为你错过了几个分号(script.txt的第3和第4行),尽管这可能不会产生任何影响。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM