firefox addon sdk 1.17截图

时间:2014-10-23 09:59:03

标签: javascript extjs firefox-addon-sdk

我正在使用sdk 1.17开发一个Firefox插件。它包含一个带有按钮的面板(使用ExtJs开发),我想在用户点击按钮时拍摄当前页面的屏幕截图。在Google Chrome中,有一个API(chrome.page-capture)就在那里。但我在Firefox中找不到类似的那个。在firefox中如何从main.js执行此任务。

1 个答案:

答案 0 :(得分:5)

O.k我找到了答案。此代码可用于拍摄整页屏幕。

在你的main.js中添加此代码。

  var window = require('sdk/window/utils').getMostRecentBrowserWindow();
  var tab = require('sdk/tabs/utils').getActiveTab(window);
  var myData;
  tabs.activeTab.attach({
    contentScript: "self.postMessage(document.body.scrollHeight);",//recieves the total scroll height of tab
    onMessage: function(data)
    {
      console.log("Tab data received: " + data);
      myData = data;
      var thumbnail = window.document.createElementNS("http://www.w3.org/1999/xhtml", "canvas");
      window = tab.linkedBrowser.contentWindow;
      thumbnail.width = window.screen.availWidth ;
      thumbnail.height = window.screen.availHeight ;
      var ctx = thumbnail.getContext("2d");
      var snippetWidth = window.outerWidth ;
      var snippetHeight = window.outerHeight ;
      ctx.canvas.left  = 0;
      ctx.canvas.top = 0;
      ctx.canvas.width  = window.innerWidth;
      ctx.canvas.height = myData;//canvas height is made equal to the scroll height of window
      ctx.drawWindow(window, 0, 0, snippetWidth, snippetHeight+myData, "rgb(255,255,255)");//

      var imageDataUri=thumbnail.toDataURL('image/png');
      imageDataUri = imageDataUri.replace("image/png", "image/octet-stream");        
      tabs.open(imageDataUri);
    }
  });

这是通过addon sdk 1.17完成的。很酷。

相关问题