gpt4 book ai didi

javascript - 在 Chrome 扩展程序中使用时不会触发 addEventListener

转载 作者:数据小太阳 更新时间:2023-10-29 06:07:57 24 4
gpt4 key购买 nike

我正在尝试制作一个 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 作为外部文件包含在内,从页面底部引用(就在您关闭正文之前)。

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

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

关于javascript - 在 Chrome 扩展程序中使用时不会触发 addEventListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9611937/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com