【发布时间】:2016-12-28 19:08:36
【问题描述】:
我一直在寻找如何做到这一点。我发现了一些最值得注意的文章
Accessing Current Tab DOM Object from "popup.html"?
但是,我对 JavaScript 和制作 chrome 扩展非常陌生,但我遇到了死胡同。
我的猜测是没有收到回复,这解释了为什么document.write("Hellp")
不工作。任何解决此问题的帮助将不胜感激。
我有三个主要文件
manifest.json
{
"name": "My First Extension",
"version": "1.0",
"description": "The first extension that I made.",
"browser_action":
{
"default_icon": "icon.png",
"popup": "popup.html"
},
"permissions":
[
"tabs"
],
"content_scripts":
[{
"matches": ["<all_urls>"],
"js": ["dom.js"]
}]
}
popup.html
<html>
<body>
</body>
<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("Hello");
document.write(response.title)
});
});
</script>
</html>
dom.js
chrome.extension.onRequest.addListener(function(request, sender, sendResponse)
{
if (request.action == "getDOM")
sendResponse({dom: document.getElementsByTagName("body")[0]});
else
sendResponse({}); // Send nothing..
});
【问题讨论】:
标签: javascript