所以当我按OK时,我想获得所选标签的html。但有些东西不起作用。我错过了什么?
的manifest.json
{
"name": "extension",
"version": "1.0",
"description": "discription",
"content_scripts": [
{
"matches": ["http://www.google.com/*"],
"js": ["contentscript.js"]
}
],
"browser_action": {
"default_icon": "icon.png",
"popup": "popup.html"
},
"permissions": [
"http://ajax.googleapis.com/",
"tabs"
]
}
popup.html
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"> </script>
</head>
<body>
<script>
function getHTML()
{
chrome.tabs.getSelected(null, function(tab) {
chrome.tabs.sendRequest(tab.id, {method: "getHTML"}, function(response) {
if(respond.method="getHTML"){
alert(response.html);
}
});
});
}
</script>
<input type="submit" value="OK" onclick ="getHTML()" />
</body>
的 contentscript.js
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) {
if(request.method == "getHTML"){
sendResponse({html: document.all[0].outerHTML});
}
}
);
答案 0 :(得分:2)
我看到两个错误: 1)popup.html正在检查“响应”而不是“响应”。 2)popup.html检查response.method为“getHTML”,但content_script从不设置“method”字段。
答案 1 :(得分:0)
document.all
!
将其替换为document.getElementsByTagName("html")[0].innerHTML
:
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) {
if(request.method == "getHTML"){
sendResponse({html: document.getElementsByTagName("html")[0].innerHTML});
}
}
);