如何调试Google Chrome后台脚本?

cie*_*bor 69 javascript debugging background google-chrome-extension

我的扩展非常简单:

的manifest.json

{
  "name": "historyCleaner",
  "version": "0.1.1",
  "manifest_version": 1,
  "description": "This is my first Chrome extension",
  "background": {
    "scripts": ["cleaner.js"]
  }, 
  "permissions": [
    "history"
  ]
}
Run Code Online (Sandbox Code Playgroud)

cleaner.js

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

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

});
Run Code Online (Sandbox Code Playgroud)

我已经将它加载到谷歌浏览器中,它已打开并且......它不起作用.它不会在控制台中记录任何内容,它不会提醒任何事情,更糟糕的是,我在开发人员工具"Scripts"选项卡中找不到它.我怎么才能找到它无效的原因?

//编辑

我把manifest.json改成了这个:

{
  "name": "historyCleaner",
  "version": "0.1.5",
  "manifest_version": 1,
  "description": "This is my first Chrome extension",
  "background_page": "background.html",
  "permissions": [
    "history",
    "background"
  ]
}
Run Code Online (Sandbox Code Playgroud)

并在background.html中嵌入了JavaScript

Der*_*會功夫 104

在此输入图像描述

而且如果你console.log("it works!");没有出现,那就意味着chrome.history.onVisited还没有解雇.

ps:对于function(HistoryItem result),您可能想要将其更改为function(result).


Sta*_*ver 11

这种反应可能会迟到,但会有所帮助.如果你的background.html有javascript错误,那么页面将无法加载(检查).

要找出您的background.html有什么问题,请在chrome:// chrome/extensions /(即管理扩展程序)下,点击background.html链接.这将加载开发人员工具,但没有background.html.在窗口右侧,您将看到一个红色错误符号,点击它将提供需要修复的行号.