Chrome扩展程序:调试器API不会触发事件

ant*_*oho 5 google-chrome google-chrome-extension

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

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;
    }
}); 
Run Code Online (Sandbox Code Playgroud)

单击图标后,我可以在页面顶部看到黄色栏,并且消息为“扩展程序正在调试此页面”。

但是,在F5之后,我看不到我的扩展程序监听时间轴事件。

似乎尚未分配事件。

chrome.debugger.onEvent.addListener(function(tabId, method, params) {
    console.log("params = ", params);
});
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

Sen*_*p12 0

您似乎忘记发送Debugger.enable

chrome.debugger.sendCommand(
        { tabId: tabId }, "Debugger.enable", {}, () => {
Run Code Online (Sandbox Code Playgroud)

在做之前

chrome.debugger.sendCommand({
                tabId: tabId
            }, "Tracing.start", { "maxCallStackDepth" : 5 }, function(response) {
Run Code Online (Sandbox Code Playgroud)

这是一个工作版本:

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;
  }
}); 
Run Code Online (Sandbox Code Playgroud)