文章目录
导读
关于Chrome Devtool Protocol的介绍,可以参考文章 《[自动化] PyChromeDevTools源码分析 https://blog.csdn.net/kinghzking/article/details/122650766》,之前用的Python写的一些工具,现在有需求要使用到C++版本的,国际惯例,github走一走。
这里用到一个github搜索技巧
Chrome Devtool Protocol language:c++
,用来指定编程语言(指定语言为c语言是没有合适结果的),其对应的效果就是跳转到了https://github.com/search?q=Chrome+Devtool+Protocol+language%3Ac%2B%2B&type=Repositories
的URL。
开发环境
版本号 | 描述 | |
---|---|---|
操作系统 | Win11-21H2 | 内部版本号22000.588 |
Visual Studio 2019 | 16.11.12 | |
Windows SDK | 10.0.17763.0 | |
编译
Chrome Devtool Protocol中使用了websockets。所以,
- 命令行用的是:
vcpkg install cpprestsdk[websockets]:x86-windows-static --recurse
。 - 命令行用的是:
vcpkg install cpprestsdk[websockets]:x86-windows-static --recurse
。 - 命令行用的是:
vcpkg install cpprestsdk[websockets]:x86-windows-static --recurse
。
如果只是使用命令行vcpkg install cpprestsdk:x86-windows-static
的话,会报下面的错误Error | LNK2019 | unresolved external symbol
。
实战
引入cpprest头文件和命名空间
#include <cpprest\containerstream.h>
#include <cpprest\filestream.h>
#include <cpprest\http_client.h>
#include <cpprest\json.h>
#include <cpprest\producerconsumerstream.h>
#include <cpprest\details\web_utilities.h>
#include <cpprest\uri.h>
#include <cpprest\streams.h>
#include <cpprest\ws_client.h>
#include <cpprest\ws_msg.h>
#include <cpprest\asyncrt_utils.h>
using namespace ::pplx;
using namespace utility;
using namespace web;
using namespace concurrency::streams;
using namespace web::http;
using namespace web::http::client;
using namespace web::json;
using namespace web::websockets::client;
引入库文件
#ifdef _DEBUG
#define MY_COMMON_LIB_TYPE "d"
#define MY_BOOST_LIB_TYPE "-mt-gd"
#define MY_POCO_LIB_TYPE "mtd"
#else
#define MY_COMMON_LIB_TYPE ""
#define MY_BOOST_LIB_TYPE "-mt"
#define MY_POCO_LIB_TYPE "mt"
#endif
#pragma comment(lib, "Winhttp.lib")
#pragma comment(lib, "Crypt32.lib")
#pragma comment(lib, "brotlicommon-static.lib")
#pragma comment(lib, "brotlidec-static.lib")
#pragma comment(lib, "brotlienc-static.lib")
#pragma comment(lib, "cpprest_2_10" MY_COMMON_LIB_TYPE ".lib")
引入别人写好的IChromeLink类
下载https://github.com/kirino17/chromelink
的代码,找到下面文件,引入自己的项目。
- IChromeLink.h
- IChromeLink.cpp
编写测试代码
- 重载IChromeTabInfo的operator IString()函数。
struct IChromeTabInfo {
//标签描述
IString strDescription;
//Web调试地址
IString strDevToolsFrontendUrl;
//标签ID
IString strId;
//标签标题
IString strTitle;
//标签类型
IString strType;
//地址
IString strUrl;
//WS调试地址
IString strWebSocketDebuggerUrl;
operator IString() const
{
// std::cout << "[operator std::string()]";
return _T("[] strId: ") + strId
+ _T("\n\t strTitle: ") + strTitle
+ _T("\n\t strUrl : ") + strUrl
+ _T("\n\t strWebSocketDebuggerUrl: ") + strWebSocketDebuggerUrl
+ _T("\n\t strDevToolsFrontendUrl : ") + strDevToolsFrontendUrl
;
}
};
- 测试:打印所有tab页面
int foobar_IChromeLink()
{
LPCTSTR szTraceAddr = _T("localhost:9829");
Komari::IChromeCurTabs curTabs;
if (Komari::IChromeLink::GetTabs(szTraceAddr, curTabs) == FALSE)
{
return 1;
}
for (auto item : curTabs)
{
MyLogD("%s \n", ls_common::string_helper::w2c(Komari::IString(item).data()).data());
}
// Komari::IChromeLink cdp();
}
测试效果
通过命令行cefclient.exe --remote-debugging-port=9829
打开一个可调式的端口9829
,然后我们如下图打开三个网页。
然后我们分别通过浏览器和程序分别查看页面列表,结果如下图所示,均能正常显示三个调试页面:
ps: 页面排序是按队列的方式,新打开一个页面,会在队列的前端插入新页面。
文章小结
Error | LNK2019 | unresolved external symbol
- 命令行用的是:
vcpkg install cpprestsdk[websockets]:x86-windows-static --recurse
。Chrome Devtool Protocol中使用了websockets,如果只是使用命令行vcpkg install cpprestsdk:x86-windows-static
的话,会报下面的错误。 Error | LNK2019 | unresolved external symbol "__declspec(dllimport) public: void __cdecl web::websockets::client::details::websocket_client_task_impl::close_pending_tasks_with_error(class web::websockets::client::websocket_exception const &)" (__imp_?close_pending_tasks_with_error@websocket_client_task_impl@details@client@websockets@web@@QEAAXAEBVwebsocket_exception@345@@Z) referenced in function __catch$??R<lambda_c8bcff768c1b7a6664919177f490ce2a>@@QEBAXV?$task@X@Concurrency@@@Z$0
- 这个问题找了很久,资料很少,最后尝试在github上的Issues中定位找到了解决方案。其实想想,这个报错就是说少编译了东西,而且跟websocket相关,其他解决方案压根跟这个问题就没有关系,多思考,能少走弯路。
文件占用错误
- 遇到错误,多看日志,而且不要盯着标红的日志,下面的标红日志没有任何意义。
参考资料
- [自动化] PyChromeDevTools源码分析 https://blog.csdn.net/kinghzking/article/details/122650766
- Awesome Chrome DevTools https://github.com/ChromeDevTools/awesome-chrome-devtools
- qq群:夜猫逐梦技术交流裙/953949723
**ps:**文章中内容仅用于技术交流,请勿用于违规违法行为。