1

I have a MiFi modem (Huawei e5776) which comes with its own web page that displays total traffic per month. I want to extract this value and display a meter in the icon tray. I'm sure this is possible in C++ Builder (or Delphi) but even though I'm pretty experienced in using C++ Builder, I am not in anything web related. Can someone give me some pointers how to do this? I assume I need to run the script and then extract the variable somewhere, how do I do this?

Thanks.

PS: I'd add the contents of the page but can't see a way to attach a document. Here's the first few lines..

// JavaScript Document
var g_monitoring_traffic_statistics = null; 
var g_wlan_security_settings = null;
var g_wlan_basic_settings = null;
var g_connection_trafficresponse = null;
//Prefix string of ssid2 of Multi-SSID
var g_prefixWifiSsid = "ssid2_";

function getTrafficInfo(bit) {
    var final_number = 0;
    var final_str = "";
    if(g_monitoring_dumeter_kb > bit) {
        final_number = formatFloat(parseFloat(bit), 2);
        final_str = final_number + " B";
    }
    else if(g_monitoring_dumeter_kb <= bit && g_monitoring_dumeter_mb > bit) {
        final_number = formatFloat(parseFloat(bit) / g_monitoring_dumeter_kb, 2);
        final_str = final_number + " KB";
    }
    else if(g_monitoring_dumeter_mb <= bit && g_monitoring_dumeter_gb > bit) {
        final_number = formatFloat((parseFloat(bit) / g_monitoring_dumeter_mb), 2);
        final_str = final_number + " MB";
    }
    else if(g_monitoring_dumeter_gb <= bit && g_monitoring_dumeter_tb > bit) {
        final_number = formatFloat((parseFloat(bit) / g_monitoring_dumeter_gb), 2);
        final_str = final_number + " GB";
    }
    else {
        final_number = formatFloat((parseFloat(bit) / g_monitoring_dumeter_tb), 2);
        final_str = final_number + " TB";
    }
    return final_str;
}
3
  • Send the device an HTTP GET and then put the response into an HTML parser. Commented Sep 17, 2013 at 10:31
  • You can get javascript variables that way? I thought that would only work with html variables? Does GET also take care of running the script then? ::confused:: Commented Sep 17, 2013 at 10:39
  • OK, you are probably right. If the values only get filled out by execution of client side Javascript then you need a Javascript engine. So that's probably a headless webbrowser and some DOM access. Commented Sep 17, 2013 at 10:44

1 Answer 1

1

I suggest you to use a great html wrapper (named BCB HTML) for mshtml writed specially for C++Builder; With this wrapper you can execute java script inside C++ Builder cpp codes:

THTMLDocument document;
document.create();
document.write(
    "<html><body><script>"
        "function myFunc(n)"
        "{"
            "return n * n;"
        "}"
    "</script></body></html>");
document.parentWindow.execScript("alert(myFunc(3))", "javascript");

For your jscript:

String value = document.parentWindow.execScript("getTrafficInfo(1024)", "javascript");

Also it is possible to handle html events inside BCB, access html objects , ...

you can download it from here. To use this source add html.cpp to your project.

If you use TWebBrowser to load a html page, you need just define document in global scope and write below code to connect/attach document variable to WebBrowser1->Document:

void __fastcall TForm1::WebBrowser1DocumentComplete(TObject *ASender,
    const IDispatch *pDisp, const OleVariant &URL)
{
    document.documentFromVariant(WebBrowser1->Document);
    String value = document.parentWindow.execScript("getTrafficInfo(1024)", "javascript");
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I'll give the version with TWebBrowser a try.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.