在chrome扩展中使用时,addEventListener不会触发

时间:2012-03-08 02:00:19

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

我正在尝试制作一个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>

然而,它甚至没有警告();当我在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"
  ]
}

非常感谢任何帮助。关于这个问题以及你在我的代码中看到的任何我还没有得到的错误。非常感谢!

2 个答案:

答案 0 :(得分:5)

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

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

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

答案 1 :(得分:2)

通过右键单击browserAction按钮调整样本并选择“inspect popup”(这将导致“更多错误”)显示,您正在尝试将事件添加到“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>