0

我正在做一个项目,我需要在 chrome 扩展弹出窗口上显示当前所选标签网站的所有图像,我尝试了很多东西,但弹出窗口总是显示为一个没有任何内容的小弹出窗口。

这是代码:

DOM.js:

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
 if (request.action == "getDOM")
sendResponse({dom: document.body.getElementsByTagName("img")});
 else
   sendResponse({}); // Send nothing..
});

清单.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"
    ]
}]

}

弹出.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
4

1 回答 1

0

我前段时间遇到了同样的问题。只是禁止在 popup.html 中放置脚本代码,因此它不起作用。将您的代码完全放在 dom.js 中,一切都应该没问题。

于 2013-05-07T13:48:41.567 回答