错误未捕获TypeError:无法读取chrome扩展中未定义的属性“onVisited”

时间:2017-05-27 18:58:50

标签: javascript jquery google-chrome-extension google-chrome-devtools

我想在用户访问faceboook网址时进行记录,但在使用onVisited时出现错误 这是我的background.js

chrome.history.onVisited.addListener(function(historyItem){
    if(historyItem.url === "https://www.facebook.com/"){
        console.log('user visits '+historyItem.url+ ' at '+historyItem.lastVisitTime+ ' number of times visited '+historyItem.visitCount);
    }
});

我还在manifest.json中添加了权限:

{
  "manifest_version": 2,
  "name": "My Proj Extension",
  "version": "0.1",

  "background":{
    "scripts":["background.js"],

    "permissions": ["tabs", "history"]
  },

  "content_scripts":[
    {
    "matches": ["<all_urls>"],
    "js": ["content.js"]
  }
  ]

}

任何人都可以建议为什么这不起作用,我怎么能这样做..

1 个答案:

答案 0 :(得分:0)

权限密钥不应位于后台:

{
  "manifest_version": 2,
  "name": "My Proj Extension",
  "version": "0.1",
  "permissions": ["tabs", "history"],
  "background":{
    "scripts":["background.js"],


  },

  "content_scripts":[
    {
    "matches": ["<all_urls>"],
    "js": ["content.js"]
  }
  ]

}