0
我正在做一个项目,我需要在Chrome扩展弹出菜单中显示当前所选标签网站的所有图像,我尝试了很多东西,但总是弹出显示为一个没有任何内容的小弹出窗口。chrome - 如何在popup.html中使用dom和js显示图像
下面是代码:
Dom.js:
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
if (request.action == "getDOM")
sendResponse({dom: document.body.getElementsByTagName("img")});
else
sendResponse({}); // Send nothing..
});
的manifest.json:
{
"manifest_version": 2,
"name": "PrintIt",
"version": "1.0",
"description": "Partilha conteudo com redes sociais",
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"permissions": [
"tabs"
],
"content_scripts": [{
"matches": [
"http://www.google.com/*"
],
"js": [
"dom.js"
]
}]
}
Popup.html:
<html>
<head>
<script>
chrome.tabs.getSelected(null, function(tab) {
// Send a request to the content script.
chrome.tabs.sendRequest(tab.id, {action: "getDOM"}, function(response) {
document.write(response.dom);
});
});
</script>
</head>
<body>
</body>
</html>
我做的console.log(response.dom)和我得到了estension控制台如下因素的错误:
Port error: Could not establish connection. Receiving end does not exist. miscellaneous_bindings:236
chromeHidden.Port.dispatchOnDisconnect miscellaneous_bindings:236
Error in event handler for 'undefined': Cannot read property 'dom' of
undefined TypeError: Cannot read property 'dom' of undefined
at chrome-extension://cfboppmcojfddlkbpohfnnnkogpeflgk/popup.js:4:23
at miscellaneous_bindings:281:11
at chrome.Event.dispatchToListener (event_bindings:390:21)
at chrome.Event.dispatch_ (event_bindings:376:27)
at chrome.Event.dispatch (event_bindings:396:17)
at Object.chromeHidden.Port.dispatchOnDisconnect (miscellaneous_bindings:239:27) event_bindings:380
我可以只是简单地将popup.html''并创建一个popup.js与'chrome.tabs.getSelected(null,function( chrome.tabs.sendRequest(tab.id,{action:“getDOM”},function(response){ document.write(response.dom); } { //向内容脚本发送请求 chrome.tabs.sendRequest ); });' – 2013-05-07 13:57:47
是的,您可以在popup.html的标题中定义' - 当弹出窗口出现时,该脚本将自动运行(您单击图标)而不需要其他伏都教;)我不确定你的剧本到底是什么 - 看起来我正在以更简单的方式工作。 请记住,您在清单中定义的所有脚本都将有权访问当前页面的DOM。 – Hexodus 2013-05-07 18:55:00