Logo
My Account |  Site Map | Contact Us  
Welcome Guest Search | Active Topics | Sign In | Register

How to check the status of XMLHttpRequest? Options
Opex
Posted: Thursday, January 7, 2021 8:35:35 PM
Rank: Newbie
Groups: Member

Joined: 1/7/2021
Posts: 2
I make a parser, the content on the site is sometimes updated via XMLHttpRequest, which periodically causes difficulties with text analysis when the content is only partially loaded. Is it possible to check the status of XMLHttpRequest?
In .js module from site I found xhr object who open XMLHttpRequest, but when I try to check him status I recieve "r is not defined":
Code: C#
JSObject rdyState = (JSObject)webView1.EvalScript("r.readyState");
textBox2.Text = rdyState.ToString();

Chrome, C# WinApp
eo_support
Posted: Friday, January 8, 2021 2:24:58 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 22,618
Hi,

You should not do this on the C# side. When you call EvalScript, it runs the code in the global scope. Here it is unlikely that your variable "r" is in the global scope as typically the this kind of callback has all variables in the local function scope. So this is entirely a JavaScript matter that's better to be resolved on the JavaScript side. Once you have the value you are interested on JavaScript side, you can either store it in a global variable, or use a global JavaScript function to return the value and then call that function from C# side to retrieve the value.

Thanks!
Opex
Posted: Friday, January 8, 2021 4:04:13 PM
Rank: Newbie
Groups: Member

Joined: 1/7/2021
Posts: 2
eo_support wrote:
that's better to be resolved on the JavaScript side. Once you have the value you are interested on JavaScript side, you can either store it in a global variable, or use a global JavaScript function to return the value

Do I understand correctly that you advise me to change the JS code on someone else's site and change the XHR object from local to global?
eo_support
Posted: Friday, January 8, 2021 5:11:55 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 22,618
Hi,

No. We are not advising you changing someone else's code per se. We are simply telling you when you work with JavaScript code, you need to follow JavaScript's rule. One of the most basic rule is a JavaScript variable is always associated to a scope. This scope can either be global or local (for example, a local variable inside a JavaScript function). Obviously for local variable inside a function, it ceases to exist once that function returns. So you simply can not access those variables from outside because both where (which scope) and when (whether that scope is still alive) would be in question. As such your way of using EvalScript won't work. In your case, you get a "r is not defined" which clearly indicates that r does not exist in global scope. You could run into a worse case where variable "r" indeed exists in the global scope but it is entirely something else, which would cause EvalScript not raising any error but return you the wrong value, which could be much harder to troubleshoot.

One way to do what you want to do is to intercept XMLHttpRequest calls. The basic steps are as follow:

1. Write JavaScript code to intercept certain XMLHttpRequest methods. You would most likely need to intercept open and send method. You can find some sample code here:

https://medium.com/@gilfink/quick-tip-creating-an-xmlhttprequest-interceptor-1da23cf90b76

2. Inside your intercepting open/send method, you can attach your own event handlers to monitor the XMLHttpRequest object's progress using addEventListener. See here for sample code:

https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest

3. Inside the event handlers in step 2, get any information you are interested and store them in a global variable;

4. Inject the script code used in step #1 to #3 to the WebView using WebView.JSInitCode property. This way as soon as the page creates an XMLHttpRequest object, it will be automatically "wiretapped" by your code;

5. Finally you can use EvalScript to access the variable you prepared in step #3 from C# side.

As you can see, all the "heavy lifting" are on the JavaScript side. Obviously you will need to know JavaScript well in order to get this to work. Unfortunately we are not in a position to provide support on general JavaScript programming, so while we are happy to point you to the right direction, you will still need to troubleshoot your JavaScript code yourself if you run into any problems.

Thanks!


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.