chrome扩展选项卡事件侦听器无法正常工作

时间:2015-07-09 13:36:08

标签: javascript google-chrome

我希望我的扩展程序能够捕获创建的任何网址,并提醒它对Chrome扩展程序来说是全新的,这是我的第一个扩展程序^ _ ^  这是manifest.json文件:

{
    "name":"modz",
    "manifest_version":2,
    "version":"1.0",
    "description":"this ext. will help you record all the urls you have visited",
    "browser_action":
    {
    "default_icon":"icon.png",
    "default_popup":"popup.html"
    },
    "permissions":[
          "tabs"
        ]

}

这是html,它包含scrpit:

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
chrome.tabs.onCreated.addListener(function ( tab ){
alert(tab.url);


});
    chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
   alert(changeInfo.url);

}); 

chrome.tabs.onActivated.addListener(function(activeInfo) {
  chrome.tabs.get(activeInfo.tabId, function(tab){
     console.log(tab.url);

  });
});
</script>
    <style type="text/css">
     body{
       width:440px;
     }
    </style>
</head>
<body>
<div id="hello">hi this is the pop up </div>
</body>
</html>

提前致谢

1 个答案:

答案 0 :(得分:1)

尝试将该脚本代码放在background.js中,并将其添加到清单中:

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

popup.html仅在用户点击浏览器操作图标时运行。 background.js将在整个会话期间运行 如果您还没有:https://developer.chrome.com/extensions/overview

,请查看此扩展指南