2012-04-10 57 views
63

我有很简单的扩展:如何调试Google Chrome后台脚本?

的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

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

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

}); 

我在谷歌浏览器加载它,它开启和...它不起作用。它不会在控制台中记录任何内容,也不会提醒任何事情,更糟糕的是,我无法在开发人员工具“脚本”选项卡中找到它。我怎么才能找到它为什么不起作用?

//编辑

我已经改变了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" 
    ] 
} 

而且在background.html内嵌的JavaScript

+1

首先,你可以尝试从第一行中删除'HistoryItem' - 你有语法错误 – hamczu 2012-04-10 01:35:24

+0

哦,我很傻,我把它从文档中粘贴,忘了那:)。但它仍然不起作用。 – ciembor 2012-04-10 01:46:18

回答

98

enter image description here

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

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

+0

我的扩展功能附近没有“活动脚本”:(。 – ciembor 2012-04-10 01:47:13

+0

活动脚本?那是什么? – 2012-04-10 01:48:15

+0

例如您的“background.html”:)。在波兰的Chrome中,它被命名为“Zobacz aktywne”,意思是“看/观看活动”。 – ciembor 2012-04-10 01:50:31

11

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

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