【问题标题】:addEventListener not triggering when used in a Chrome extension在 Chrome 扩展程序中使用时 addEventListener 未触发
【发布时间】:2012-03-25 14:21:27
【问题描述】:

我正在尝试制作一个 Chrome 扩展程序,它将搜索给定页面的不同缓存数据库。但是,它并没有像我预期的那样工作。

<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">

var x;
var img = document.getElementsByTagName("img");
for(x in img) {
    img[x].addEventListener('click',openPage, false);
}

function openPage(event) {
    alert("clicked");
    var e = event.target;
    switch(e.alt) {
    case "WayBack Machine":
        chrome.tabs.update(tab.id, { url: "http://wayback.archive.org/web/*/" + tab.url });
        break;
    
    case "Google Cache":
        if(tab.url.substring(0, 5) == "http:")
            chrome.tabs.update(tab.id, { url: 'http://webcache.googleusercontent.com/search?q=cache:' + tab.url.substr(7) });
        else if(tab.url.substring(0,6) == "https:")
            chrome.tabs.update(tab.id, { url: 'http://webcache.googleusercontent.com/search?q=cache:' + tab.url.substr(8) });
        break;

    case "Yahoo Cache":
        // Need the yahoo cache url
        break;

    case "Bing Cache":
        chrome.tabs.update(tab.id, { url: "http://cc.bingj.com/cache.aspx?q=" + tab.url + "&d=4572216504747453&mkt=en-US&setlang=en-US&w=802790a9,218b61b8" });
        break;
    
    case "Gigablast":
        chrome.tabs.update(tab.id, { url: "http://www.gigablast.com/get?q=" + tab.url + "&c=main&d=70166151696&cnsp=0" });
        break;

    case "CoralCDN":
        chrome.tabs.update(tab.id, { url: tab.url + ".nyud.net" });
        break;
    
    default: // Webcite
        // Need to send a request, this won't do
        chrome.tabs.update(tab.id, { url: "http://webcitation.org/query" });
        break;
    }

}

</script>
</head>

<body>
<img src="Google Cache.png", alt="WayBack Machine" class="button" id="WayBack Machine" height ="40px" />
<img src="Google Cache.png", alt="Google Cache" class="button" id="Google Cache" height ="40px" />
<img src="Google Cache.png", alt="Yahoo Cache" class="button" id="Yahoo Cache" height ="40px" />
<img src="Google Cache.png", alt="Bing Cache" class="button" id="Bing Cache" height ="40px" />
<img src="Google Cache.png", alt="Gigablast" class="button" id="Gigablast" height ="40px" />
<img src="Google Cache.png", alt="CoralCDN" class="button" id="CoralCDN" height ="40px" />
<img src="Google Cache.png", alt="Webcite" class="button" id="Webcite" height ="40px" />
</body>

</html>

但是,它甚至没有 alert(); 当我在 jsfiddle.net 中尝试代码时,它可以工作:http://jsfiddle.net/RZ2wC/


这是我的 manifest.json:

{
  "name": "gCache",
  "version": "1.1.5",
  "description": "View the Google Cache of a page",

  "browser_action": {
    "default_icon": "icon.png",
    "default_text": "Google Cache version of this page",
    "default_popup": "cacheList.html"
  },

  "permissions": [
    "tabs"
  ]
}

任何帮助将不胜感激。关于这个问题以及您在我的代码中看到的我尚未解决的任何错误。

【问题讨论】:

    标签: javascript json google-chrome google-chrome-extension dom-events


    【解决方案1】:

    通过右键单击 browserAction 按钮并选择“检查弹出窗口”(这将导致“更多错误”)调试您的示例,显示您正在尝试将事件添加到“0”

    Uncaught TypeError: Object 0 has no method 'addEventListener'
    

    原因是恕我直言,页面尚未加载,但您正在尝试访问图像的 DOM。

    尝试将您的 addEvents 包装在类似的函数中

    function magic() {
      var x;
      var img = document.getElementsByTagName("img");
      for(x=0; x < img['length']; ++x) {
        img[x].addEventListener('click',openPage, false);
      }
    }
    

    并从 body-onload 事件(或 $(document).ready() 如果使用 jQuery)调用它

    <body onload="magic()">
    <img src="Google Cache.png", alt="WayBack Machine" class="button" id="WayBack Machine" height ="40px" />
    <img src="Google Cache.png", alt="Google Cache" class="button" id="Google Cache" height ="40px" />
    ...
    </body>
    

    【讨论】:

    • 感谢调试工具提示。这很有帮助。
    【解决方案2】:

    尝试将您的 javascript 作为外部文件包含在页面底部(在您关闭正文之前)引用。

    另外,为了确保 DOM 已经实际加载,请将其包装在 DOMContentLoaded-listener 中:

    document.addEventListener('DOMContentLoaded', function() {
        /* Add your event listeners here */
    });
    

    【讨论】:

    • +1 用于使用 DOMContentLoaded 的 addEventListener。但是,文件不需要包含在页面底部。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-10
    • 1970-01-01
    • 2018-10-24
    • 2015-04-08
    • 1970-01-01
    相关资源
    最近更新 更多