基於OpenCV的視頻圖像組態 (11): CEF瀏覽器與C++通訊

取得頁面源碼 html

CEF按事件來處理,有點麻煩。總共分如下幾步: url

  • TChromium->OnLoadEnd事件中檢測frame->IsMain(),來判斷頁面是否已徹底載入
  • 而後調用TChromium->RetrieveHTML
  • 要在TChromium->OnTextResultAvailable事件中取得源碼

 

本身處理一下 spa

 

void __fastcall TCbwChromiumExplorer::LoadURL(UnicodeString url) { code

    FUrl = url; orm

    FChromium->LoadURL(url); htm

    FStatus = CBW_CHROMIUM_STAGE_BEGIN_LOAD; blog

} 事件

 

void __fastcall TCbwChromiumExplorer::OnLoadEnd(System::TObject* Sender, ip

    const Ucefinterfaces::_di_ICefBrowser browser, 源碼

    const Ucefinterfaces::_di_ICefFrame frame, int httpStatusCode) {

    if(frame->IsMain()) {

        FStatus = CBW_CHROMIUM_STAGE_MAINCOMPLETE;

        FChromium->RetrieveHTML();

        FStatus = CBW_CHROMIUM_STAGE_BEGIN_GETSOURCE;

    }

}

 

void __fastcall TCbwChromiumExplorer::OnTextResultAvailable(System::TObject* Sender, const System::UnicodeString aText) {

    if(CBW_CHROMIUM_STAGE_BEGIN_GETSOURCE == FStatus) {

        FSourceCode = aText;

FStatus = CBW_CHROMIUM_STAGE_END_GETSOURCE;

    }

}

 

順便判斷一下是否取得了網頁

bool __fastcall TCbwChromiumExplorer::GetIsBlank() {

    while(FStatus != CBW_CHROMIUM_STAGE_MAINCOMPLETE && FStatus != CBW_CHROMIUM_STAGE_END_GETSOURCE)

        THelper::Util::Delay(100);

    if(FSourceCode.Length() < 40)

        return true;

    return false;

}

採用FSourceCode.Length()<40來判斷的緣由是:若是載入空白,則源碼爲<html><head></head><body></body></html>,數了一下,字符數目爲39。

 

最後,在調用的時候是這樣樣的

    GlobalChromiumExplorer->LoadURL(CbwOptionForm->LabeledEdit_StartPage->Text);

    if(GlobalChromiumExplorer->IsBlank)

        GlobalChromiumExplorer->LoadURL(CbwOptionForm->LabeledEdit_StartPage->Text);

 

固然,還能夠更進一步,直接在LoadURL中進行檢測,便可簡單實現。

 

JavaSript à C++

在網上找了一下,就一種方法:

參考DELPHI的寫法,改形成下面的樣子。

class TCbwJS2C : public TObject {

public:

    static UnicodeString __fastcall Request();

};

 

class TCustomRenderProcessHandler : public TCefRenderProcessHandlerOwn {

public:

    virtual void __fastcall OnWebKitInitialized();

};

 

UnicodeString __fastcall TCbwJS2C::Request() {

ShowMessage("Call TCbwJS2C::Request");

}

 

void TCustomRenderProcessHandler::OnWebKitInitialized() {

TCefRTTIExtension::Register("JS_DRGRAPH", TCbwJS2C);

}

稍微處理一下,獲得指望結果。

相關文章
相關標籤/搜索