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-1da23cf90b762. 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_XMLHttpRequest3. 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!