如何调试Google Chrome后台脚本?

问题描述:

我有非常简单的扩展名:

I have very simple extension:

manifest.json

manifest.json

{
  "name": "historyCleaner",
  "version": "0.1.1",
  "manifest_version": 1,
  "description": "This is my first Chrome extension",
  "background": {
    "scripts": ["cleaner.js"]
  }, 
  "permissions": [
    "history"
  ]
}

clean.js

chrome.history.onVisited.addListener(function(HistoryItem result) {

  console.log("it works!");
  alert("it works!");

});

我已经将其加载到Google Chrome中,它已打开,...它没有工作。它不会在控制台中记录任何东西,它不会发出任何警告,更糟糕的是,我无法在开发人员工具脚本选项卡中找到它。我如何找到为什么它不起作用?

I've loaded it in Google Chrome, it is turned on and... it doesn't work. It doesn't log anything in console, it doesn't alert anything and what is worse, I can't find it in developers tools "Scripts" tab. How can I find why it doesn't work?

//编辑

我已经改变了清单。 json到这一个:

I've changed manifest.json to this one:

{
  "name": "historyCleaner",
  "version": "0.1.5",
  "manifest_version": 1,
  "description": "This is my first Chrome extension",
  "background_page": "background.html",
  "permissions": [
    "history",
    "background"
  ]
}

并将JavaScript嵌入到background.html

And embeded JavaScript in background.html

,如果您的 console.log (it works!); 不显示,那意味着 chrome.history.onVisited 尚未触发。

and also if your console.log("it works!"); does not show up, then that's mean chrome.history.onVisited is not fired yet.

ps:对于函数(HistoryItem结果),您可能需要将其更改为 function(result)

ps: For function(HistoryItem result), you may want to change it to function(result).