使用chrome扩展调试源文件

时间:2017-05-28 19:15:44

标签: javascript google-chrome-extension

我正在尝试使用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文件是从sources选项卡里面的网站加载的,它很大,我们在格式化之后会说150k +行,并且加载需要一些时间。

现在任何人都可以告诉我如何在源文件(USING CHROME EXTENSION)中简单地在js文件中添加一个断点,这样它就可以在动作上触发,然后停止调试器,这样我就可以更改值等等?

5 个答案:

答案 0 :(得分:1)

编辑:刚刚看到你对这是关于你正在编写的自定义扩展的评论。我的回答对你没有帮助(对不起!),但它可能会帮助那些寻找在Chrome中设置正常断点的方法的人。

也许您已经这样做了,但是......您是否尝试过单击要设置断点的行号?

像这样:

您甚至可以在同一行中的多个不同呼叫中启用或禁用断点。

加载页面后,使用F12打开Dev Tools,然后导航到Sources面板中的JS文件,并添加所需的断点。然后,刷新页面再次加载 - Chrome会记住您设置断点的位置并停在它们处。

答案 1 :(得分:1)

也许您放错了断点位置(格式化来源),尝试使用原始来源并添加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"
  });
}

演示

debugger extension demo

答案 2 :(得分:0)

如果你可以修改你想调试的文件的源代码,我会看看使用调试器语句。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/debugger

function potentiallyBuggyCode() {
    debugger;  //application will break here as long as chrome developer tools are open
}

答案 3 :(得分:0)

function breakhere() {
    debugger;
}

答案 4 :(得分:0)

好的,首先你必须发送命令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);
});

然后在你里面,你可以设置断点

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工具来选择那些脚本并让用户调试,这个过程可能需要你有段时间:(

希望我指引你朝着正确的方向前进,祝你好运!