本指南将为你详细介绍 Chrome 调试工具的安装、使用方法,帮助你高效地进行 Chrome 调试工作。
在项目根目录下创建一个 config.js 文件:
// config.js
module.exports = {
port: 9229, // 自定义调试端口
chromePath: 'C:/Program Files/Google/Chrome/Application/chrome.exe' // Chrome 安装路径
};
在终端中运行以下命令启动调试服务:
node index.js
你可以使用 Node.js 的包管理工具 npm 来安装项目:
npm install chrome-debug
若不想使用 npm,可直接从 GitHub 下载源代码,并解压到项目目录中。
const { Debugger } = require('chrome-debug');
// 初始化调试器
const debugger = new Debugger({
port: 9229, // 自定义端口
configPath: 'config.js' // 配置文件路径
});
// 启动调试服务
debugger.start().then(() => {
console.log('调试服务器已启动');
});
const { chrome } = require('@modelcontextprotocol/chrome-remote-interface');
async function main() {
const chrome = await chrome.connect({
url: 'ws://localhost:9229',
target: {
browserContextId: 0
}
});
// 操作示例:获取当前页面标题
const title = await chrome.Runtime.getTitle();
console.log('页面标题:', title);
// 关闭 Chrome 实例
await chrome.close();
}
main().catch(console.error);
chrome-debug --start --port=9230
chrome-debug --connect --url=http://localhost:9230
此项目使用了以下开源库:
此项目遵循 MIT 协议,具体内容请参阅 LICENSE 文件。
有关贡献的详细信息,请参阅 Contributing.md。
感谢以下资源和社区的支持: