【问题标题】:Chrome extension-Open the captured screen shot in new tabChrome 扩展 - 在新标签页中打开捕获的屏幕截图
【发布时间】:2014-10-07 06:32:11
【问题描述】:
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' 已经在路径文件夹中)。

提前致谢

【问题讨论】:

    标签: javascript jquery google-chrome google-chrome-extension


    【解决方案1】:

    我之前描述过一种方法here

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

    出现的一个小问题是您不知道新打开的页面何时准备就绪。我通过让新打开的页面自己联系背景页面来解决这个问题,但是因为只有一个全局变量而显得草率。

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

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

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

    【讨论】:

      猜你喜欢
      • 2012-03-23
      • 2017-11-14
      • 2017-11-04
      • 1970-01-01
      • 2011-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多