B2G/gecko/testing/marionette/marionette-listener.js
/**
 * Saves a screenshot and returns a Base64 string
 */
Saves a screenshot and returns a Base64 string
Saves a screenshot and returns a Base64 string
function screenShot(msg) {
  let node = null;
  if (msg.json.element) {
    try {
      node = elementManager.getKnownElement(msg.json.element, curWindow)
    }
    catch (e) {
      sendResponse(e.message, e.code, e.stack, msg.json.command_id);
      return;
    }
  }
  else {
      node = curWindow;
  }
  let highlights = msg.json.highlights;
  var document = curWindow.document;
  var rect, win, width, height, left, top, needsOffset;
  if (node == curWindow) {
    win = node;
    width = win.innerWidth;
    height = win.innerHeight;
    top = 0;
    left = 0;
    needsOffset = true;
  }
  else {
    win = node.ownerDocument.defaultView;
    rect = node.getBoundingClientRect();
    width = rect.width;
    height = rect.height;
    top = rect.top;
    left = rect.left;
    needsOffset = false;
  }
  var canvas = document.createElementNS("http://www.w3.org/1999/xhtml", "canvas");
  canvas.width = width;
  canvas.height = height;
  var ctx = canvas.getContext("2d");
  ctx.drawWindow(win, left, top, width, height, 'rgb(255,255,255)');
  if (highlights) {
    ctx.lineWidth = "2";
    ctx.strokeStyle = "red";
    ctx.save();
    for (var i = 0; i < highlights.length; ++i) {
      var elem = highlights[i];
      rect = elem.getBoundingClientRect();
      var offsetY = 0, offsetX = 0;
      if (needsOffset) {
        var offset = getChromeOffset(elem);
        offsetX = offset.x;
        offsetY = offset.y;
      } else {
        offsetY = -top;
        offsetX = -left;
      }
      ctx.strokeRect(rect.left + offsetX, rect.top + offsetY, rect.width, rect.height);
    }
  }
  sendResponse({value:canvas.toDataURL("image/png","")}, msg.json.command_id);
}
function screenShot ( msg ) { let node = null ; if ( msg . json . element ) { try { node = elementManager . getKnownElement ( msg . json . element , curWindow ) } catch ( e ) { sendResponse ( e . message , e . code , e . stack , msg . json . command_id ) ; return ; } } else { node = curWindow ; } let highlights = msg . json . highlights ; var document = curWindow . document ; var rect , win , width , height , left , top , needsOffset ; if ( node == curWindow ) { win = node ; width = win . innerWidth ; height = win . innerHeight ; top = 0 ; left = 0 ; needsOffset = true ; } else { win = node . ownerDocument . defaultView ; rect = node . getBoundingClientRect ( ) ; width = rect . width ; height = rect . height ; top = rect . top ; left = rect . left ; needsOffset = false ; } var canvas = document . createElementNS ( "http://www.w3.org/1999/xhtml" , "canvas" ) ; canvas . width = width ; canvas . height = height ; var ctx = canvas . getContext ( "2d" ) ; ctx . drawWindow ( win , left , top , width , height , 'rgb(255,255,255)' ) ; if ( highlights ) { ctx . lineWidth = "2" ; ctx . strokeStyle = "red" ; ctx . save ( ) ; for ( var i = 0 ; i < highlights . length ; ++ i ) { var elem = highlights [ i ] ; rect = elem . getBoundingClientRect ( ) ; var offsetY = 0 , offsetX = 0 ; if ( needsOffset ) { var offset = getChromeOffset ( elem ) ; offsetX = offset . x ; offsetY = offset . y ; } else { offsetY = - top ; offsetX = - left ; } ctx . strokeRect ( rect . left + offsetX , rect . top + offsetY , rect . width , rect . height ) ; } } sendResponse ( { value : canvas . toDataURL ( "image/png" , "" ) } , msg . json . command_id ) ; }
Saves a screenshot and returns a Base64 string
Saves a screenshot and returns a Base64 string
// node can be either a window or an arbitrary DOM node // node is a window // offset needed for highlights to take 'outerHeight' of window into account // node is an arbitrary DOM node // offset for highlights not needed as they will be relative to this node // Draws the DOM contents of the window to the canvas // This section is for drawing a red rectangle around each element passed in via the highlights array // Don't need to offset the window chrome, just make relative to containing node // Draw the rectangle // Return the Base64 String back to the client bindings and they can manage // saving the file to disk if it is required