gpt4 book ai didi

javascript - 使用 chrome 扩展程序调试源文件

转载 作者:数据小太阳 更新时间:2023-10-29 04:19:00 25 4
gpt4 key购买 nike

我正在尝试使用 Chrome 扩展程序控制调试器。

我正在使用 devtools-protocolchrome extension文档,但我不知道如何实现它们,因为我没有看到任何使用方法的示例。我使用了 here 中的示例扩展它显示了如何仅暂停和恢复调试器,但这对我来说绝对没有用。我尝试在示例中实现一些方法,但没有任何反应。

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 暂停/恢复 -> 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"
});
}

演示

debugger extension demo

关于javascript - 使用 chrome 扩展程序调试源文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44231098/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com