chrome.tabs.query({
active: true,
currentWindow: true
},
function (tabs) {
chrome.tabs.captureVisibleTab(null
,{ format: "png"},
function (src) {
$('body').append("<img src='" + src + "'>" + tabs[0].url + "</img>");//appends captured image to the popup.html
}
);
});
此代码将捕获的图像附加到popup.html的正文中。但是我想要将图像添加到弹出窗口中,我想使用chrome.tabs.create({url:“ newtab.html”)打开新标签,然后将捕获的图像添加到此newtab.html。 “ newtab.html”已在路径文件夹中)。
提前致谢
我在这里过分描述了一种方法。
要点是要打开一个包含脚本的选项卡,并使用消息与之通信。
出现的一个小问题是,您不知道新打开的页面何时准备就绪。我通过使新打开的页面自己与背景页面联系来解决了该问题,但是草率的只有一个全局变量。
更好的解决方案是单次事件侦听器,如下所示:
// Execute this code from the background page, not the popup!
function openScreenshot(src){
chrome.tabs.create(
{ url: chrome.runtime.getURL("newtab.html") },
function(tab) {
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
// If the request is expected and comes from the tab we just opened
if(message.getScreenshot && sender.tab.id == tab.id) {
sendResponse(src);
// Ensure we're only run once
chrome.runtime.onMessage.removeListener(arguments.callee);
}
});
}
);
}
除此之外,请遵循链接的答案。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句