【问题标题】:Chrome extension: Debugger API does not fire eventChrome 扩展:调试器 API 不会触发事件
【发布时间】:2015-07-22 11:18:10
【问题描述】:

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

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

有什么想法吗?

【问题讨论】:

标签: google-chrome google-chrome-extension


【解决方案1】:

您好像忘记发送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;
  }
}); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-11
    • 2014-11-04
    • 2015-09-12
    • 1970-01-01
    • 2014-10-27
    • 2016-05-09
    • 2014-09-25
    • 2012-09-09
    相关资源
    最近更新 更多