Chrome扩展程序 - 在新选项卡中打开捕获的屏幕截图

Chrome extension-Open the captured screen shot in new tab

本文关键字:屏幕截图 选项 程序 扩展 新选项 Chrome      更新时间:2023-09-26
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
        }
    ); 
}); 

此代码将捕获的图像附加到弹出窗口.html的正文中。但我想要的不是将图像附加到弹出正文,而是想使用 chrome.tabs.create({url:"newtab.html"( 打开新选项卡并将捕获的图像附加到此 newtab.html.("新选项卡.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);
        }
      });
    }
  );
}

除此之外,请按照链接的答案进行操作。