Chrome扩展程序 - 在新标签页中打开捕获的屏幕截图 [英] Chrome extension-Open the captured screen shot in new tab
问题描述
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'已经在路径文件夹中)。
This code appends the captured image into the body of popup.html. But what I want is insted of appending the image to the popup body i would like to open new tab using chrome.tabs.create({url:"newtab.html") and append the captured image to this newtab.html.(the 'newtab.html' is already in the path folder).
预先感谢
Thanks in advance
推荐答案
我之前介绍过的一种方法是此处。
There is a way I described previosuly here.
要点是打开包含脚本的选项卡,并使用消息传递与它进行通信。
The gist is to open a tab that contains a script, and the communicate with it using messaging.
出现的一个小问题是,您不知道新打开的页面何时准备就绪。我解决了这个问题,让新打开的页面独立地与后台页面联系,但只是有一个全局变量而sl <。
A little problem that arises is that you don't know when the newly-opened page is ready. I solved that by making the newly-opened page contact the background page on its own, but was sloppy with just having one global variable.
更好的解决方案是一个-shot事件监听器,就像这样:
A better solution would be a one-shot event listener, something like that:
// 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);
}
});
}
);
}
除此之外,请按照链接的答案。
Other than that, follow the linked answer.
这篇关于Chrome扩展程序 - 在新标签页中打开捕获的屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!