Chrome扩展TAB事件监听器不工作

chrome extension tab event listeners are not working

本文关键字:工作 监听器 事件 扩展 TAB Chrome      更新时间:2023-09-26

我想要我的扩展捕捉任何创建的url,并提醒它是全新的chrome扩展,这是我的第一个扩展^_^这是舱单。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,它包含脚本:

<!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>

thanks in advance

尝试将脚本代码放在background.js中,并将其添加到您的manifest中:

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

pop .html仅在用户单击浏览器操作图标时运行。Background.js将在整个会话期间运行。
看看这个扩展指南,如果你还没有:https://developer.chrome.com/extensions/overview