我正在尝试创建一个弹出窗口,将可见标签显示为小图像。从chrome.tabs.captureVisibleTab()函数返回的 ImgSrc 是“Undefined”。我试过从各个地方运行它。我可以验证tabs.Query()返回的选项卡是否为null,因此tabs [0] .id不为null。
我这样做不正确吗?
这是我的清单,popup.html和popup.js文件:
{
"manifest_version": 2,
"name": "SuperFave",
"description": "Saves favorites demo",
"version": "1.0",
"browser_action": {
"default_popup": "popup.html"
},
"permissions": [
"tabs",
"<all_urls>"
]
}
popup.html:
<html>
<head>
<script type="text/javascript" src="jquery-1.10.2.min.js">
</script>
<script type="text/javascript" src="popup.js">
</script>
</head>
<body>
</body>
</html>
popup.js:
$(document).ready( function () {
chrome.tabs.query( {
// gets the window the user can currently see
active: true,
currentWindow: true
},
function (tabs) {
chrome.tabs.captureVisibleTab(
tabs[0].id,
function (src) {
// displays a link to the image. Can be replaced by an alert() to
// verify the result is 'undefined'
$('body').append("<a href='" + src + "'>" + tabs[0].url + "</a>");
}
);
}
);
});
答案 0 :(得分:2)
captureVisibleTab仅适用于窗口中当前的活动选项卡。因此,我需要传入window-id,而不是tab-id。
popup.js需要:
$(document).ready( function () {
chrome.tabs.query( {
// gets the window the user can currently see
active: true,
currentWindow: true
},
function (tabs) {
chrome.tabs.captureVisibleTab(
chrome.windows.WINDOW_ID_CURRENT,
function (src) {
// displays a link to the image. Can be replaced by an alert() to
// verify the result is 'undefined'
$('body').append("<a href='" + src + "'>" + tabs[0].url + "</a>");
}
);
}
);
});