2014-10-07 57 views
8
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'已经在路径文件夹中)。Chrome浏览器扩展程序 - 在新标签页中打开捕获的屏幕截图

在此先感谢

回答

2

还有就是我previosuly here描述的方式。

要点是打开包含脚本的选项卡,并使用消息传递与它进行通信。

出现的一个小问题是,您不知道新打开的页面何时准备就绪。我通过让新打开的页面自己接触背景页面来解决这个问题,但是仅仅有一个全局变量就很sl sl。

更好的解决方案将是一个一次性的事件监听器,这样的事情:

// 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); 
     } 
     }); 
    } 
); 
} 

其他,跟随链接的答案。

相关问题