Home | History | Annotate | Download | only in js
      1 // Copyright 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 /**
      6  * Enum for modifier keys, same as DevTools protocol.
      7  * @enum {number}
      8  */
      9 var ModifierMask = {
     10   ALT: 1 << 0,
     11   CTRL: 1 << 1,
     12   META: 1 << 2,
     13   SHIFT: 1 << 3,
     14 };
     15 
     16 /**
     17  * Dispatches a context menu event at the given location.
     18  *
     19  * @param {number} x The X coordinate to dispatch the event at.
     20  * @param {number} y The Y coordinate to dispatch the event at.
     21  * @param {modifiers} modifiers The modifiers to use for the event.
     22  */
     23 function dispatchContextMenuEvent(x, y, modifiers) {
     24   var event = new MouseEvent(
     25       'contextmenu',
     26       {view: window,
     27        bubbles: true,
     28        cancelable: true,
     29        screenX: x,
     30        screenY: y,
     31        clientX: x,
     32        clientY: y,
     33        ctrlKey: modifiers & ModifierMask.CTRL,
     34        altKey: modifiers & ModifierMask.ALT,
     35        shiftKey: modifiers & ModifierMask.SHIFT,
     36        metaKey: modifiers & ModifierMask.META,
     37        button: 2});
     38 
     39   var elem = document.elementFromPoint(x, y);
     40   if (!elem) {
     41     throw new Error('cannot right click outside the visible bounds of the ' +
     42                     'document: (' + x + ', ' + y + ')');
     43   }
     44   elem.dispatchEvent(event);
     45 }
     46