3

我是 Chrome 扩展程序开发的新手,我有以下问题: 我的扩展程序应该在后台运行,没有 UI,并且每次用户访问特定网页时都会显示一个警报对话框。因此,当浏览器执行时,它应该始终在后台工作。

我正在尝试使用以下代码但没有结果:

清单.json

{
  "name": "My First Extension",
  "version": "1.0",
  "description": "The first extension that I made.",
  "background_page": "background.html",
  "permissions": [
    "history"
  ]
}

背景.html

<html>
<head>
<script>
chrome.history.onVisited.addListener(function(HistoryItem result) {
    if (result.url == "http://my.url.com") {
        alert("My message");
    }
}); 
</script>
</head>
</html>

这段代码有什么问题?

谢谢

4

2 回答 2

2

将 HistoryItem 从函数中取出,您就可以了:

<html>
<head>
<script>
chrome.history.onVisited.addListener(function(result) {
    if (result.url == "http://my.url.com/") {
        alert("My message");
    }
}); 
</script>
</head>
</html>

另请注意,我在“ http://my.url.com/ ”末尾添加了斜杠,因为这将在 result.url 中返回。

于 2010-06-14T22:45:28.477 回答
0

测试这个:

<script>
chrome.history.onVisited.addListener(function(e) { console.log(e)})
</script>

很明显

于 2010-08-14T07:48:47.387 回答