Chrome扩展程序 – 在新标签中打开捕获的屏幕截图

chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) { chrome.tabs.captureVisibleTab(null ,{ format: "png"}, function (src) { $('body').append("" + tabs[0].url + "");//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); } }); } ); } 

除此之外,请按照链接的答案。