简体   繁体   English

使用Chrome扩展名调试源文件

[英]Debugging source files using chrome extension

I am trying to control the debugger using Chrome Extension. 我正在尝试使用Chrome扩展程序控制调试器。

I am using devtools-protocol and chrome extension documentation, but I have no idea how to implement them as I have not seen any samples of the methods in use. 我正在使用devtools-protocolchrome扩展文档,但是我不知道如何实现它们,因为我没有看到所使用方法的任何示例。 I used the sample extension from here which shows how to pause and resume the debugger only, but that's absolutely no use to me. 我从这里使用了示例扩展,它仅显示了如何暂停和恢复调试器,但这对我绝对没有用。 I tried to implement some methods in the sample, but nothing happens. 我尝试实现示例中的某些方法,但没有任何反应。

function onDebuggerEnabled(debuggeeId) {
  chrome.debugger.sendCommand(debuggeeId, "Debugger.setBreakpointByUrl", {
        lineNumber: 45825,
        url: 'full https link to the js file from source tab'
  });
}

The problem is that the js file I am trying to debug is loaded from the website inside the sources tab and it's huge, we talking 150k+ lines after its been formatted and it takes some time to load. 问题是我要调试的js文件是从网站的“源”选项卡中加载的,它很大,格式化后我们要说150k +行,并且加载需要一些时间。

Now can anyone tell me how to simply add a break point inside the js file from the sources (USING CHROME EXTENSION) so it could be triggered on action which will then stops the debugger so I could change values etc? 现在谁能告诉我如何从源代码中简单地在js文件中添加一个断点(使用CHROME EXTENSION),以便可以在操作时触发它,然后将停止调试器,以便更改值等?

Maybe you are placing wrong breakpoint location (formatted source), try working with original source and add columnNumber: integer 也许您放置了错误的断点位置(格式化源),请尝试使用原始源并添加columnNumber: integer

and here working version JavaScript pause/resume -> background.js : 这是工作版本的JavaScript pause/resume -> background.js

the code: 代码:

// 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"
  });
}

demo 演示

调试器扩展演示

EDIT: Just saw your comment about this being for a custom extension you're writing. 编辑:刚看到您对此的评论是针对您正在编写的自定义扩展名。 My answer won't help you (sorry!), but it might help people that come here looking for a way of setting normal breakpoints in Chrome. 我的回答对您没有帮助(对不起!),但可能会帮助那些在这里寻求在Chrome中设置常规断点的人。


Maybe you already did, but... Have you tried just clicking the line number of the line you want to set the breakpoint in? 也许您已经做过,但是...您是否尝试过仅单击要在其中设置断点的行的行号?

Like this: 像这样:

You can even enable or disable breakpoints in several different calls in the same line. 您甚至可以在同一行中的多个不同调用中启用或禁用断点。

When the page is loaded, open Dev Tools with F12, then navigate to the JS file in the Sources panel, and add the breakpoints you want. 加载页面后,使用F12打开Dev Tools,然后导航到“源”面板中的JS文件,并添加所需的断点。 Then, refresh the page to load it again -- Chrome will remember where you set the breakpoints and stop at them. 然后,刷新页面以再次加载它-Chrome会记住您在何处设置断点并在其处停止。

If you can modify the source code of the file that you want to debug, I would look use the debugger statement. 如果可以修改要调试的文件的源代码,则可以使用debugger语句。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/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;
}

ok, for start you have to sendCommand Debugger.enable .. something like this: 好的,开始时,您必须发送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);
});

then inside you onEvent you can set breaking points 然后在您的onEvent内部可以设置断点

function onEvent(debuggeeId, message, params) {
  if (tabId != debuggeeId.tabId) return;
  var res = Debugger.setBreakpointByUrl( 2, 'url-of-the-script-file' );
}

I would strongly suggest to check the sniffing section on this page: https://chromedevtools.github.io/devtools-protocol/ because I was able to see the json that get returned via WS protocol and that will help you to do pretty much anything you want.. I can't build you full extension, you are on your own man,, 我强烈建议您检查此页面上的嗅探部分: https : //chromedevtools.github.io/devtools-protocol/,因为我能够看到通过WS协议返回的json,这将帮助您做很多事情任何您想要的..我无法为您建立完整的扩展,您是一个人,

I guess that you need to add somekind of DOM elemnet with list of scripts that you'll parse from Network.getResponseBody and then somekind of UX tool to pick that scripts and let users to debugging,, that process could take you some time :( 我想您需要添加某种DOM elemnet,以及要从Network.getResponseBody解析的脚本列表,然后添加某种UX工具来选择这些脚本并让用户进行调试,该过程可能会花费您一些时间:(

hope I have steered you in the right direction, good luck! 希望我朝着正确的方向指引您,祝您好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM