Javascript 如何调试谷歌浏览器后台脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10081898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 23:45:37  来源:igfitidea点击:

How to debug Google Chrome background script?

javascriptdebuggingbackgroundgoogle-chrome-extension

提问by ciembor

I have very simple 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"
  ]
}

cleaner.js

清洁器.js

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

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

});

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?

我已经在谷歌浏览器中加载了它,它被打开了......它不起作用。它不会在控制台中记录任何内容,也不会发出任何警报,更糟糕的是,我在开发人员工具的“脚本”选项卡中找不到它。我怎样才能找到它不起作用的原因?

//edit

//编辑

I've changed manifest.json to this one:

我已将 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"
  ]
}

And embeded JavaScript in background.html

并在 background.html 中嵌入 JavaScript

回答by u283863

enter image description here

在此处输入图片说明

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

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

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

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

回答by StackRover

This response might be late but would help the rest. if your background.html has javascript errors then the page will not load (to inspect).

这个回应可能会迟到,但会帮助其他人。如果您的 background.html 有 javascript 错误,那么页面将不会加载(检查)。

To find out whats wrong with your background.html, under chrome://chrome/extensions/ (i.e., manage extensions), click on the background.html link. This will load the developer tools but without background.html. At the botton-right of the window, you will see a red error symbol, and clicking on it will provide line numbers that needs to be fixed.

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