Chrome扩展程序:调试器API未触发事件。

6

我想要监听选项卡中发生的所有时间线事件, 我创建了一个扩展程序。

chrome.browserAction.onClicked.addListener(function(tab) {
    var tabId = tab.id;

    console.log("tabId = ", tabId);

    if (running === false) {
        checkAvailable();

        chrome.debugger.attach({
            tabId: tabId
        }, protocolVersion, function() {
            running = true;

            if (chrome.runtime.lastError) {
                console.log(chrome.runtime.lastError.message);
                return;
            }

            chrome.debugger.sendCommand({
                tabId: tabId
            }, "Tracing.start", { "maxCallStackDepth" : 5 }, function(response) {

                console.log(response);              
                // listening for responses from Timeline
                chrome.debugger.onEvent.addListener(function(tabId, method, params) {
                    console.log("params = ", params);
                });
            });



            chrome.debugger.onDetach.addListener(function (source, reason) {
                running = false;
            });
        });        
    } else {
        chrome.debugger.detach({
            tabId: tabId
        }, null);
        running = false;
    }
}); 

点击图标后,我可以看到页面顶部出现了黄色条形,并显示“扩展正在调试此页面”的消息。

然而,在按下F5后,我没有看到我的扩展监听时间轴事件。

看起来事件还没有被分配。

chrome.debugger.onEvent.addListener(function(tabId, method, params) {
    console.log("params = ", params);
});

有什么想法吗?

可能与这个重复了:https://dev59.com/z4Pba4cB1Zd3GeqPp0JB - Dayton Wang
1个回答

0

看起来你忘记发送 Debugger.enable 了:

chrome.debugger.sendCommand(
        { tabId: tabId }, "Debugger.enable", {}, () => {

在进行之前

chrome.debugger.sendCommand({
                tabId: tabId
            }, "Tracing.start", { "maxCallStackDepth" : 5 }, function(response) {

这是一个可用的版本:

let running = false
let protocolVersion = '1.3'

chrome.browserAction.onClicked.addListener(function (tab) {
  var tabId = tab.id;

  console.log("tabId = ", tabId);

  if (running === false) {
    /*    checkAvailable(); */

    chrome.debugger.attach({
      tabId: tabId
    }, protocolVersion, function () {
      running = true;

      if (chrome.runtime.lastError) {
        console.log(chrome.runtime.lastError.message);
        return;
      }


      chrome.debugger.sendCommand(
        { tabId: tabId }, "Debugger.enable", {}, () => {

        chrome.debugger.sendCommand({
          tabId: tabId
        }, "Tracing.start", { "maxCallStackDepth": 5 }, function (response) {

          console.log(response);
          // listening for responses from Timeline
          chrome.debugger.onEvent.addListener(function (tabId, method, params) {
            console.log("params = ", params);
          });
        });

})

      chrome.debugger.onDetach.addListener(function (source, reason) {
        running = false;
      });
    });
  } else {
    chrome.debugger.detach({
      tabId: tabId
    }, null);
    running = false;
  }
}); 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接