I utilized JavaScript injection into WebBrowser control in C#
(System.Windows.Controls.WebBrowser)
to achieve, <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,
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);
}});
This functionality triggers when a user clicks on any point within the webbrowser page, capturing the href links situated around that location.
The intention was to retrieve the href array back to my C# code to generate buttons with those URLs.
However, upon testing,
Console.WriteLine(retVal);
No output was displayed on the console. Regardless of attempts to convert them into string or int and use other placeholder values, there was no result printed. Is the return value being obtained correctly? Are there any methods available to inspect the JavaScript return output?