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;
}
4

1 回答 1

1

我建议你为专门为 C++Builder 编写的 mshtml 使用一个很棒的 html 包装器(名为 BCB HTML);使用此包装器,您可以在 C++ Builder cpp 代码中执行 java 脚本:

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");

对于您的 jscript:

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

也可以在 BCB 中处理 html 事件,访问 html 对象,...

你可以从这里下载。要使用此源代码,请将 html.cpp 添加到您的项目中。

如果您使用 TWebBrowser 加载 html 页面,您只需在全局范围内定义文档并编写以下代码以将文档变量连接/附加到 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");
}
于 2013-09-17T14:38:03.127 回答