Dbz*_*Dbz 5 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>
Run Code Online (Sandbox Code Playgroud)
但是,它甚至没有警报(); 当我在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"
]
}
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激.关于这个问题以及你在我的代码中看到的任何我还没有得到的错误.非常感谢!
尝试将您的javascript包含为外部文件,从页面底部引用(在关闭正文之前).
另外,为确保实际加载了DOM,请将其包装在DOMContentLoaded-listener中:
document.addEventListener('DOMContentLoaded', function() {
/* Add your event listeners here */
});
Run Code Online (Sandbox Code Playgroud)
通过右键单击 browserAction 按钮并选择“检查弹出窗口”(这将导致您出现“更多错误”)来调试示例,表明您正在尝试将事件添加到“0”
Uncaught TypeError: Object 0 has no method 'addEventListener'
Run Code Online (Sandbox Code Playgroud)
恕我直言,原因是页面尚未加载,但您正在尝试访问图像的 DOM。
尝试将 addEvents 包装在类似的函数中
function magic() {
var x;
var img = document.getElementsByTagName("img");
for(x=0; x < img['length']; ++x) {
img[x].addEventListener('click',openPage, false);
}
}
Run Code Online (Sandbox Code Playgroud)
并从 body-onload 事件中调用它(如果使用 jQuery,则为 $(document).ready() )
<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>
Run Code Online (Sandbox Code Playgroud)