【问题标题】:Chrome extension and history.onVisited eventChrome 扩展和 history.onVisited 事件
【发布时间】:2010-06-14 21:55:14
【问题描述】:

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

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

manifest.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>

这段代码有什么问题?

谢谢

【问题讨论】:

  • 考虑使用chrome.tabs.onUpdated。它返回changeinfo.url。顺便说一句,布拉德利的回答可以解决您的问题。

标签: google-chrome-extension


【解决方案1】:

将 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 中返回。

【讨论】:

    【解决方案2】:

    测试一下:

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

    很明显

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-27
      • 2013-11-30
      • 2017-03-20
      • 2012-01-12
      • 2011-03-24
      • 1970-01-01
      • 2015-02-20
      • 1970-01-01
      相关资源
      最近更新 更多