[英]VSCode: How to use Chromium for debugging on local files (extension: Debugger for Chrome )
[英]Debugging source files using chrome extension
我正在尝试使用Chrome扩展程序控制调试器。
我正在使用devtools-protocol和chrome扩展文档,但是我不知道如何实现它们,因为我没有看到所使用方法的任何示例。 我从这里使用了示例扩展,它仅显示了如何暂停和恢复调试器,但这对我绝对没有用。 我尝试实现示例中的某些方法,但没有任何反应。
function onDebuggerEnabled(debuggeeId) {
chrome.debugger.sendCommand(debuggeeId, "Debugger.setBreakpointByUrl", {
lineNumber: 45825,
url: 'full https link to the js file from source tab'
});
}
问题是我要调试的js文件是从网站的“源”选项卡中加载的,它很大,格式化后我们要说150k +行,并且加载需要一些时间。
现在谁能告诉我如何从源代码中简单地在js文件中添加一个断点(使用CHROME EXTENSION),以便可以在操作时触发它,然后将停止调试器,以便更改值等?
也许您放置了错误的断点位置(格式化源),请尝试使用原始源并添加columnNumber: integer
这是工作版本的JavaScript pause/resume -> background.js
:
代码:
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// mod by ewwink
var attachedTabs = {};
var version = "1.1";
chrome.debugger.onEvent.addListener(onEvent);
chrome.debugger.onDetach.addListener(onDetach);
chrome.browserAction.onClicked.addListener(function(tab) {
var tabId = tab.id;
var debuggeeId = {
tabId: tabId
};
if (attachedTabs[tabId] == "pausing")
return;
if (!attachedTabs[tabId])
chrome.debugger.attach(debuggeeId, version, onAttach.bind(null, debuggeeId));
else if (attachedTabs[tabId])
chrome.debugger.detach(debuggeeId, onDetach.bind(null, debuggeeId));
});
function onAttach(debuggeeId) {
if (chrome.runtime.lastError) {
alert(chrome.runtime.lastError.message);
return;
}
var tabId = debuggeeId.tabId;
chrome.browserAction.setIcon({
tabId: tabId,
path: "debuggerPausing.png"
});
chrome.browserAction.setTitle({
tabId: tabId,
title: "Pausing JavaScript"
});
attachedTabs[tabId] = "pausing";
chrome.debugger.sendCommand(
debuggeeId, "Debugger.enable", {},
onDebuggerEnabled.bind(null, debuggeeId));
}
function onDebuggerEnabled(debuggeeId) {
chrome.debugger.sendCommand(debuggeeId, "Debugger.setBreakpointByUrl", {
lineNumber: 10,
url: 'https://ewwink.github.io/demo/script.js'
});
}
function onEvent(debuggeeId, method, params) {
var tabId = debuggeeId.tabId;
if (method == "Debugger.paused") {
attachedTabs[tabId] = "paused";
var frameId = params.callFrames[0].callFrameId;
chrome.browserAction.setIcon({
tabId: tabId,
path: "debuggerContinue.png"
});
chrome.browserAction.setTitle({
tabId: tabId,
title: "Resume JavaScript"
});
chrome.debugger.sendCommand(debuggeeId, "Debugger.setVariableValue", {
scopeNumber: 0,
variableName: "changeMe",
newValue: {
value: 'hijacked by Extension'
},
callFrameId: frameId
});
}
}
function onDetach(debuggeeId) {
var tabId = debuggeeId.tabId;
delete attachedTabs[tabId];
chrome.browserAction.setIcon({
tabId: tabId,
path: "debuggerPause.png"
});
chrome.browserAction.setTitle({
tabId: tabId,
title: "Pause JavaScript"
});
}
演示
编辑:刚看到您对此的评论是针对您正在编写的自定义扩展名。 我的回答对您没有帮助(对不起!),但可能会帮助那些在这里寻求在Chrome中设置常规断点的人。
也许您已经做过,但是...您是否尝试过仅单击要在其中设置断点的行的行号?
像这样:
您甚至可以在同一行中的多个不同调用中启用或禁用断点。
加载页面后,使用F12打开Dev Tools,然后导航到“源”面板中的JS文件,并添加所需的断点。 然后,刷新页面以再次加载它-Chrome会记住您在何处设置断点并在其处停止。
如果可以修改要调试的文件的源代码,则可以使用debugger语句。
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/debugger
function potentiallyBuggyCode() {
debugger; //application will break here as long as chrome developer tools are open
}
function breakhere() {
debugger;
}
好的,开始时,您必须发送Command Debugger.enable
..像这样:
var tabId = parseInt(window.location.search.substring(1));
window.addEventListener("load", function() {
chrome.debugger.sendCommand({tabId:tabId}, "Debugger.enable");
chrome.debugger.attach( tabId, "0.1" );
chrome.debugger.onEvent.addListener(onEvent);
});
然后在您的onEvent内部可以设置断点
function onEvent(debuggeeId, message, params) {
if (tabId != debuggeeId.tabId) return;
var res = Debugger.setBreakpointByUrl( 2, 'url-of-the-script-file' );
}
我强烈建议您检查此页面上的嗅探部分: https : //chromedevtools.github.io/devtools-protocol/,因为我能够看到通过WS协议返回的json,这将帮助您做很多事情任何您想要的..我无法为您建立完整的扩展,您是一个人,
我想您需要添加某种DOM elemnet,以及要从Network.getResponseBody
解析的脚本列表,然后添加某种UX工具来选择这些脚本并让用户进行调试,该过程可能会花费您一些时间:(
希望我朝着正确的方向指引您,祝您好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.