取得页面源码
取得页面源码
CEF按事件来处理,有点麻烦。总共分以下几步:
自己处理一下
void __fastcall TCbwChromiumExplorer::LoadURL(UnicodeString url) {
FUrl = url;
FChromium->LoadURL(url);
FStatus = CBW_CHROMIUM_STAGE_BEGIN_LOAD;
}
void __fastcall TCbwChromiumExplorer::OnLoadEnd(System::TObject* Sender,
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来判断的原因是:如果载入空白,则源码为
,数了一下,字符数目为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);
}
稍微处理一下,得到期望结果。