drawWindow()被多进程Firefox(e10s)打破?

时间:2016-01-04 17:38:58

标签: firefox firefox-addon firefox-addon-sdk jpm e10s

Firefox drawWindow() - 函数需要第一个参数a XUL content-window,由低级别的api tab utils提供。

然而,随着Firefox(代号电解或e10s)中多进程架构的引入,通过低级API直接访问选项卡是no longer possible。虽然有可用的兼容性填充程序,但明确声明它们不是support plattform APIs that expect DOM objects。 另一方面drawWindow()无法在内容脚本中使用,因为它是"chrome only"

所以我的问题是这些:

  1. 如果我不能在chrome之外使用它并且无法在chrome中获取contentWindow-object,我应该如何使用drawWindow()
  2. 让我的插件在多进程Firefox中截取网站截图有哪些其他选择?
  3. 我们当前的方法基于on the answer to this SO question。但是它不适用于多进程Firefox

1 个答案:

答案 0 :(得分:1)

使用drawWindow()的解决方案确实是使用了像Noitidart在评论中建议的framecripts。我用于截图的截图如下所示:

addMessageListener("fs/make_screenshot_from_rectangle", makeScreenshot);

function makeScreenshot(payload) {
    var rectangle = payload.data;
    var startX = rectangle.startX || 0;
    var startY = rectangle.startY || 0;
    var width = rectangle.width || content.innerWidth;
    var height = rectangle.height || content.innerHeight;
    // Create canvas to draw window unto
    var canvas = content.document.createElementNS("http://www.w3.org/1999/xhtml", "canvas");
    canvas.width = width;
    canvas.height = height;
    // Create context for drawing, draw the old window unto the canvas
    var context = canvas.getContext("2d");
    context.drawWindow(content, startX, startY, width, height, "rgb(255,255,255)");
    // Save context as png
    var image = canvas.toDataURL('image/png');
    sendAsyncMessage("got-screenshot", image);
}

使用以下函数从chrome脚本调用它:

function (rectangle) {
    var tab = require("sdk/tabs").activeTab;
    var xulTab = require("sdk/view/core").viewFor(tab);
    var xulBrowser = require("sdk/tabs/utils").getBrowserForTab(xulTab);

    var browserMM = xulBrowser.messageManager;
    if ( /* framescript not yet attached to tab */ ) {
        browserMM.loadFrameScript(require("sdk/self").data.url("content-scripts/frame-script.js"), false);
        ... // do something to remember that there is a framescript attached to the tab
        browserMM.addMessageListener("got-screenshot", function (payload) {
            ... // handle the screenshot
        });
    }
    browserMM.sendAsyncMessage('fs/make_screenshot_from_rectangle', rectangle);
}

相关阅读: