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

阿比拉什
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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Chrome扩展程序:URL未在新标签页中打开

Chrome扩展程序:如何在新标签页中打开链接?

简单的Chrome扩展程序:在新标签页中打开一个网址

单击工具栏图标时,在新标签页中打开Chrome扩展程序

Chrome扩展程序在新标签页中运行js

Chrome扩展程序在新标签页上打开新标签页

开发chrome扩展程序的屏幕截图

Chrome扩展程序注入当前标签页

Firefox/Chrome 扩展程序 - 阻止新标签页

在新标签页上触发Chrome扩展程序

Chrome扩展程序仅在新标签页中正常运行

无法使用Chrome扩展程序弹出窗口中的按钮打开新标签页

Chrome扩展程序:打开新标签页,而不会失去弹出窗口的焦点

编辑 Chrome 扩展程序(Adblock)代码,使其在安装时不会打开新标签页

Chrome扩展程序-打开多个标签

在新标签页中打开打印屏幕(window.print)会冻结Chrome的父窗口

从chrome扩展程序打开的窗口中的活动面板中捕获屏幕

从Chrome扩展程序获取Chrome标签页pid

在Chrome扩展程序中打开(导入)文件

无法在Chrome中打开扩展程序窗口

Chrome扩展程序:如何在新标签页中显示背景图片

将内容写入Chrome扩展程序中的新空白标签

新标签中的Google Chrome扩展程序默认

简单的chrome扩展程序打开新标签并显示页面中的数据

Chrome扩展程序:如何捕获重定向?

Chrome扩展程序弹出窗口无法打开

Chrome扩展程序:无法创建标签

chrome扩展程序关闭或更新标签

每个标签的会话-Google Chrome扩展程序