Retrieving JavaScript return values in a C# WebBrowser control within a WPF application

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?

Answer №1

It appears there may be a scope issue at play here. Have you considered relocating the

var elementsArray = [];

outside of the (above) mentioned

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

function? Perhaps something like

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

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

Additionally, don't forget to add semicolons in a few places (lines 5 & 6 of script.js), although it may not affect the outcome significantly.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Experiencing inaccuracies in Magento's item validation process when checking the quantity of items being added to the cart

Upon entering a text string in the quantity field of the "Add to Cart" input box, Magento does not display an error message but instead considers it as a quantity of "1". Is there a way to modify this behavior and have the validation system mark strings ...

Tools for parsing command strings in NodeJS

Currently, I'm utilizing SailsJS for my application. Users will input commands through the front-end using NodeWebkit, which are then sent to the server via sockets. Once received, these commands are parsed in the back-end and a specific service/cont ...

What sets apart this public address for an ethereum signature?

I have successfully created a signature using Metamask and the personal_sign RPC method. Now, I am attempting to verify this signature in my C# backend. After some research, I came across the Nethereum library which seems to be a good fit for this task. Be ...

Utilize AngularFire to generate a new User and invoke the factory function to showcase notifications

I recently started working with AngularJS and wanted to integrate it with AngularFire/Firebase for storing project data. I created a factory that manages notifications to be displayed in a centralized location. Here is the structure of my factory: myApp.f ...

Numerous Fascinating Challenges with React Native

Looking to share my React Native project and seeking help with some issues I'm facing. Despite multiple attempts, I have been unsuccessful in resolving them. Can anyone provide assistance? I have thoroughly searched Stack Overflow for similar questio ...

The issue with MaterialUI Select's set value is that it consistently falls outside the expected

I'm currently working on a MaterialUI Select component where I am dynamically handling the value parameter. However, I'm facing an issue where even though I set a valid value from the available options, it always shows as out of range. SelectInp ...

Converting a JSON object into an array of objects

I am looking to transform the following JSON structure let data = { item1: [11, 12, 13, 14, 15], item2: [16, 17, 18, 19, 20] } into this specific format using JavaScript's native functionalities of arrays or objects (compatible with all web brow ...

What is the best way to implement a series of delayed animations in jQuery that are connected

Imagine you have the following items: <div id="d1"><span>This is div1</span></div> <div id="d2"><span>This is div2</span></div> <div id="d3"><span>This is div3</sp ...

Whenever I try to retrieve data from MongoDB using Node.js, I consistently encounter a timeout error

Currently, I am in the process of developing a website using React.js for the front end, Node.js for the back end, and MongoDB as the database. Dummy data has been inserted into the database which can be viewed . The database has been created in Atlas, the ...

What is the best way to retrieve the duration of an object tag using JavaScript or jQuery?

My goal is to determine the duration and length of only mp4 type videos. Although I used the video tag to retrieve these values, it does not support mp4 files. Despite several attempts, I was unable to get the video tag to play only mp4 files, as it stric ...

Why is it that PHP is used to retrieve the database value, JavaScript increments it, and AJAX saves it back to the database, yet it resets to 0.0 upon page refresh?

I am in the process of creating a page for setting temperature targets. The form allows users to adjust the target temperature by increments of 0.5 using JavaScript buttons. Once the user is satisfied with the new target, they can click 'set' whi ...

Performing a map or foreach function on an array of objects limited to the first 5 objects

I need to iterate through an array of objects, but I only want to loop through the first 5 objects and then stop. What is the most efficient way to achieve this? [ {"visibility": 10000,}, {"visibility": 10000,}, {"visibilit ...

Issues with Toggling Visibility in HTML, CSS, and Javascript

Currently, I am working on a website and following a tutorial called "Creating Slideshow using HTML, CSS, and Javascript" by W3Schools. In the project, I want to hide the thumbnail images located at the bottom and the navigation arrows initially and have t ...

Convert and transfer CSS image data encoded in Base-64 to Asp.Net MVC/WebApi

Regarding the query. I successfully displayed a base-64 encoded image on the client side. .even { background: #fff url(data:image/gif;base64,R0lGODlhBgASALMAAOfn5+rq6uvr6+zs7O7u7vHx8fPz8/b29vj4+P39/f///wAAAAAAAAAAAAAAAAAAACwAAAAABgASAAAIMAAVCBxIsKDBgwgTDk ...

React - Utilizing Secondary Prop Value in Material UI Node Components

I've been working on streamlining my code and am wondering about the best way to pass an additional value using props while fetching data from the backend. I'm utilizing material UI's Autocomplete with the PERN stack. Everything is functioni ...

Flask-SocketIO: Transmitting data between nodes using Redis adapter

When integrating SocketIO into an application running behind a node-balancer, the documentation recommends using SocketIO-Redis to facilitate event passing between nodes: const io = require('socket.io')(3000); const redis = require('socket.i ...

Guide to obtaining context vnode within vue 3 custom directives

Unable to retrieve context from directive <template lang="pug"> div(class="form") select(name="name" v-model="form.input" v-select="form.inputs") option(v-for="(option, ke ...

Optimal method for retrieving data from a JSON object using the object's ID with a map

Can you teach me how to locate a json object in JavaScript? Here is a sample Json: { "Employees" : [ { "userId":"rirani", "jobTitleName":"Developer", "preferredFullName":"Romin Irani", "employeeCode":"E1", "region":"CA", "phoneNumber":"408-1234567", " ...

What is the best way to set up a simple example using a Node Stream.Readable?

I am currently exploring the concept of streams and encountering some difficulties in making it work properly. For this scenario, my goal is to send a static object to the stream and then pipe it to the server's response. This code snippet shows my ...

React component that enables radio inputs to repeat upon selection

My current project involves creating a quiz app where users can answer single questions using React on Codepen. I am utilizing an API to fetch a question, along with 3 incorrect answers and 1 correct answer, then storing them in the app's state. Howev ...